I have written a class to do compression/decompression using
System.IO.Compression.GZipStream. The compression seems to work fine,
but when I try and decompress a compressed string, nothing is returned
in the buffer...To do the processing, I pass the GZipStream as input
into the following function:
private void ProcessStreams(Stream input, Stream output) {
int offset = 0;
try {
if (input.CanRead & output.CanWrite) {
byte[] buffer = new byte[4096];
//make sure stream is at the beginning
if (input.CanSeek)
input.Seek(0,SeekOrigin.Begin);
while (true) {
//read input stream
int bytesRead = input.Read(buffer, 0,
bufferSize);
if (bytesRead > 0)
{
//write to output stream
output.Write(buffer, offset, bytesRead);
offset += bytesRead;
} else {
break;
}
}
//flush write buffer
output.Flush();
}
} catch (ArgumentNullException argEx) {
throw new CompressorException("Input stream is not
valid", ExceptionLevel.Error, false, this.ToString(),
"GetCompressionOutput",
argEx);
} catch (InvalidOperationException ioEx) {
throw new CompressorException("Input stream is
read-only", ExceptionLevel.Error, false, this.ToString(),
"GetCompressionOutput",
ioEx);
} catch (IOException ioEx) {
throw new CompressorException("Invalid I/O operation",
ExceptionLevel.Error, false, this.ToString(),
"GetCompressionOutput",
ioEx);
} catch (Exception ex) {
throw new CompressorException("Unspecified exception",
ExceptionLevel.Error, false, this.ToString(),
"GetCompressionOutput",
ex);
}
+------------------------------------------------------------------------+
I have read in doing research that the GZipStream doesn't return the
bytes read when running GZipStream.Read (is this true?), so when I
check the buffer, it is "empty". The base stream of the GZipStream is
a MemoryStream if that makes a difference.
Any help would be great. Thanks in advance.
Gabriele G. Ponti - 22 Jun 2006 16:09 GMT
I remember having a similar problem some months ago. In my case the culprit
was the compression part.
I have attached the class I wrote when I was testing the GZipStream class.
It's not production quality, so you want to use it only as reference. Also
be advised that in my example I use a header with the size of the
uncompressed data.
HTH,
Gabriele
>I have written a class to do compression/decompression using
> System.IO.Compression.GZipStream. The compression seems to work fine,
[quoted text clipped - 60 lines]
>
> Any help would be great. Thanks in advance.
Michael D. Ober - 23 Jun 2006 13:50 GMT
What are you attempting to acutally do? If it's reading and writing zip
files, import java.util.zip. You'll need a reference to "vjslib.dll" to use
it. A C# Zip example is at
http://msdn.microsoft.com/msdnmag/issues/03/06/ZipCompression/#S8
I use this library in a production application for our sales force.
Mike Ober.
> I have written a class to do compression/decompression using
> System.IO.Compression.GZipStream. The compression seems to work fine,
[quoted text clipped - 60 lines]
>
> Any help would be great. Thanks in advance.
7elephants - 23 Jun 2006 15:12 GMT
I am trying to decompress a stream via the GZipStream (GZip, not ZIP
algorithm) class...
As far as I know, that is all I need to do it...no other libraries
necessary.
> What are you attempting to acutally do? If it's reading and writing zip
> files, import java.util.zip. You'll need a reference to "vjslib.dll" to use
[quoted text clipped - 69 lines]
> >
> > Any help would be great. Thanks in advance.
Jon Skeet [C# MVP] - 24 Jun 2006 00:53 GMT
> I have written a class to do compression/decompression using
> System.IO.Compression.GZipStream. The compression seems to work fine,
> but when I try and decompress a compressed string, nothing is returned
> in the buffer...To do the processing, I pass the GZipStream as input
> into the following function:
Could you post a short but complete program which demonstrates the
problem?
See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
7elephants - 26 Jun 2006 14:21 GMT
Here is the method that does the compression/decompression...
private void ProcessStreams(Stream input, Stream output) {
int offset = 0;
try {
if (input.CanRead & output.CanWrite) {
Int32 bufferSize = 100;
Int32.TryParse(ConfigurationManager.AppSettings["bufferLimit"].ToString(),
out bufferSize);
byte[] buffer = new byte[bufferSize];
//make sure stream is at the beginning
if (input.CanSeek)
input.Seek(0,SeekOrigin.Begin);
while (true) {
//read input stream
int bytesRead = input.Read(buffer, 0,
bufferSize);
if ((bytesRead > 0) || (buffer[0] > 0))
{
//write to output stream
output.Write(buffer, offset, bytesRead);
offset += bytesRead;
//force new buffer
buffer = new byte[bufferSize];
} else {
break;
}
}
//flush write buffer
output.Flush();
}
} catch (ArgumentNullException argEx) {
throw new CompressorException("Input stream is not
valid", ExceptionLevel.Error, false, this.ToString(),
"GetCompressionOutput",
argEx);
} catch (InvalidOperationException ioEx) {
throw new CompressorException("Input stream is
read-only", ExceptionLevel.Error, false, this.ToString(),
"GetCompressionOutput",
ioEx);
} catch (IOException ioEx) {
throw new CompressorException("Invalid I/O operation",
ExceptionLevel.Error, false, this.ToString(),
"GetCompressionOutput",
ioEx);
} catch (Exception ex) {
throw new CompressorException("Unspecified exception",
ExceptionLevel.Error, false, this.ToString(),
"GetCompressionOutput",
ex);
}
+==========================================+
Here is the specific code that calls the method above...
...
GZipStream compressedStream = new GZipStream(inputStream,
CompressionMode.Decompress, true);
ProcessStreams(compressedStream, outputStream);
+==========================================+
When I change the base stream from a MemoryStream to a FileStream, the
decompression seems to work. I have seen information about
MemoryStreams and GZipStream not playing nice, but I haven't seen any
specifics.
Jon wrote:
> > I have written a class to do compression/decompression using
> > System.IO.Compression.GZipStream. The compression seems to work fine,
[quoted text clipped - 12 lines]
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
Jon Skeet [C# MVP] - 26 Jun 2006 20:37 GMT
> Here is the method that does the compression/decompression...
Please see http://www.pobox.com/~skeet/csharp/incomplete.html

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too