7
7elephants
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.
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.