I
ian_jacobsen
I'm having some trouble with GZipStream in System.IO.Compression. I
can compress a file without much trouble (at least as far as I can
see), however when I decompress the data multiple problems arise.
1) I'm missing a couple bytes of data after performing the
decompression. I've included my two sample files below. The file
test.txt is the original file, and test_after.txt is the file after
decompression. Notice that the test_after.txt is missing z-9. Why are
these bytes falling off?
2) Notice how I'm performing a Read from the unzipStream during
decompression. On the first Read, the count returned is always 0.
Then if I perform the Read again, the count returned is some positive
integer, however it does not match the size of the original file that
would be expected. This behavior is not consistent with Reads other
stream object, so what's the problem here? Am I crazy, or am I
missing something here?
P.S. - I followed the example that was given on MSDN for GZipStrem
that can be found at:
http://msdn2.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx
Thanks, --Ian
Contents of c:\temp\test.txt (no newline after second line):
ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
abcdefghijklmnopqrstuvwxyz0123456789
Contents of c:\temp\test_after.txt (with bytes missing!):
ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
abcdefghijklmnopqrstuvwxy
Sample code from a stub app (see noted comments):
using System.IO;
using System.IO.Compression;
private void button1_Click(object sender, EventArgs e)
{
// Read input file
FileStream fsInput =
new FileStream(@"c:\temp\test.txt", FileMode.Open);
int nInputFileSize = (int)fsInput.Length;
byte[] abInput = new byte[nInputFileSize];
fsInput.Read(abInput, 0, nInputFileSize);
// Compress
MemoryStream ms = new MemoryStream();
GZipStream zipStream = new System.IO.Compression.GZipStream(ms,
CompressionMode.Compress, false);
zipStream.Write(abInput, 0, nInputFileSize);
// Uncompress
ms.Seek(0, SeekOrigin.Begin);
GZipStream unzipStream = new GZipStream(ms,
CompressionMode.Decompress, false);
byte[] abOutput = new byte[nInputFileSize];
// NOTE: I know the next few lines look goofy,
// but the first Read does not fill the buffer
int nCount = 0;
while (nCount == 0 && nInputFileSize > 0)
{
nCount = unzipStream.Read(abOutput, 0, abOutput.Length);
}
if (nCount != nInputFileSize)
{
// NOTE: step over this exception to see the output file
throw new Exception("Uncompressed file size does not match original
file!");
}
FileStream fsOutput =
new FileStream(@"c:\temp\test_after.txt", FileMode.Create);
fsOutput.Write(abOutput, 0, abOutput.Length);
fsOutput.Close();
}
can compress a file without much trouble (at least as far as I can
see), however when I decompress the data multiple problems arise.
1) I'm missing a couple bytes of data after performing the
decompression. I've included my two sample files below. The file
test.txt is the original file, and test_after.txt is the file after
decompression. Notice that the test_after.txt is missing z-9. Why are
these bytes falling off?
2) Notice how I'm performing a Read from the unzipStream during
decompression. On the first Read, the count returned is always 0.
Then if I perform the Read again, the count returned is some positive
integer, however it does not match the size of the original file that
would be expected. This behavior is not consistent with Reads other
stream object, so what's the problem here? Am I crazy, or am I
missing something here?
P.S. - I followed the example that was given on MSDN for GZipStrem
that can be found at:
http://msdn2.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx
Thanks, --Ian
Contents of c:\temp\test.txt (no newline after second line):
ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
abcdefghijklmnopqrstuvwxyz0123456789
Contents of c:\temp\test_after.txt (with bytes missing!):
ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
abcdefghijklmnopqrstuvwxy
Sample code from a stub app (see noted comments):
using System.IO;
using System.IO.Compression;
private void button1_Click(object sender, EventArgs e)
{
// Read input file
FileStream fsInput =
new FileStream(@"c:\temp\test.txt", FileMode.Open);
int nInputFileSize = (int)fsInput.Length;
byte[] abInput = new byte[nInputFileSize];
fsInput.Read(abInput, 0, nInputFileSize);
// Compress
MemoryStream ms = new MemoryStream();
GZipStream zipStream = new System.IO.Compression.GZipStream(ms,
CompressionMode.Compress, false);
zipStream.Write(abInput, 0, nInputFileSize);
// Uncompress
ms.Seek(0, SeekOrigin.Begin);
GZipStream unzipStream = new GZipStream(ms,
CompressionMode.Decompress, false);
byte[] abOutput = new byte[nInputFileSize];
// NOTE: I know the next few lines look goofy,
// but the first Read does not fill the buffer
int nCount = 0;
while (nCount == 0 && nInputFileSize > 0)
{
nCount = unzipStream.Read(abOutput, 0, abOutput.Length);
}
if (nCount != nInputFileSize)
{
// NOTE: step over this exception to see the output file
throw new Exception("Uncompressed file size does not match original
file!");
}
FileStream fsOutput =
new FileStream(@"c:\temp\test_after.txt", FileMode.Create);
fsOutput.Write(abOutput, 0, abOutput.Length);
fsOutput.Close();
}