最简单的方法是将文件作为a打开,Stream
并使用压缩API包装它GZipStream
.
using (var fileStream = File.Open(theFilePath, FileMode.OpenOrCreate) {
using (var stream = new GZipStream(fileStream, CompressionMode.Compress)) {
// Write to the `stream` here and the result will be compressed
}
}
Run Code Online (Sandbox Code Playgroud)