-1
在.NET中,我有一個文件,其中包含1024字節的(未壓縮)標頭信息塊,後跟1MB用Deflate壓縮的數據塊。我如何解壓縮數據塊?我試圖打開文件作爲一個FileStream,跳過標題,其餘的一個MemoryStream,然後從MemoryStream創建一個DeflateStream對象,並閱讀,但我得到一個「塊長度不匹配其補充」 (據推測,這意味着「這個數據沒有正確放氣」)例外。.NET使用壓縮和非壓縮數據壓縮文件
// 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;
}