2

我正在尝试将 LZMA 流解码为流对象,以便我可以从中读取而不会占用内存。

我的输入文件如下所示:

some uncompressed data
.
.
some lzma compressed data
.
.

我想读取未压缩的数据,然后制作一个流对象来读取其余的压缩数据。

将整个内容读入 byte[] 不是一种选择,因为文件太大。我需要一个流(这必须是可能的,因为你可以压缩非常大的文件)

我尝试过使用sevenzipsharp,但是缺少文档使得具有我(不是很重要)经验的人无法理解。

有什么建议么?

编辑:我正在从文件读取到内存中,因此将 zip 文件解码为文件是不够的。

4

2 回答 2

0

您可以使用FileStream.Read方法读取流的未压缩部分。在读取所有未压缩部分后,该方法将底层流的位置推进到压缩部分的开头,从而有效地成为可用于解压缩的压缩部分的流。

FileStream.Read用未压缩的数据填充字节数组。要轻松解析其内容,您可以像这样使用BinaryReader

BinaryReader reader = BinaryReader(new MemoryStream(byteArray));
于 2015-07-09T07:01:09.007 回答
0
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();
 }
}
于 2016-07-31T16:13:31.300 回答