简体   繁体   English

GZIP压缩Java / C#压缩问题的区别

[英]GZIP Compression Java/C# difference in compression issue

I'm adding in compression to my project with the aim of improving speed in the 3G Data communication from Android app to ASP.NET C# Server. 我正在为我的项目添加压缩,旨在提高从Android应用程序到ASP.NET C#Server的3G数据通信的速度。

The methods I've researched/written/tested works. 我研究/编写/测试过的方法很有效。 However, there's added white space after compression. 但是,压缩后会增加空白区域。 And they are different as well. 它们也有所不同。 This really puzzles me. 这真让我困惑。

Is it something to do with different implementation of the GZIP classes in both Java/ASP.NET C#? 是否与Java / ASP.NET C#中的GZIP类的不同实现有关? Is it something that I should be concerned with or do I just move on with .Trim() and .trim() after decompressing? 这是我应该关注的事情,还是在解压后我继续使用.Trim()和.trim()?


Java , compressing "Mary had a little lamb" gives: Java ,压缩“玛丽有一只小羊羔”给出:

Compressed data length: 42 压缩数据长度:42
Base64 Compressed String: H4sIAAAAAAAAAPNNLKpUyEhMUUhUyMksKclJVchJzE0CAHrIujIWAAAA Base64压缩字符串:H4sIAAAAAAAAAPNNLKpUyEhMUUhUyMksKclJVchJzE0CAHrIujIWAAAA

protected static byte[] GZIPCompress(byte[] data) {
    try {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(byteArrayOutputStream);

        gZIPOutputStream.write(data);
        gZIPOutputStream.close();

        return byteArrayOutputStream.toByteArray();
    } catch(IOException e) {
        Log.i("output", "GZIPCompress Error: " + e.getMessage());
        return null;
    }
}


ASP.NET C# , compressing "Mary had a little lamb" ASP.NET C# ,压缩“玛丽有一只小羊羔”

Compressed data length: 137 压缩数据长度:137
Base64 Compressed String: H4sIAAAAAAAEAO29B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/Ir7I6ut0ns3SLC2Lti3ztMwWk/8Hesi6MhYAAAA= BASE64压缩字符串:H4sIAAAAAAAEAO29B2AcSZYlJi9tynt / SvVK1 + B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee ++ 999577733ujudTif33 / 8 / XGZkAWz2zkrayZ4hgKrIHz9 + FB8 / Ir7I6ut0ns3SLC2Lti3ztMwWk / 8Hesi6MhYAAAA =

    public static byte[] GZIPCompress(byte[] data)
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            using (GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Compress))
            {
                gZipStream.Write(data, 0, data.Length);
            }

            return memoryStream.ToArray();
        }
    }

I get 42 bytes on .NET as well. 我在.NET上也获得了42个字节。 I suspect you're using an old version of .NET which had a flaw in its compression scheme. 怀疑你使用的是旧版本的.NET,它的压缩方案存在缺陷。

Here's my test app using your code: 这是我的测试应用程序使用您的代码:

using System;
using System.IO;
using System.IO.Compression;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        var uncompressed = Encoding.UTF8.GetBytes("Mary had a little lamb");
        var compressed = GZIPCompress(uncompressed);
        Console.WriteLine(compressed.Length);
        Console.WriteLine(Convert.ToBase64String(compressed));
    }

    static byte[] GZIPCompress(byte[] data)
    {
        using (var memoryStream = new MemoryStream())
        {
            using (var gZipStream = new GZipStream(memoryStream, 
                                                   CompressionMode.Compress))
            {
                gZipStream.Write(data, 0, data.Length);
            }

            return memoryStream.ToArray();
        }
    }
}

Results: 结果:

42
H4sIAAAAAAAEAPNNLKpUyEhMUUhUyMksKclJVchJzE0CAHrIujIWAAAA

This is exactly the same as the Java data. 这与Java数据完全相同。

I'm using .NET 4.5. 我正在使用.NET 4.5。 I suggest you try running the above code on your machine, and compare the results. 我建议你尝试在你的机器上运行上面的代码,并比较结果。

I've just decompressed the base64 data you provided, and it is a valid "compressed" form of "Mary had a little lamb", with 22 bytes in the uncompressed data. 我刚解压缩你提供的base64数据,它一个有效的“压缩”形式的“玛丽有一只小羊羔”,在未压缩的数据中有22个字节。 That surprises me... and reinforces my theory that it's a framework version difference. 这让我感到惊讶......并强化了我的理论,即它是框架版本的差异。

EDIT: Okay, this is definitely a framework version difference. 编辑:好的,这绝对是框架版本的差异。 If I compile with the .NET 3.5 compiler, then use an app.config which forces it to run with that version of the framework, I see 137 bytes as well. 如果我使用.NET 3.5编译器进行编译,那么使用app.config强制它与该版本的框架一起运行,我也看到了137个字节。 Given comments, it looks like this was only fixed in .NET 4.5. 鉴于评论,它看起来只在.NET 4.5中得到修复。

You may compress data in Android Java and transmit to Asp.Net and vice-versa. 您可以在Android Java中压缩数据并传输到Asp.Net,反之亦然。 A wonderful approach to compress and decompress data between Android and Asp.Net is here in this article. 本文将介绍在Android和Asp.Net之间压缩和解压缩数据的一种很棒的方法。 This article talks about GZipStream and cross technological data transmission. 本文讨论了GZipStream和跨技术数据传输。

http://www.youritbuddy.com/blog/post/2016/11/15/data-compression-decompression-between-android-and-asp-net-csharp http://www.youritbuddy.com/blog/post/2016/11/15/data-compression-decompression-between-android-and-asp-net-csharp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM