GZipStream 机器依赖 [英] GZipStream machine dependence

查看:22
本文介绍了GZipStream 机器依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .NET 4.0 中遇到了一些奇怪的机器/操作系统依赖 GZipStream 行为.这是相关代码:

public static string Compress(string input) {使用(var ms = new MemoryStream(Encoding.UTF8.GetBytes(input)))使用(var os = new MemoryStream()){使用(var gz = new GZipStream(os,CompressionMode.Compress,true)) {ms.CopyTo(gz);}return string.Join("",os.ToArray().Select(b=>b.ToString("X2")));}}

运行 Compress("freek") 给了我

1F8B08000000000004004B2B4A4DCD06001E33909D05000000

在 Windows 7 和

<预> <代码> 1F8B0800000000000400ECBD07601C499625262F6DCA7B7F4AF54AD7E074A10880601324D8904010ECC188CDE692EC1D69472329AB2A81CA6556655D661640CCED9DBCF7DE7BEFBDF7DE7BEFBDF7BA3B9D4E27F7DFFF3F5C6664016CF6CE4ADAC99E2180AAC81F3F7E7C1F3F22CEEB3C7FFBFF040000FFFF1E33909D05000000

在 Windows Server 2008R2 上.两者都是64位.我希望结果是一样的.

当我解压缩任何一个结果时,两台机器都给出了正确的结果.我已经发现在 W7 ms.Length == 25 而在 W2K8R2 ms.Length==128 上,但不知道为什么.

怎么回事?

解决方案

宣布 .NET 4.5 Beta 包含 zip 压缩改进以减小大小:

<块引用>

从 .NET Framework 4.5 RC 开始,DeflateStream 类使用 zlib 库进行压缩.因此,它提供了更好的压缩算法,并且在大多数情况下,比在早期版本的 .NET Framework 中提供的压缩文件更小.

您是否在 Win7 机器上安装了 .Net 4.5+?

I'm running into some strange machine/OS dependent GZipStream behavior in .NET 4.0. This is the relevant code:

public static string Compress(string input) {
    using(var ms = new MemoryStream(Encoding.UTF8.GetBytes(input)))
    using(var os = new MemoryStream()) {
        using(var gz = new GZipStream(os,CompressionMode.Compress,true)) {
            ms.CopyTo(gz);
        }
        return string.Join("",os.ToArray().Select(b=>b.ToString("X2")));
    }
}

Running Compress("freek") gives me

1F8B08000000000004004B2B4A4DCD06001E33909D05000000

on Windows 7 and

1F8B0800000000000400ECBD07601C499625262F6DCA7B7F4AF54AD7E074A10880601324D8904010ECC188CDE692EC1D69472329AB2A81CA6556655D661640CCED9DBCF7DE7BEFBDF7DE7BEFBDF7BA3B9D4E27F7DFFF3F5C6664016CF6CE4ADAC99E2180AAC81F3F7E7C1F3F22CEEB3C7FFBFF040000FFFF1E33909D05000000

on Windows Server 2008R2. Both are 64bit. I would expect the results to be the same.

Both machines give the correct result when I decompress either result. I already found out that on W7 ms.Length == 25 while on W2K8R2 ms.Length==128, but no clue why.

What's going on?

解决方案

It was announced that .NET 4.5 Beta includes zip compression improvements to reduce the size:

Starting with the .NET Framework 4.5 RC, the DeflateStream class uses the zlib library for compression. 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.

Do you perhaps have .Net 4.5+ installed on the Win7 machine?

这篇关于GZipStream 机器依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆