如何使用7Z SDK来COM preSS和DECOM preSS文件 [英] How to use the 7z SDK to compress and decompress a file

查看:212
本文介绍了如何使用7Z SDK来COM preSS和DECOM preSS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据这个链接如何创建的7-Zip ?使用.NET档案,WOPR告诉我们如何COM preSS使用7Z SDK(LMZA(7Z COM pression算法)的文件的http://www.7-zip.org/sdk.html

According to this link How do I create 7-Zip archives with .NET? , WOPR tell us how to compress a file with LMZA (7z compression algorithm) using 7z SDK ( http://www.7-zip.org/sdk.html )

using SevenZip.Compression.LZMA;
private static void CompressFileLZMA(string inFile, string outFile)
{
   SevenZip.Compression.LZMA.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();
      }
   }
}

但如何DECOM preSS呢?

But how to decompress it?

我尝试:

private static void DecompressFileLZMA(string inFile, string outFile)
        {
            SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder();
            using (FileStream input = new FileStream(inFile, FileMode.Open))
            {
                using (FileStream output = new FileStream(outFile, FileMode.Create))
                {
                    coder.Code(input, output, input.Length, -1, null);
                    output.Flush();
                }
            }
        }

但没有成功。

你有一个工作的例子?

感谢

PS:
据一位其他code <一个href=\"http://www.koders.com/csharp/fid43E85EE5AE7BB255C69D18ECC3288285AD67A4A4.aspx?s=zip+en$c$cr#L5\">http://www.koders.com/csharp/fid43E85EE5AE7BB255C69D18ECC3288285AD67A4A4.aspx?s=zip+en$c$cr#L5 ,似乎脱codeR需要的报头,在该文件的工作的开始的字典。通过Koders生成此文件不是一个7z格式归档。

PS: According to an other code http://www.koders.com/csharp/fid43E85EE5AE7BB255C69D18ECC3288285AD67A4A4.aspx?s=zip+encoder#L5 , it seems that the decoder needs a header, a dictionary at the beginning of the file to work. This file generated by Koders is not a 7z archive.

   public static void Decompress(Stream inStream, Stream outStream)
    {
        byte[] properties = new byte[5];
        inStream.Read(properties, 0, 5);
        SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
        decoder.SetDecoderProperties(properties);
        long outSize = 0;
        for (int i = 0; i < 8; i++)
        {
            int v = inStream.ReadByte();
            outSize |= ((long)(byte)v) << (8 * i);
        }
        long compressedSize = inStream.Length - inStream.Position;
        decoder.Code(inStream, outStream, compressedSize, outSize, null);
    }

的特大计算比其的COM preSS的方法相同的方法。但如何计算,否则输出尺寸?

The outSize is computed the same way than their Compress method. But how to compute the output size otherwise?

推荐答案

这个问题是有点老了,但谷歌不能提供一个满意的答复,所以这是为那些像我一样仍然在寻求它。如果你看看SDK的LMZAAlone文件夹中有code,它融为一体presses和DECOM presses文件。使用它作为一个例子它似乎你需要写和读的EN codeR性能和DECOM presses文件大小到输出文件:

This question is a little old, but google fails to provide a satisfactory answer so this is for those like me still seeking it out. If you look into the LMZAAlone folder of the SDK there is code that compresses and decompresses files. Using it as an example it would seem you need to write and read the encoder properties and decompresses file size to your output file:

private static void CompressFileLZMA(string inFile, string outFile)
    {
        SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder();
        FileStream input = new FileStream(inFile, FileMode.Open);
        FileStream output = new FileStream(outFile, FileMode.Create);

        // Write the encoder properties
        coder.WriteCoderProperties(output);

        // Write the decompressed file size.
        output.Write(BitConverter.GetBytes(input.Length), 0, 8);

        // Encode the file.
        coder.Code(input, output, input.Length, -1, null);
        output.Flush();
        output.Close();
    }

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

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

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

请注意,这种方式创建的文件可以由7zip的程序以及被提取,但不会保留其文件名或任何其他元数据。

Note that the files created this way can be extracted by the 7zip program as well but will not retain their filename or any other metadata.

这篇关于如何使用7Z SDK来COM preSS和DECOM preSS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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