C# Zlib 兼容的压缩流?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/70347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Zlib-compatible compression streams?
提问by Ben Collins
Are System.IO.Compression.GZipStream or System.IO.Compression.Deflate compatible with zlib compression?
System.IO.Compression.GZipStream 或 System.IO.Compression.Deflate 是否与 zlib 压缩兼容?
采纳答案by Rasmus Faber
From MSDNabout System.IO.Compression.GZipStream:
从MSDN关于 System.IO.Compression.GZipStream :
This class represents the gzip data format, which uses an industry standard algorithm for lossless file compression and decompression.
此类表示 gzip 数据格式,它使用行业标准算法进行无损文件压缩和解压缩。
From the zlib FAQ:
The gz* functions in zlib on the other hand use the gzip format.
另一方面,zlib 中的 gz* 函数使用 gzip 格式。
So zlib and GZipStream should be interoperable, but only if you use the zlib functions for handling the gzip-format.
所以 zlib 和 GZipStream 应该是可互操作的,但前提是您使用 zlib 函数来处理 gzip 格式。
System.IO.Compression.Deflate and zlib are reportedly not interoperable.
据报道,System.IO.Compression.Deflate 和 zlib 不能互操作。
If you need to handle zip files (you probably don't, but someone else might need this) you need to use SharpZipLibor another third-party library.
如果您需要处理 zip 文件(您可能不需要,但其他人可能需要这个),您需要使用SharpZipLib或其他第三方库。
回答by Andreas Bakurov
They just compressing the data using zlib or deflate algorithms , but does not provide the output for some specific file format. This means that if you store the stream as-is to the hard drive most probably you will not be able to open it using some application (gzip or winrar) because file headers (magic number, etc ) are not included in stream an you should write them yourself.
他们只是使用 zlib 或 deflate 算法压缩数据,但不提供某些特定文件格式的输出。这意味着如果您将流按原样存储到硬盘驱动器中,您很可能无法使用某些应用程序(gzip 或 winrar)打开它,因为流中不包含文件头(幻数等),您应该自己写。
回答by configurator
I agree with andreas. You probably won't be able to open the file in an external tool, but if that tool expects a stream you might be able to use it. You would also be able to deflate the file back using the same compression class.
我同意安德烈斯的意见。您可能无法在外部工具中打开文件,但如果该工具需要流,您也许可以使用它。您还可以使用相同的压缩类将文件放回原处。
回答by Lasse V. Karlsen
gzip is deflate + some header/footer data, like a checksum and length, etc. So they're not compatible in the sense that one method can use a stream from the other, but they employ the same compression algorithm.
gzip 是 deflate + 一些页眉/页脚数据,如校验和和长度等。因此,它们不兼容,因为一种方法可以使用另一种方法的流,但它们采用相同的压缩算法。
回答by Isak Savo
I've used GZipStream to compress the output from the .NET XmlSerializer and it has worked perfectly fine to decompress the result with gunzip (in cygwin), winzip and another GZipStream.
我已经使用 GZipStream 来压缩来自 .NET XmlSerializer 的输出,并且使用 gunzip(在 cygwin 中)、winzip 和另一个 GZipStream 解压缩结果非常好。
For reference, here's what I did in code:
作为参考,这是我在代码中所做的:
FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
using (GZipStream gzStream = new GZipStream(fs, CompressionMode.Compress))
{
XmlSerializer serializer = new XmlSerializer(typeof(MyDataType));
serializer.Serialize(gzStream, myData);
}
Then, to decompress in c#
然后,在c#中解压
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
using (Stream input = new GZipStream(fs, CompressionMode.Decompress))
{
XmlSerializer serializer = new XmlSerializer(typeof(MyDataType));
myData = (MyDataType) serializer.Deserialize(input);
}
Using the 'file' utility in cygwin reveals that there is indeed a difference between the same file compressed with GZipStream and with GNU GZip (probably header information as others has stated in this thread). This difference, however, seems to not matter in practice.
在 cygwin 中使用“文件”实用程序表明,使用 GZipStream 和使用 GNU GZip 压缩的同一文件之间确实存在差异(可能是其他人在此线程中所述的标头信息)。然而,这种差异在实践中似乎无关紧要。
回答by Cheeso
DotNetZipincludes a DeflateStream, a ZlibStream, and a GZipStream, to handle RFC 1950, 1951, and 1952. The all use the DEFLATE Algorithm but the framing and header bytes are different for each one.
DotNetZip包括一个 DeflateStream、一个 ZlibStream 和一个 GZipStream,以处理 RFC 1950、1951 和 1952。它们都使用 DEFLATE 算法,但每个算法的帧和标头字节都不同。
As an advantage, the streams in DotNetZip do not exhibit the anomaly of expanding data sizeunder compression, reported against the built-in streams. Also, there is no built-in ZlibStream, whereas DotNetZip gives you that, for good interop with zlib.
作为一个优势,DotNetZip 中的流没有表现出在压缩下扩展数据大小的异常,与内置流相比。此外,没有内置的 ZlibStream,而 DotNetZip 为您提供了内置的 ZlibStream,以便与 zlib 进行良好的互操作。
回答by Blake Ramsdell
I ran into this issue with Git objects. In that particular case, they store the objects as deflated blobs with a Zlib header, which is documented in RFC 1950. You can make a compatible blob by making a file that contains:
我在使用 Git 对象时遇到了这个问题。在这种特殊情况下,它们将对象存储为带有 Zlib 标头的压缩 blob,该标头记录在RFC 1950 中。您可以通过制作包含以下内容的文件来制作兼容的 blob:
- Two header bytes (CMF and FLG from RFC 1950) with the values
0x78 0x01
CM
= 8 = deflateCINFO
= 7 = 32Kb windowFCHECK
= 1 = checksum bits for this header
- The output of the C#
DeflateStream
- An Adler32 checksum of the input data to the
DeflateStream
, big-endian format (MSB first)
- 两个标头字节(来自 RFC 1950 的 CMF 和 FLG)带有值
0x78 0x01
CM
= 8 = 放气CINFO
= 7 = 32Kb 窗口FCHECK
= 1 = 此标头的校验和位
- C#的输出
DeflateStream
- 输入数据的 Adler32 校验和,
DeflateStream
大端格式(MSB 优先)
I made my own Adler implementation
我做了我自己的 Adler 实现
public class Adler32Computer
{
private int a = 1;
private int b = 0;
public int Checksum
{
get
{
return ((b * 65536) + a);
}
}
private static readonly int Modulus = 65521;
public void Update(byte[] data, int offset, int length)
{
for (int counter = 0; counter < length; ++counter)
{
a = (a + (data[offset + counter])) % Modulus;
b = (b + a) % Modulus;
}
}
}
And that was pretty much it.
差不多就是这样。
回答by VirtualVDX
Starting from .NET Framework 4.5 the System.IO.Compression.DeflateStream
class uses the zlib library.
从 .NET Framework 4.5 开始,System.IO.Compression.DeflateStream
该类使用 zlib 库。
From the class's MSDN article:
来自该课程的MSDN 文章:
This class represents the Deflate algorithm, which is an industry-standard algorithm for lossless file compression and decompression. Starting with the .NET Framework 4.5, the DeflateStream class uses the zlib library. As a result, it provides a better compression algorithm and, in most cases, a smaller compressed file than it provides in earlier versions of the .NET Framework.
此类代表 Deflate 算法,这是一种用于无损文件压缩和解压缩的行业标准算法。从 .NET Framework 4.5 开始,DeflateStream 类使用 zlib 库。因此,它提供了更好的压缩算法,并且在大多数情况下,比在早期版本的 .NET Framework 中提供的压缩文件更小。