Compress
Basically, the compressed file will contain 3 things with the first 2 are metadata:
- The first 5 bytes are compression properties
- The next 8 bytes are file size before compression
- The compressed bytes
var encoder = New Encoder(); encoder.WriteCoderProperties(outStream); // Write properties encoder.Write(BitConverter.GetBytes(inputFileSize), 0, 8); // Write uncompressed file size encoder.Code(inStream, outSteam, inStream.Length, -1, null); // Actual compress
Decompress
To decompress the file, the metadata needs to be provided to the decoder. My code initially threw an error because there is no metadata.
var properties = new byte[5]; inStream.Read(properties, 0, 5); // Read properties var fileSizeBytes = new byte[8]; inStream.Read(fileSizeBytes, 0, 8); // Read uncompressed file size var fileSize = BitConverter.ToInt64(fileSizeBytes, 0); var decoder = New Decoder(); decoder.SetDecoderProperties(properties); // Provide the properties to decoder decoder.Code(inStream, outStream, inStream.Length, fileSize, null); // Actual decompress
No comments:
Post a Comment