web-dev-qa-db-fra.com

Comment compresser un tableau d'octets sans flux ni système io

J'essaie de coder une image dans un tableau d'octets et de l'envoyer à un serveur . L'encodage et l'envoi de pièces sont corrects, mais le problème est que le tableau d'octets est trop volumineux et prend trop de temps pour l'envoi. cela le ferait aller plus vite. mais le problème est que je ne peux pas utiliser system.io ni les flux. et je cible .net 2.0 . Merci.

9
Henjin
using System.IO;
using System.IO.Compression;

code:

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

Mis à jour

Utilisez la bibliothèque 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);
32
mjb

Compresse  

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

Décompresser

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