xorxornop / LZ4PCL

Streaming safe+unsafe x86/64 LZ4 compression portable class library in C#
Apache License 2.0
16 stars 3 forks source link

How would I know the output lenght for decode? #4

Open korhun opened 6 years ago

korhun commented 6 years ago

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.

    private static byte[] compress_LZ4PCL(byte[] arr)
     {
         return LZ4PCL.LZ4Codec.Encode64(arr,0, arr.Length);            
     }
     private static byte[] decompress_LZ4PCL(byte[] arr)
     {
           return LZ4PCL.LZ4Codec.Decode64(arr, 0, arr.Length, ????);

         byte[] res = new byte[????];
         int len = LZ4PCL.LZ4Codec.Decode64(arr, 0, arr.Length, res, 0, res.Length, false);
         return res;
     }
xorxornop commented 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();
}
korhun commented 6 years ago

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.