5

I have been reading for a short while about GZipStream and its Write method. What I am attempting to do is convert the compressed data from the stream and put it in a byte array. I will leave you with my code below as I believe it will help significantly.

public static void Compress(byte[] fi)
{
    using (MemoryStream inFile = new MemoryStream(fi))
    using (FileStream outFile = File.Create(@"C:\Compressed.exe"))
    using (GZipStream Compress = new GZipStream(outFile, CompressionMode.Compress))
    {
        inFile.CopyTo(Compress);
    }
}

Rather than writing to a file on my disk, I would like to write the compressed data onto a byte array, and then return the byte array (assuming I made this a function of course).

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502

2 Answers2

6

You can simply use use another MemoryStream and its ToArray method.

public static byte[] Compress(byte[] fi)
{
    using (MemoryStream outFile = new MemoryStream())
    {
        using (MemoryStream inFile = new MemoryStream(fi))
        using (GZipStream Compress = new GZipStream(outFile, CompressionMode.Compress))
        {
            inFile.CopyTo(Compress);
        }
        return outFile.ToArray();
    }
}
svick
  • 236,525
  • 50
  • 385
  • 514
  • I am actually coming across a slight error when using the memorystream. My old version produced a compressed file of 2.62kb, but when using the memory stream the compressed version is 2.61 kb. When I decompress the file the outcome is broken (will not run). Any suggestions? –  May 20 '11 at 21:07
  • 1
    @Evan, you're right. It seems `GZipStream` writes some final information to the stream when it's disposing, so you have to first dispose it and only then return the result. See updated answer. – svick May 20 '11 at 21:26
  • OH MY GOSH - it works! I have to ask ... how did you find that information out ... I have spent nearly 2 hours trying to make this work now! What a great feeling - so close. Thanks so much –  May 20 '11 at 21:29
  • @Evan, you mean the last bit? I though about the way streams work and that they usually have some buffer and it just hit me. – svick May 20 '11 at 22:13
  • just CompressionMode.Decompress to decompress ? – Cannon Jun 15 '11 at 03:40
3

From one of my extension libraries

public static string Compress(this string s)
    {
        byte[] bytesToEncode = Encoding.UTF8.GetBytes(s);
        return Convert.ToBase64String(bytesToEncode.Compress());
    }

    public static byte[] Compress(this byte[] bytesToEncode)
    {
        using (MemoryStream input = new MemoryStream(bytesToEncode))
        using (MemoryStream output = new MemoryStream())
        {
            using (System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(output, System.IO.Compression.CompressionMode.Compress))
            {
                input.CopyTo(zip);
            }
            return output.ToArray();
        }
    }

    public static string Explode(this string s)
    {
        byte[] compressedBytes = Convert.FromBase64String(s);
        return Encoding.UTF8.GetString(compressedBytes.Explode());
    }

    public static byte[] Explode(this byte[] compressedBytes)
    {
        using (MemoryStream input = new MemoryStream(compressedBytes))
        using (MemoryStream output = new MemoryStream())
        {
            using (System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(input, System.IO.Compression.CompressionMode.Decompress))
            {
                zip.CopyTo(output);
            }
            return output.ToArray();
        }
    }
Joe Enzminger
  • 11,110
  • 3
  • 50
  • 75