我正在尝试在Windows上使用GzipOutputStream,但是虽然流式压缩似乎很有效,但是我无法关闭文件。
我尝试的方式:
int output_file_handle = -1;
_sopen_s(&output_file_handle, accumulation_cache_file.c_str(),
_O_CREAT | _O_BINARY | _O_TRUNC| _O_WRONLY | _O_SEQUENTIAL, _SH_DENYNO, 0);
google::protobuf::io::FileOutputStream zero_copy_out_stream(output_file_handle);
google::protobuf::io::GzipOutputStream::Options compression_options;
compression_options.compression_level = 1; // fastest
google::protobuf::io::GzipOutputStream gzip_out_stream(&zero_copy_out_stream, compression_options);
if(output_file_handle != -1) {
for(int i=0...) {
...
google::protobuf::util::SerializeDelimitedToZeroCopyStream(some_message_pointer,
&gzip_out_stream);
if(i%10 == 0)
gzip_out_stream.Flush();
}
gzip_out_stream.Close();
//_close(output_file_handle);
}
该代码运行完毕后,该文件约为16MB(但只要应用程序仍在运行,我就无法关闭它,这意味着它没有正确关闭)。当我结束应用程序时,文件大小约为250MB。 如果我改为使用注释掉的_close(output_file_handle);然后该应用程序此时默默崩溃,文件大小为250MB。
如何在带有MSVC的Windows上正确执行此操作?