如何使用 SevenZip C# 解压缩内存流

问题描述

我使用下面的代码使用 SevenZip 压缩流,它产生一个输出,我将其写回到文件系统中。

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),8);
    encoder.Code(inputStream,outputStream,inputStream.Length,-1,null);
    outputStream.Flush();
    outputStream.Close();

    return outputStream.ToArray();
}

但是当我尝试解压缩输出文件时,使用下面的代码会引发错误

System.OverflowException: '数组维度超出了支持的范围。

在第 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,5);

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

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

我无法找出 SetDecoderProperties 中有什么不正确?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)