5

I know there is system.io.compression.gzipstream but it accept a stream as arguments.

I'm looking for a method that accept string

eg.

string compress(string stringtocompress,compressionlevel level);
string decompress(string stringtodecompress);
ollo
  • 24,797
  • 14
  • 106
  • 155
sparrows81
  • 431
  • 3
  • 6
  • 19
  • possible duplicate of [Compression/Decompression string with C#](http://stackoverflow.com/questions/7343465/compression-decompression-string-with-c-sharp) – alxx May 17 '13 at 11:29

2 Answers2

26

you should try this:

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

...

public static string Compress(string s)
{
    var bytes = Encoding.Unicode.GetBytes(s);
    using (var msi = new MemoryStream(bytes))
    using (var mso = new MemoryStream())
    {
        using (var gs = new GZipStream(mso, CompressionMode.Compress))
        {
            msi.CopyTo(gs);
        }
        return Convert.ToBase64String(mso.ToArray());
    }
}

public static string Decompress(string s)
{
    var bytes = Convert.FromBase64String(s);
    using (var msi = new MemoryStream(bytes))
    using (var mso = new MemoryStream())
    {
        using (var gs = new GZipStream(msi, CompressionMode.Decompress))
        {
            gs.CopyTo(mso);
        }
        return Encoding.Unicode.GetString(mso.ToArray());
    }
}
Jignesh.Raj
  • 5,776
  • 4
  • 27
  • 56
  • 4
    Keep in mind, base64 algorithm increases size of encoded data up to 35%. So it may 'eat' benefit from used compression. It's much better to set compression on protocol level (e.q. http) or use byte arrays instead of strings. – adobrzyc Mar 22 '18 at 13:43
  • changing to UTF8 works for me. If I use Unicode, the standard common decompression tool gives me "H.e.l.l.o." instead of "Hello" – Nick Chan Abdullah Sep 25 '19 at 10:09
5

Please try below, based on this url : http://dreamstatecoding.blogspot.com/2013/04/c-array-compress-and-decompress-with.html. We avoid to use double MemoryStream.

    public static byte[] Compress(string text)
    {
        var bytes = Encoding.Unicode.GetBytes(text);
        using (var mso = new MemoryStream())
        {
            using (var gs = new GZipStream(mso, CompressionMode.Compress))
            {
                gs.Write(bytes, 0, bytes.Length);
            }
            return mso.ToArray();
        }
    }

    public static string Decompress(byte[] data)
    {
        // Read the last 4 bytes to get the length
        byte[] lengthBuffer = new byte[4];
        Array.Copy(data, data.Length - 4, lengthBuffer, 0, 4);
        int uncompressedSize = BitConverter.ToInt32(lengthBuffer, 0);

        var buffer = new byte[uncompressedSize];
        using (var ms = new MemoryStream(data))
        {
            using (var gzip = new GZipStream(ms, CompressionMode.Decompress))
            {
                gzip.Read(buffer, 0, uncompressedSize);
            }
        }
        return Encoding.Unicode.GetString(buffer);            
    }
Haryono
  • 2,184
  • 1
  • 21
  • 14
  • Note that `gzip.Read(buffer, 0, uncompressedSize)` may not actually read the entire stream. You may have to do repeated calls in a loop looking at the return value to keep track of your position from how many bytes have actually been read into the buffer. See https://github.com/dotnet/runtime/issues/62954 for more insight. – Stephen Swensen Jun 25 '23 at 04:18
  • Also, the `uncompressedSize` can be calculated more succinctly with `BitConverter.ToInt32(data, data.Length - 4)` – Stephen Swensen Jun 25 '23 at 04:22