This is a migrated thread and some comments may be shown as answers.

Compress a byte[] array

3 Answers 885 Views
ZipLibrary
This is a migrated thread and some comments may be shown as answers.
Levi
Top achievements
Rank 1
Levi asked on 14 Oct 2011, 06:02 AM
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

3 Answers, 1 is accepted

Sort by
0
Viktor Tsvetkov
Telerik team
answered on 18 Oct 2011, 05:13 PM
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 >>

0
B
Top achievements
Rank 2
answered on 05 Mar 2014, 03:02 PM
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);



0
B
Top achievements
Rank 2
answered on 13 Mar 2014, 11:41 AM

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;
            }
        }
    }
}
Tags
ZipLibrary
Asked by
Levi
Top achievements
Rank 1
Answers by
Viktor Tsvetkov
Telerik team
B
Top achievements
Rank 2
Share this question
or