Search code examples
javafilechecksumfile-type

Create new file type, add checksum and data from compressed file?


I have a txt file which i compressed with an algorithm, and have calculated a MD5 checksum for the original txt file. Now i have to create a new file type (new extension) and add the compressed data and the checksum. After decompressing the new file type i need to check if the checksum matches with the original file.

My question is: What is the best way to create the new file type and add the data and checksum?

I was thinking about the following method: Write the checksum first into the new file (as plain txt) and then write the compressed data after that. When reading the file i read checksum first, dont close the InputStream and continue with reading the compressed data. Is this a good way to do it?

Any links or solution are welcome.

Thanks in advance.

NOTE: New file type can be simple and only 1 file will be compressed at a time


Solution

  • Your biggest consideration is the size of the compressed file. If the compressed file is still fairly large (1/2+ MB) I'd just use JSON or XML to encode the checksum & data fields. If you feel like this file type should be the absolute SMALLEST it can be, I would suggest allocating the first x bytes of data... 4 would be an int, 6 would be a long, but you might need more... the size of this field should be the MAXIMUM you could ever imagine your file size being