如何在不使用流或系统IO的情况下压缩字节数组

27

我想将一张图片编码成字节数组并发送到服务器。编码和发送部分都没有问题,但我的问题在于字节数组太大,发送时间太长,所以我想压缩它可以加快速度。但实际的问题是我不能使用 system.io 或 streams,而且我正在针对 .net 2.0。


你好,出于好奇,为什么不能使用system.io或streams?你使用的是不同的平台吗? - rollsch
2
我正在编写WebGL代码,但在WebGL中,我无法使用大多数 .net 库。 - Henjin
2个回答

71
using System.IO;
using System.IO.Compression;

代码:

public static byte[] Compress(byte[] data)
{
    MemoryStream output = new MemoryStream();
    using (DeflateStream dstream = new DeflateStream(output, CompressionLevel.Optimal))
    {
        dstream.Write(data, 0, data.Length);
    }
    return output.ToArray();
}

public static byte[] Decompress(byte[] data)
{
    MemoryStream input = new MemoryStream(data);
    MemoryStream output = new MemoryStream();
    using (DeflateStream dstream = new DeflateStream(input, CompressionMode.Decompress))
    {
        dstream.CopyTo(output);
    }
    return output.ToArray();
}

已更新

请使用7zip库:
http://www.splinter.com.au/compressing-using-the-7zip-lzma-algorithm-in/

// Convert the text into bytes
byte[] DataBytes = ASCIIEncoding.ASCII.GetBytes(OriginalText);

// Compress it
byte[] Compressed = SevenZip.Compression.LZMA.SevenZipHelper.Compress(DataBytes);

// Decompress it
byte[] Decompressed = SevenZip.Compression.LZMA.SevenZipHelper.Decompress(Compressed);

我使用了7Zip库,但它找不到SevenZipHelper的引用。你能帮忙吗? - PulkitG
@pulkit 目标框架(.NET 版本)与所引用的 7Zip DLL 的 .NET 版本构建不匹配。例如,您的项目是 .NET 2.0,但 7Zip DLL 是 .NET 4.0,或者反之亦然。 - mjb
这个有没有对应的NuGet库? - fly3rbug
我已经测试了DeflateStream,我的结果是如果有更多重复的字节,则可以压缩得更多,如果没有,则可能会出现比未压缩更大的大小。 - Alireza Jamali

-1

压缩

        public static byte[] Compress(byte[] inputData)
            {
                if (inputData == null)
                    throw new ArgumentNullException("inputData must be non-null");

                MemoryStream output = new MemoryStream();
                using (DeflateStream dstream = new DeflateStream(output, CompressionLevel.Optimal))
                {
                    dstream.Write(inputData, 0, inputData.Length);
                }
                return output.ToArray();
    }

OR



public static byte[] Compress(byte[] inputData)
    {
        if (inputData == null)
            throw new ArgumentNullException("inputData must be non-null");

        using (var compressIntoMs = new MemoryStream())
        {
            using (var gzs = new BufferedStream(new GZipStream(compressIntoMs,
             CompressionMode.Compress), BUFFER_SIZE))
            {
                gzs.Write(inputData, 0, inputData.Length);
            }
            return compressIntoMs.ToArray();
        }
    }

解压缩

    public static byte[] Decompress(byte[] inputData)
            {
                if (inputData == null)
                    throw new ArgumentNullException("inputData must be non-null");

                MemoryStream input = new MemoryStream(inputData);
                MemoryStream output = new MemoryStream();
                using (DeflateStream dstream = new DeflateStream(input, CompressionMode.Decompress))
                {
                    dstream.CopyTo(output);
                }
                return output.ToArray();

                if (inputData == null)
                    throw new ArgumentNullException("inputData must be non-null");
            }

OR

    public static byte[] Decompress(byte[] inputData)
            {
                if (inputData == null)
                    throw new ArgumentNullException("inputData must be non-null");

                using (var compressedMs = new MemoryStream(inputData))
                {
                    using (var decompressedMs = new MemoryStream())
                    {
                        using (var gzs = new BufferedStream(new GZipStream(compressedMs, CompressionMode.Decompress), BUFFER_SIZE))
                        {
                            gzs.CopyTo(decompressedMs);
                        }
                        return decompressedMs.ToArray();
                    }
                }
            }

1
该操作员已经说明他不能使用System.IO命名空间,因为他正在开发另一个没有该库的平台。 - mjb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接