我尝试使用此代码部分压缩文件
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();
}
}
}
}
但是我解压后文件损坏了。此代码有效
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);
}
}
}
}
为什么 gzip.Flush() 不起作用?为什么只有 gzip.Close() 有效?