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?
Có ngoại lệ nào không? Thông báo lỗi? – PVitt
tôi nhận được một NullReferenceException trên 'm_Coders [i] .Init(); ' trong Init() của lớp LiteralDecoder – Djax
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