I'm having trouble reading a compressed (deflated) data file using C # .NET DeflateStream(..., CompressionMode.Decompress) . The file was written earlier using DeflateStream(..., CompressionMode.Compress) , and it looks just fine (I can even unzip it using the Java program).
However, the first call to Read() input stream to decompress / inflate the compressed data returns a length of zero (end of file).
Here is the main driver that is used for compression and decompression:
public void Main(...) { Stream inp; Stream outp; bool compr; ... inp = new FileStream(inName, FileMode.Open, FileAccess.Read); outp = new FileStream(outName, FileMode.Create, FileAccess.Write); if (compr) Compress(inp, outp); else Decompress(inp, outp); inp.Close(); outp.Close(); }
Here is the basic decompression code that doesn't work:
public long Decompress(Stream inp, Stream outp) { byte[] buf = new byte[BUF_SIZE]; long nBytes = 0;
A called FAILS call always returns zero. What for? I know that it should be something simple, but I just do not see it.
Here is the basic compression code that works just fine, and is almost exactly the same as the decompression method with replaced names:
public long Compress(Stream inp, Stream outp) { byte[] buf = new byte[BUF_SIZE]; long nBytes = 0;
solvable
After viewing the correct solution, the constructor operator should be changed to:
inp = new DeflateStream(inp, CompressionMode.Decompress, true);
which saves the open input stream, and the following line should be added after calling inp.Flush() :
inp.Close();
Calling Close() causes the deflater thread to flush its internal buffers. The true flag prevents closing the underlying thread, which closes later in Main() . The same changes should be made to the Compress() method.