在.NET中,我有一个文件,其中包含一个1024字节的(未压缩)标头信息块,后跟一个使用Deflate压缩的1MB数据块。如何只解压缩数据块?
我尝试将文件作为FileStream打开,跳过标题,将其余部分写入MemoryStream,然后从MemoryStream创建一个DeflateStream对象并读取它,但我得到一个"块长度与其不匹配补充" (据说这意味着"这些数据没有正确放气")例外。
// FIn and FOut are input and output FileStreams, respectively
// UncompressedFileSize is the size of the uncompressed data
MemoryStream MS = new MemoryStream();
byte[] B = new byte[1048576];
// Skip the header, and read the data into the MemoryStream
FIn.Seek(1024, SeekOrigin.Begin);
FIn.Read(B, 0, 1048576);
MS.Write(B, 0, 1048576);
// Reset the MemoryStream, then feed it to a DeflateStream
MS.Seek(0L, SeekOrigin.Begin);
DeflateStream F = new DeflateStream(MS, CompressionMode.Decompress);
int BytesRemaining = UncompressedFileSize;
while (BytesRemaining > 0)
{
int ReadSize = 1048576;
if (ReadSize > BytesRemaining)
{
ReadSize = (int)BytesRemaining;
}
int BytesRead = F.Read(B, 0, ReadSize);
FOut.Write(B, 0, BytesRead);
BytesRemaining -= ReadSize;
}
答案 0 :(得分:-1)
没关系 - 事实证明我试图解压缩的Zip文件存在问题。该方法略有修改(读取标题;读取数据块,并将其提供给MemoryStream;重置MemoryStream的指针,然后读取它,将其提供给DeflateStream,并使用DeflateStream的CopyTo方法将其写入输出FileStream工作。