如何从 gzip 压缩文件转换为 Zstandard
Posted
技术标签:
【中文标题】如何从 gzip 压缩文件转换为 Zstandard【英文标题】:How to convert from a gzip compressed file to Zstandard 【发布时间】:2021-12-31 03:11:26 【问题描述】:如何将 gzip 压缩文件转换为 Zstandard?这是我的尝试:
using Zstandard.Net;
public static void ConvertToZstd(string path)
byte[] compressed = null;
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
using (var gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
using (var memoryStream = new MemoryStream())
using (var compressionStream = new ZstandardStream(memoryStream, CompressionMode.Compress))
compressionStream.CompressionLevel = 11; // optional!!
//compressionStream.CompressionDictionary = dictionary; // optional!!
compressionStream.Write(gzipStream, 0, gzipStream.Length);
compressionStream.Close();
compressed = memoryStream.ToArray();
【问题讨论】:
我会拆分这段代码。先解压gzip,看看能不能用,再压缩成ZstandardStream。 【参考方案1】:public static void ConvertToZstd(string path, string outputPath)
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
using (var gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
using (var memoryStream = new MemoryStream())
using (var compressionStream = new ZstandardStream(memoryStream, CompressionMode.Compress))
compressionStream.CompressionLevel = 11; // optional!!
//compressionStream.CompressionDictionary = dictionary; // optional!!
gzipStream.CopyTo(compressionStream);
compressionStream.Close();
File.WriteAllBytes(outputPath, memoryStream.ToArray());
【讨论】:
你可以将File.WriteAllBytes
移到内部using
块之外,而不是compressionStream.Close();
。以上是关于如何从 gzip 压缩文件转换为 Zstandard的主要内容,如果未能解决你的问题,请参考以下文章
如何解压缩从 iPhone 上的服务器接收到的 gzip 文件?