如何在没有流或系统io的情况下压缩字节数组

时间:2016-08-28 13:48:09

标签: c# .net compression

我正在尝试将图像编码为字节数组并将其发送到服务器。 编码和发送部分很好,但我的问题是字节数​​组太大,发送时间太长,所以我认为压缩它会使它更快。但实际问题是我不能使用system.io或stream。我的目标是.net 2.0。 谢谢。

2 个答案:

答案 0 :(得分:36)

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);

答案 1 :(得分:0)

压缩

        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();
                    }
                }
            }