How to Decompress memorystream using SevenZip C#

320 Views Asked by At

I'm using the below code to compress the stream using SevenZip, and it produces an output which I write back into filesystem.

private static byte[] Compress(byte[] input)
{
    var inputStream = new MemoryStream(input);
    var outputStream = new MemoryStream();

    Int32 dictionarySize = 1 << 16;
    string matchFinder = "bt4";
    Int32 numFastBytes = 128;

    CoderPropID[] propIDs =
    {
        CoderPropID.DictionarySize
        ,CoderPropID.MatchFinder
        ,CoderPropID.NumFastBytes
    };

    object[] properties =
    {
        (Int32)(dictionarySize)
        ,matchFinder
        ,(Int32)(numFastBytes)
    };

    SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
    encoder.SetCoderProperties(propIDs, properties);
    encoder.WriteCoderProperties(outputStream);
    outputStream.Write(BitConverter.GetBytes(inputStream.Length), 0, 8);
    encoder.Code(inputStream, outputStream, inputStream.Length, -1, null);
    outputStream.Flush();
    outputStream.Close();

    return outputStream.ToArray();
}

But when I try to DeCompress the output file, using below code it throws error

System.OverflowException: 'Array dimensions exceeded supported range.

at line coder.SetDecoderProperties(properties);.

private static void DecompressFileLZMA(string inFile, string outFile)
{
    SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder();
    FileStream input = new FileStream(inFile, FileMode.Open);
    FileStream output = new FileStream(outFile, FileMode.Create);

    // Read the decoder properties
    byte[] properties = new byte[5];
    input.Read(properties, 0, 5);

    // Read in the decompress file size.
    byte[] fileLengthBytes = new byte[8];
    input.Read(fileLengthBytes, 0, 8);
    long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);

    coder.SetDecoderProperties(properties);
    coder.Code(input, output, input.Length, fileLength, null);
    output.Flush();
    output.Close();
}

I'm not able to find out what is incorrect in SetDecoderProperties?

0

There are 0 best solutions below