Open korhun opened 6 years ago
You should really be using the provided stream functionality. When used in conjunction with a MemoryStream (or FileStream), this makes use very simple.
The provided LZ4Stream does encode the original length of each chunk that it encodes - see here.
The int outputLength
parameter can be 0
if the bool knownOutputLength
parameter is set to false
. I admit that this is probably a little confusing, but I never really intended for people to use the LZ4Codec class directly - the streaming functionality, LZ4Stream, should be used, since it makes use of this LZ4 compression/decompression very easy.
using (var fileStream = new FileStream("myFile.lz4", FileMode.Create)) {
using (var lz4Stream = new LZ4Stream(fileStream, CompressionMode.Compress)) {
lz4Stream.Write(myBytes, 0, myBytes.Length);
}
}
byte[] decompressed;
using (var byteStream = new MemoryStream()) {
using (var fileStream = new FileStream("myFile.lz4", FileMode.Open)) {
using (var lz4Stream = new LZ4Stream(fileStream, CompressionMode.Decompress)) {
lz4Stream.Read(myBytes, 0, myBytes.Length);
}
}
decompressed = byteStream.ToArray();
}
Thank you for your response and the valuable information. I think It would be nice to have a function for the ones that would only deal with byte arrays.
I couldn't understand how byteStream works in your 2nd code. It is only defined, nothing else. Could there be a mistake? Also an example without dealing files but only MemoryStream, that may be used for byte arrays would be nice. Sorry If you have already given that example somewhere else. Regards.
Hi
I am trying to compress and decompress byte arrays using your library. I could not understand how I could decode an encoded byte array since I cannot know the output array size.
I might of course add some bytes to store and read the initial (uncompressed) array lenght myself, but shouldn't the library do that? I really don't see why output lenght should be given for a decode process.
Any help is appreciated.