使用7zip sdk压缩文件,但存档文件不是原始文件,无法使用unrar解压缩

时间:2011-12-01 11:29:15

标签: c# 7zip

我正在使用7zip sdk(http://www.7-zip.org/sdk.html)来压缩文件。

使用这个包装器可以正常工作:

public void EncodeSingleFile(FileStream inStream, FileStream outStream)   {
     bool eos = false;
     Int32 dictionary = 1 << 21;
     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";

     propIDs = new CoderPropID[]
        {
           CoderPropID.DictionarySize,
           CoderPropID.PosStateBits,
           CoderPropID.LitContextBits,
           CoderPropID.LitPosBits,
           CoderPropID.Algorithm,
           CoderPropID.NumFastBytes,
           CoderPropID.MatchFinder,
           CoderPropID.EndMarker
        };
     properties = new object[]
        {
           dictionary,
           posStateBits,
           litContextBits,
           litPosBits,
           algorithm,
           numFastBytes,
           mf,
           eos
        };

     Encoder encoder = new Encoder();
     encoder.SetCoderProperties(propIDs, properties);
     encoder.WriteCoderProperties(outStream);
     Int64 fileSize = inStream.Length;
     for (int i = 0; i < 8; i++)
     {
        outStream.WriteByte((Byte) (fileSize >> (8*i)));
     }
     encoder.Code(inStream, outStream, -1, -1, null);   

}

然而,我遇到了一个问题:

我只能使用从7-zip.org安装的7zip shell解压缩它,无法用unrar解压缩。如果我设置了一些参数,它也可以用于unrar吗?

如果我在7zip中打开文件并检查属性,我得到了:

  • 姓名:abc.pdb
  • 尺寸:1 809 920
  • 包装尺寸:249 305
  • 方法:LZMA:21
  • 类型:lzma

2 个答案:

答案 0 :(得分:1)

有人在CodeProject上使用SDK为7z创建C#接口。他还提到现在可以使用COM来对抗DLL,但我不知道它是如何工作的。

在代码项目上查看C# (.NET) Interface for 7-Zip Archive DLLs

答案 1 :(得分:0)

如果您要创建RAR存档,可能会因为它是封闭源而运气不好。您可以使用外部库,但压缩算法是专有的。

来自RAR Wiki Page

  

RAR文件只能使用商业软件WinRAR,RAR和获得许可人Alexander Roshal许可的软件创建

我建议调查许多未受阻碍的替代方案中的一个。

相关问题