使用 7zip sdk 压缩文件,但无法使用 winrar 或 7zip 解压 [英] using 7zip sdk to compress a file, but can not decompress using winrar or 7zip

查看:33
本文介绍了使用 7zip sdk 压缩文件,但无法使用 winrar 或 7zip 解压的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从此处下载了 SDK 7zip.

I downloaded the SDK 7zip from here.

然后我用这段代码将文件压缩为 7zip:

Then I used this code to compress a file to 7zip:

private static void CompressFileLZMA(string inFile, string outFile)
{
    Encoder coder = new SevenZip.Compression.LZMA.Encoder();

    using (FileStream input = new FileStream(inFile, FileMode.Open))
    using (FileStream output = new FileStream(outFile, FileMode.Create))
    {
        coder.Code(input, output, -1, -1, null);
        output.Flush();
    }
}

我在网站上尝试了 SDK 版本 9.20 和 9.22 测试版.

I tried both the SDK versions 9.20 and 9.22 beta on the site.

压缩似乎可以将我的文件从 1.6 MB 压缩到 239 KB.

The compression seems working to compress my file from: 1.6 MB to 239 KB.

但是,如果我使用 WinRar 或 7zip 解压缩.他们无法识别存档文件,错误类似于

However, if I use WinRar or 7zip to decompress. the archive file is not recognized by them, the error is like

未知的存档文件或损坏的文件"

"unknown archive file or damaged file"

对此有什么想法吗?

推荐答案

你可以尝试使用 7zSharp wrapper或者至少分析包装器代码是如何完成的.

You can try to use 7zSharp wrapper or at least analyze wrappers code how everything is done.

压缩文件的代码(取自 7zSharp):

Code to compress file(taken from 7zSharp):

  public void EncodeSingleFile(string inFile, string outFile)
  {
     using (FileStream inStream = new FileStream(inFile, FileMode.Open, FileAccess.Read))
     {
        using (FileStream outStream = new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.Write))
        {
           EncodeSingleFile(inStream, outStream);
        }
     }
  }

  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);
  }

这篇关于使用 7zip sdk 压缩文件,但无法使用 winrar 或 7zip 解压的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆