Ionic.zlib stops compressing when the compressed file has reached 65536 bytes? - C#
Hey, I'm having problems compressing files with Ionic.zlib, I'm very new to C# so the problem may be easily solvable. If I compress a large file, let's say 500kb in size then once the compress开发者_StackOverflow社区ed file has reached 65536 bytes it will stop, if I then decompress the file theres a lot of data missing :/. I can fix this by setting the buffer to like 4,000,000, but I heard that it's best to have it set to 0x4000.
ZlibStream compressor = new ZlibStream(gsc_stream, CompressionMode.Compress, CompressionLevel.BestCompression, true);
byte[] buffer = new byte[0x4000];
Int32 n;
int previous = Convert.ToInt32(zone.Position);
while ((n = compressor.Read(buffer, 0, buffer.Length)) != 0)
{
zone.Write(buffer, 0, n);
}
zone.Flush();
compressor.Flush();
Looks like you have it the other way around.
If you're trying to compress the file in the stream gsc_stream
and write the result into the stream zone
then correct code would be:
using (ZlibStream compressor = new ZlibStream(zone, CompressionMode.Compress, CompressionLevel.BestCompression, true))
{
byte[] buffer = new byte[0x4000];
int n;
while ((n = gsc_stream.Read(buffer, 0, buffer.Length)) != 0)
{
compressor.Write(buffer, 0, n);
}
zone.Flush();
compressor.Flush();
}
精彩评论