我正在尝试将一个LZMA流解码为一个流对象,这样我就可以在不占用内存的情况下读取它。
我的输入文件如下所示:
some uncompressed data
.
.
some lzma compressed data
.
.
我想读取未压缩的数据,然后创建一个流对象来读取其余的压缩数据。
将整个文件读取到byte[]中是不可行的,因为文件太大了。我需要一个流(这必须是可能的,因为你可以压缩非常大的文件)
我试过使用sevenzipsharp,但由于缺乏文档,像我这样(不是很重要的)经验的人是不可能理解的。
有什么建议吗?
编辑:我正在从一个文件读取到内存中,所以将一个zip文件解码成一个文件是不够的。
发布于 2015-07-09 15:01:09
您可以使用FileStream.Read方法来读取流的未压缩部分。在读取所有未压缩部分之后,该方法将底层流的位置推进到压缩部分的开头,从而有效地成为可用于解压缩的压缩部分的流。
FileStream.Read
用未压缩的数据填充字节数组。为了方便地解析其内容,您可以像这样使用BinaryReader:
BinaryReader reader = BinaryReader(new MemoryStream(byteArray));
发布于 2016-08-01 00:13:31
public static void Decompress(string inFile, string outFile) {
try {
inStream = new FileStream(inFile, FileMode.Open);
outStream = new FileStream(outFile, FileMode.Create);
byte[] properties = new byte[5];
if (inStream.Read(properties, 0, 5) != 5)
throw (new Exception("Input stream is too short."));
Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder();
decoder.SetDecoderProperties(properties);
var br = new BinaryReader(inStream, Encoding.UTF8);
long decompressedSize = br.ReadInt64();
long compressedSize = br.ReadInt64();
decoder.Code(inStream, outStream, compressedSize, decompressedSize, null);
} catch (Exception e) {
throw e;
} finally {
inStream.Flush();
inStream.Close();
outStream.Flush();
outStream.Close();
}
}
https://stackoverflow.com/questions/31282418
复制相似问题