I've tried to use this code to compress a file in parts
using (var fsIn = new FileStream("test.avi", FileMode.Open))
{
using (var fsOut = new FileStream("test.avi.gz", FileMode.Create))
{
var buf = new byte[1024 * 1024];
using (var gzip = new GZipStream(fsOut, CompressionMode.Compress, true))
{
while (true)
{
var readCount = fsIn.Read(buf, 0, buf.Length);
if (readCount <= 0)
{
break;
}
gzip.Write(buf, 0, buf.Length);
gzip.Flush();
}
}
}
}
but i've got corrupted file after decompression. This code works
using (var fsIn = new FileStream("test.avi", FileMode.Open))
{
using (var fsOut = new FileStream("test.avi.gz", FileMode.Create))
{
var buf = new byte[1024*1024];
while (true)
{
var readCount = fsIn.Read(buf, 0, buf.Length);
if (readCount <= 0)
{
break;
}
// This string was transferred into "while" cycle
using (var gzip = new GZipStream(fsOut, CompressionMode.Compress, true))
{
gzip.Write(buf, 0, buf.Length);
}
}
}
}
Why gzip.Flush() doesn't work? Why only gzip.Close() works?