使用7zip sdk压缩文件,但压缩文件不是原来的文件,无法使用unrar解压
我正在使用 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
I am using 7zip sdk (http://www.7-zip.org/sdk.html) to compress a file.
it works fine using this wrapper:
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);
}
However, I have got a problem:
I can only decompress it using 7zip shell installed from 7-zip.org, can not decompress with unrar. is that possible if I set some parameters and it will work with unrar as well?
If I open the file in 7zip and check the properties, I got:
- Name: abc.pdb
- Size: 1 809 920
- Packed Size: 249 305
- Method: LZMA:21
- Type: lzma
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CodeProject 上有一个示例,其中有人使用 SDK 为 7z 创建了 C# 接口。他还提到现在可以对 DLL 使用 COM,但我不知道它是如何工作的。
查看代码上的 7-Zip 存档 DLL 的 C# (.NET) 接口项目。
There is an example on CodeProject of someone creating a C# interface for 7z using the SDK. He also mentions it is now possible to use COM against the DLL's, but I don't know how that works.
Check out C# (.NET) Interface for 7-Zip Archive DLLs on The Code Project.
如果您想要创建 RAR 存档,您可能会不走运,因为它是闭源的。您可能可以使用外部库,但压缩算法是专有的。
来自 RAR Wiki 页面:
。我建议您研究众多不受阻碍的替代方案之一。
If you are looking to create a RAR archive, you may be out of luck, as it is closed-source. You can probably use external libraries, but the compression algorithm is proprietary.
From the RAR Wiki Page:
I would recommend investigating one of the many unencumbered alternatives.