【问题标题】:How do I create 7-Zip archives containing multiple files with .NET?如何使用 .NET 创建包含多个文件的 7-Zip 存档?
【发布时间】:2015-12-26 01:07:28
【问题描述】:

Here 在这篇文章中提到了如何使用 LZMA SDK 压缩和解压缩文件。
这是代码:

public static void CompressFileLZMA(string inFile, string outFile)
{
    Int32 dictionary = 1 << 23;
    Int32 posStateBits = 2;
    Int32 litContextBits = 3; // for normal files
    // UInt32 litContextBits = 0; // for 32-bit data
    Int32 litPosBits = 0;
    // UInt32 litPosBits = 2; // for 32-bit data
    Int32 algorithm = 2;
    Int32 numFastBytes = 128;

    string mf = "bt4";
    bool eos = true;
    bool stdInMode = false;


    CoderPropID[] propIDs =  {
        CoderPropID.DictionarySize,
        CoderPropID.PosStateBits,
        CoderPropID.LitContextBits,
        CoderPropID.LitPosBits,
        CoderPropID.Algorithm,
        CoderPropID.NumFastBytes,
        CoderPropID.MatchFinder,
        CoderPropID.EndMarker
    };

    object[] properties = {
        (Int32)(dictionary),
        (Int32)(posStateBits),
        (Int32)(litContextBits),
        (Int32)(litPosBits),
        (Int32)(algorithm),
        (Int32)(numFastBytes),
        mf,
        eos
    };

    using (FileStream inStream = new FileStream(inFile, FileMode.Open))
    {
        using (FileStream outStream = new FileStream(outFile, FileMode.Create))
        {
            SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
            encoder.SetCoderProperties(propIDs, properties);
            encoder.WriteCoderProperties(outStream);
            Int64 fileSize;
            if (eos || stdInMode)
                fileSize = -1;
            else
                fileSize = inStream.Length;
            for (int i = 0; i < 8; i++)
                outStream.WriteByte((Byte)(fileSize >> (8 * i)));
            encoder.Code(inStream, outStream, -1, -1, null);
        }
    }

}

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

            byte[] properties = new byte[5];
            if (input.Read(properties, 0, 5) != 5)
                throw (new Exception("input .lzma is too short"));
            decoder.SetDecoderProperties(properties);

            long outSize = 0;
            for (int i = 0; i < 8; i++)
            {
                int v = input.ReadByte();
                if (v < 0)
                    throw (new Exception("Can't Read 1"));
                outSize |= ((long)(byte)v) << (8 * i);
            }
            long compressedSize = input.Length - input.Position;

            decoder.Code(input, output, compressedSize, outSize, null);
        }
    }
}

现在我需要使用 SDK 压缩和解压缩多个文件。
我应该在代码中进行哪些更改以获得最佳结果?

【问题讨论】:

    标签: c# 7zip lzma


    【解决方案1】:

    最后我决定使用SevenZipSharp。它具有压缩和解压缩多个文件、单个文件或字节或流的简单类。

    【讨论】:

      猜你喜欢
      • 2017-02-07
      • 2015-01-21
      • 2010-09-18
      • 1970-01-01
      • 2016-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多