Split and join back a binary file in java

后端 未结 7 848
闹比i
闹比i 2020-12-13 10:45

I am trying to divide a binary file (like video/audio/image) into chunks of 100kb each and then join those chunks back to get back the original file. My code seems to be wor

7条回答
  •  被撕碎了的回忆
    2020-12-13 11:19

    What happens when you do a binary comparison of the files. e.g. with diff. Do you see a difference after the first file?

    Can you try breaking up a text TXT file? if there are bytes are out of place it should be more obvious what is going wrong. e.g. a repeated block/file/or data full of nul bytes. ??

    EDIT: As others have noticed, you read the files in no particular order. What you can do is use a padded file number like.

    newName = String.format("%s.part%09d", fname, nChunks - 1);
    

    This will give you up to 1 billion files in numeric order.

    When you read the files, you need to ensure they are sorted.

    Arrays.sort(files);
    for (File file : files) {
    

    Using a custom comparator as others have suggest would reduce the size of the padded numbers but it can be nice to be able to sort by name to get the correct order. e.g. in explorer.

提交回复
热议问题