Compress a byte[] array

4 posts, 0 answers
  1. Levi
    Levi avatar
    7 posts
    Member since:
    Sep 2011

    Posted 14 Oct 2011 Link to this post

    Hello,
     
        I tried to compress a byte array in C# using Zip Library, but it seems I a missing something because every time I call compress sending a byte array parameter, I cannot decompress it correctly please see code as follows to compress

    private string Compress(byte[] data)
           {
               MemoryStream memoryStream = new MemoryStream();
               ZipCompression method = ZipCompression.Default;
               ZipOutputStream zipOutputStream = new ZipOutputStream(memoryStream, method);
               StreamWriter writer = new StreamWriter(zipOutputStream);
                
               writer.Write(data);
               writer.Flush();
               return Convert.ToBase64String(memoryStream.ToArray());
           }

    and the following is how I decompress it back and the function will return byte array as follows:

    private byte[] UnCompress(string str)
            {
                MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(str));
                ZipInputStream input = new ZipInputStream(memoryStream);
                StreamReader reader = new StreamReader(input);
     
                return memoryStream.ToArray();
     
            }

    Thanks,

    Levi
  2. Viktor Tsvetkov
    Admin
    Viktor Tsvetkov avatar
    382 posts

    Posted 18 Oct 2011 Link to this post

    Hi Levi,

    You simply need to use the defined StreamReader object in order to read from the ZipInputStream (opposite to what you do with the ZipOutputStream - you write data into it) as follows: reader.ReadToEnd(); and then if you need convert the string to byte array again.

    Kind regards,
    Viktor Tsvetkov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. B
    B avatar
    74 posts
    Member since:
    Jul 2011

    Posted 05 Mar 2014 in reply to Viktor Tsvetkov Link to this post

    Hi,

    Maybe a bit late reaction, but here is what I came up with.

    The compress/decompress code works on both Silverlight client (Silverlight dll) and server (WPF dll).

    Regards,

    Bayram
    public static byte[] Compress(string text)
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            using (ZipOutputStream zipOutputStream = new ZipOutputStream(memoryStream, ZipCompression.Default))
            {
                using (StreamWriter streamWriter = new StreamWriter(zipOutputStream))
                {
                    streamWriter.Write(text);
                    streamWriter.Flush();
                    byte[] bytes = memoryStream.ToArray();
                    return bytes;
                }
            }
        }
    }
    public static string Decompress(byte[] data)
    {
        using (MemoryStream memoryStream = new MemoryStream(data))
        {
            using (ZipInputStream zipInputStream = new ZipInputStream(memoryStream))
            {
                using (StreamReader streamReader = new StreamReader(zipInputStream, new System.Text.UTF8Encoding()))
                {
                    string text = streamReader.ReadToEnd();
                    return text;
                }
            }
        }
    }

    Examples of how to use them:

    // From XElement to compressed byte[]
    XElement groupXml = DefinitionSelector.GetGroupDefinition(groupName, definitionFile);
    string text = groupXml.ToString();
    byte[] c = PowerUtils.Compress(text);
     
    // From compressed byte[] to XElement
    string text = PowerUtils.Decompress(data);
    XElement groupXml = XElement.Parse(text);
    DefinitionSelector.PutGroupDefinition(groupName, groupXml);



  4. B
    B avatar
    74 posts
    Member since:
    Jul 2011

    Posted 13 Mar 2014 in reply to B Link to this post

    Changed this code to work with the new version 2014 Q1

    public static byte[] Compress(string text)
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            using (ZipOutputStream zipOutputStream = new ZipOutputStream(memoryStream, ZipCompression.Deflated))
            {
                using (StreamWriter streamWriter = new StreamWriter(zipOutputStream))
                {
                    streamWriter.Write(text);
                    //streamWriter.Flush();
                }
                byte[] bytes = memoryStream.ToArray();
                return bytes;
            }
        }
    }
    public static string Decompress(byte[] data)
    {
        using (MemoryStream memoryStream = new MemoryStream(data))
        {
            using (ZipInputStream zipInputStream = new ZipInputStream(memoryStream))
            {
                using (StreamReader streamReader = new StreamReader(zipInputStream, new System.Text.UTF8Encoding()))
                {
                    string text = streamReader.ReadToEnd();
                    return text;
                }
            }
        }
    }
Back to Top