我压缩日志文件数据写入到它,像:碰撞安全上的即时压缩与GZipStream
using (var fs = new FileStream("Test.gz", FileMode.Create, FileAccess.Write, FileShare.None))
{
using (var compress = new GZipStream(fs, CompressionMode.Compress))
{
for (int i = 0; i < 1000000; i++)
{
// Clearly this isn't what is happening in production, just
// a simply example
byte[] message = RandomBytes();
compress.Write(message, 0, message.Length);
// Flush to disk (in production we will do this every x lines,
// or x milliseconds, whichever comes first)
if (i % 20 == 0)
{
compress.Flush();
}
}
}
}
我想要确保的是,如果该进程崩溃或死亡,档案仍然有效和可读。我曾希望自上次冲洗以来的任何事情都是安全的,但我只是以腐败归档而告终。
有没有什么方法可以确保每次刷新后我都会得到一个可读的归档文件?
注:我们使用GZipStream并不重要,如果别的东西会给我们想要的结果。
为什么不以全胖模式写出文件,并且一旦它们在磁盘上就按计划批量压缩?这将是一大堆更安全。 – spender 2013-03-27 11:26:07
因为我们想要保持磁盘利用率低。但是,如果进程死亡,我们也希望最大化可读数据量 – Cocowalla 2013-03-27 11:30:55
我也不确定如果我们能够批量压缩 - 文件每分钟滚动一次,但每个文件可以高达500MB左右没有压缩 – Cocowalla 2013-03-27 11:33:26