2011-10-04 47 views
15

Theo liên kết này How do I create 7-Zip archives with .NET?, WOPR cho chúng tôi biết làm thế nào để nén một tập tin với LMZA (thuật toán nén 7z) sử dụng 7z SDK (http://www.7-zip.org/sdk.html)Làm thế nào để sử dụng SDK 7z để nén và giải nén một file

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

Nhưng làm thế nào để giải nén nó?

tôi cố gắng:

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

nhưng không thành công.

Bạn có ví dụ làm việc không?

Cảm ơn

PS: Theo một mã khác http://www.koders.com/csharp/fid43E85EE5AE7BB255C69D18ECC3288285AD67A4A4.aspx?s=zip+encoder#L5, có vẻ như bộ giải mã cần có một tiêu đề, một cuốn từ điển ở phần đầu của tập tin để làm việc. Tệp này được tạo bởi Koders không phải là một kho lưu trữ 7z.

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

Phương thức thanh toán ngoài được tính giống như phương pháp nén của chúng. Nhưng làm thế nào để tính toán kích thước đầu ra nếu không?

+0

Có ngoại lệ nào không? Thông báo lỗi? – PVitt

+0

tôi nhận được một NullReferenceException trên 'm_Coders [i] .Init(); ' trong Init() của lớp LiteralDecoder – Djax

+0

Ngoài ra còn có một chút hoàn thiện hơn câu trả lời ở đây: http://stackoverflow.com/ a) 8775927/220904 – Vando

Trả lời

26

Câu hỏi này hơi cũ, nhưng google không cung cấp câu trả lời thỏa đáng vì vậy điều này là dành cho những người như tôi vẫn đang tìm kiếm. Nếu bạn nhìn vào thư mục LMZAAlone của SDK, có mã để nén và giải nén các tệp. Sử dụng nó như là một ví dụ nó sẽ có vẻ bạn cần phải viết và đọc các thuộc tính mã hóa và giải nén kích thước tập tin vào tập tin đầu ra của bạn:

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

Lưu ý rằng các tập tin được tạo ra theo cách này có thể được tách ra bởi chương trình 7zip cũng nhưng sẽ không giữ lại tên tệp hoặc bất kỳ siêu dữ liệu nào khác.

+0

Tôi biết đây là một nhận xét cũ (và đăng), nhưng tôi đã thử mã này và nó không hoạt động. Khi mở tệp lưu trữ mới, Windows 10 cho biết tệp không hợp lệ và sẽ không mở tệp đó. –

+0

Tôi đã thử nó với phiên bản mới nhất của chương trình và thư viện 7zip và nó vẫn hoạt động cho tôi. Những gì bạn có nghĩa là Windows 10 nói rằng nó không hợp lệ? Bạn có đang cố mở tệp bằng trình xem tệp nén được tích hợp trong Windows Explorer không? Tôi không nghĩ rằng hỗ trợ 7zip/LZMA. – Fr33dan

+0

Tắt trình xem lưu trữ của Windows 10 không hỗ trợ .7z. Mã của bạn hoạt động, tuy nhiên, khi giải nén các tập tin lưu trữ, phần mở rộng tập tin không xuất hiện để có mặt, mặc dù nó được bao gồm trong chuỗi đầu vào. Tệp vẫn hợp lệ, nó chỉ thiếu phần mở rộng; làm thế nào bạn có thể sửa lỗi này? –

Các vấn đề liên quan