-1

In .NET, I have a file that has a 1024-byte block of (uncompressed) header information, followed by a 1MB block of data that is compressed with Deflate. How do I decompress just the data block?

I tried opening the file as a FileStream, skipping the header, writing the rest to a MemoryStream, then creating a DeflateStream object from the MemoryStream and reading that, but I got a "block length does not match with its complement" (which supposedly means "this data isn't Deflated properly") exception.

// 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;
}
Don Del Grande
  • 411
  • 6
  • 20

1 Answers1

-1

Never mind - it turns out that there was a problem with the Zip file I was trying to decompress. The method, slightly modified (read the header; read the data block, and feed it into a MemoryStream; reset the MemoryStream's pointer, then read it, feed it to a DeflateStream, and use the DeflateStream's CopyTo method to write it to the output FileStream) works.

Don Del Grande
  • 411
  • 6
  • 20