Amazing .Net C # DeflateStream issue
I wonder if anyone can shed some light on the problem that is driving me crazy?
I am writing a compression test class. To test this, I serialize the dataset into a memory stream, compress it and unpack it and compare the results.
The squeeze is fine, but the lag is where it gets in the dirt. This is the unpacking function:
public static Stream GetUncompressedStreamCopy(Stream inStream)
{
Stream outStream = new MemoryStream();
inStream.Position = 0;
DeflateStream uncompressStream = new DeflateStream(inStream,
CompressionMode.Decompress, true);
byte[] buffer = new byte[65536];
int totalread = 0;
int bytesread = 0;
do {
bytesread = uncompressStream.Read(buffer, 0, buffer.Length);
totalread += bytesread;
outStream.Write(buffer, 0, bytesread);
Console.WriteLine("bytesRead: [{0}]\t outStream.Length [{1}]",
bytesread, outStream.Length);
} while (bytesread > 0);
Console.WriteLine("total bytes read [{0}]", totalread);
outStream.Flush();
return outStream;
}
With a buffer size of 65536, an uncompressed stream always returns one byte less than it was uncompressed.
This now brings me to the second question that I am fighting. With some buffer sizes, uncompressStream.Read returns 0, although there is still compressed data left to retrieve.
For these cases, deflateStream.Read (s) only once in the do {} loop, and then returns an uncompressed stream equal to the buffering, if you increment the buffering one byte at a time, all is well (except for the missing byte).
Buffering Output 65536: (original uncompressed data - 207833)
bytesRead: [65536] outStream.Length [65536]
bytesRead: [65536] outStream.Length [131072]
bytesRead: [58472] outStream.Length [189544]
bytesRead: [18288] outStream.Length [207832]
bytesRead: [0] outStream.Length [207832]
total bytes read [207832]
buffersize of 189544 (Some magic number where code loops are)
bytesRead: [189544] outStream.Length [189544]
bytesRead: [0] outStream.Length [189544]
total bytes read [189544]
Unompressed stream size 189544
Also notice the 3rd buffering read 65536 ex: bytesRead: [58472] Obviously this should also be 65536 since there is still data remaining in the buffer?
Any ideas are greatly appreciated.
TIA
- Jaco
source to share
Well, I couldn't figure out your problem, but follow some code I wrote sometime ago for ICSharpCode.SharpZipLib ;
byte[] compressedData;
using(MemoryStream ms = new MemoryStream())
{
Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, true);
Stream s = new DeflaterOutputStream(ms, deflater);
s.Write(sendData, 0, sendData.Length);
s.Close();
compressedData = (byte[])ms.ToArray();
}
// ...
MemoryStream inflated = new MemoryStream();
using (Stream inflater = new InflaterInputStream(
inputStream, new Inflater(true)))
{
int count = 0;
byte[] deflated = new byte[4096];
while ((count = inflater.Read(deflated, 0, deflated.Length)) != 0)
{
inflated.Write(deflated, 0, count);
}
inflated.Seek(0, SeekOrigin.Begin);
}
byte[] content = new byte[inflated.Length];
inflated.Read(content, 0, content.Length);
source to share