Click here to Skip to main content
14,741,362 members
Articles » Desktop Development » Files and Folders » Compression
Posted 23 Jun 2008


40 bookmarked

GZipStream - Compress/Decompress a String

Rate me:
Please Sign up or sign in to vote.
2.64/5 (16 votes)
23 Jun 2008CPOL
An article on how use GZipStream with string as input parameter.


This article presents two methods to compress and decompress strings using System.IO.Compression.GZipStream.


After converting code from VB.NET 1.1 to C#.NET 3.5, I needed to change some code using a third party zip class to GZipStream. Code samples found on the web or on VS help were presenting solutions dealing with FileStream but in this case, a string is given as an input parameter.

So the little challenge was to go from a string to a byte array (byte[]) and vice versa without losing a char nor changing its encoding because I ended up finding that System.Text.Encoding.Unicode.GetBytes()/GetString() previously used in the VB code was not acting properly, resulting in a loss of characters. Plus the Flush() method on GZipStream with the compression option doesn't flush everything...


So as I found no method transforming a string into a byte[] and vice versa without involving encoding specifications, I ended up with a loop and a cast. If you have a better idea, please tell me so.

And I found out, if you only flush a GZipStream instance and retrieve the data from the underlying stream without closing this GZipStream instance, you miss part of the data.

The Code

Compress/zip a string:

public static string Zip(string value)
    //Transform string into byte[]
    byte[] byteArray = new byte[value.Length];
    int indexBA = 0;
    foreach (char item in value.ToCharArray())
        byteArray[indexBA++] = (byte)item;

    //Prepare for compress
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms,

    sw.Write(byteArray, 0, byteArray.Length);
    //Close, DO NOT FLUSH cause bytes will go missing...

    //Transform byte[] zip data to string
    byteArray = ms.ToArray();
    System.Text.StringBuilder sB = new System.Text.StringBuilder(byteArray.Length);
    foreach (byte item in byteArray)
    return sB.ToString();

Decompress/Unzip a string: input value has been previously compressed with GZipStream.

public static string UnZip(string value)
    //Transform string into byte[]
    byte[] byteArray = new byte[value.Length];
    int indexBA = 0;
    foreach (char item in value.ToCharArray())
        byteArray[indexBA++] = (byte)item;

    //Prepare for decompress
    System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray);
    System.IO.Compression.GZipStream sr = new System.IO.Compression.GZipStream(ms,

    //Reset variable to collect uncompressed result
    byteArray = new byte[byteArray.Length];

    int rByte = sr.Read(byteArray, 0, byteArray.Length);

    //Transform byte[] unzip data to string
    System.Text.StringBuilder sB = new System.Text.StringBuilder(rByte);
    //Read the number of bytes GZipStream red and do not a for each bytes in
    for (int i = 0; i < rByte; i++)
    return sB.ToString();

Points of Interest

Well, if you haven't dealt with a lot of streams like me, hopefully this will avoid loss of time!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Systems Engineer
United States United States
No Biography provided

Comments and Discussions

Suggestiontransforming a string into a byte[] and vice versa without involving encoding specifications Pin
Member 1383323917-May-18 23:39
MemberMember 1383323917-May-18 23:39 
QuestionLosing Data Pin
Mansoor ali P11-Apr-16 19:35
MemberMansoor ali P11-Apr-16 19:35 
QuestionUse this code instead Pin
Jason Jakob17-Dec-14 13:28
MemberJason Jakob17-Dec-14 13:28 
SuggestionThanks & a small correction! Pin
amitthk29-Sep-14 8:28
professionalamitthk29-Sep-14 8:28 
GeneralMy vote of 1 Pin
Yclkvnc8-May-13 7:28
MemberYclkvnc8-May-13 7:28 
GeneralMy vote of 4 Pin
Nasenbaaer12-Apr-13 7:24
MemberNasenbaaer12-Apr-13 7:24 
SuggestionSharing my code Pin
SamNaseri16-Nov-11 19:38
MemberSamNaseri16-Nov-11 19:38 
GeneralRe: Sharing my code Pin
Thymine20-Mar-12 12:40
MemberThymine20-Mar-12 12:40 
GeneralRe: Sharing my code Pin
SamNaseri20-Mar-12 13:53
MemberSamNaseri20-Mar-12 13:53 
GeneralRe: Sharing my code Pin
Frankidoze21-Mar-12 7:00
MemberFrankidoze21-Mar-12 7:00 
GeneralThis code will work properly if Pin
Member 28508507-Jun-11 20:16
MemberMember 28508507-Jun-11 20:16 
GeneralThis article is full of bugs, don't use it! Pin
xxbbcc28-May-11 17:51
Memberxxbbcc28-May-11 17:51 
GeneralMy vote of 1 Pin
xxbbcc28-May-11 17:42
Memberxxbbcc28-May-11 17:42 
GeneralMy vote of 1 Pin
djpitagora27-Sep-10 8:04
Memberdjpitagora27-Sep-10 8:04 
GeneralRe: My vote of 1 Pin
Frankidoze21-Mar-12 6:50
MemberFrankidoze21-Mar-12 6:50 
GeneralDecompress bug fix Pin
filoteanuadrian9-Apr-10 5:10
Memberfiloteanuadrian9-Apr-10 5:10 
GeneralRe: Decompress bug fix Pin
TEST ME26-Apr-11 17:37
MemberTEST ME26-Apr-11 17:37 
GeneralDoesn't work at all Pin
Vikcia26-Mar-10 8:49
MemberVikcia26-Mar-10 8:49 
GeneralPublic Class CZip .... Pin
Erfan2-Feb-10 20:45
MemberErfan2-Feb-10 20:45 
GeneralRe: Public Class CZip .... Pin
Nasenbaaer12-Apr-13 7:23
MemberNasenbaaer12-Apr-13 7:23 
GeneralobjCompressedStream.Length not supported Pin
Upendra Jagdale26-Aug-09 21:17
professionalUpendra Jagdale26-Aug-09 21:17 
GeneralPerformance Improvement/Cleanup Pin
matthias Weiser10-Dec-08 4:34
Membermatthias Weiser10-Dec-08 4:34 
GeneralRe: Performance Improvement/Cleanup Pin
matthias Weiser10-Dec-08 4:48
Membermatthias Weiser10-Dec-08 4:48 
GeneralIn the decompression to string the buffer size is wrong. A decompressed string may be larger than the original data. Pin
elinder91225-Sep-08 11:59
Memberelinder91225-Sep-08 11:59 
GeneralFlush doesn't flush Pin
Richard Birkby8-Sep-08 4:16
MemberRichard Birkby8-Sep-08 4:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.