C# – How to create a zip file using encoded string in C#

c++gzipstream

I'm new to C# and using C#.Net 2.0 with Visual Studio 2005.

How can I create a zip file from a string using GZipStream. (I don't want to use any third party libraries and doing this purely using C#.)

FYI: Scenario is this. Already there is a zip file in a folder. I need to encode this zip file stream in Base64 and again zip the Base 64 encoded string. (Creating a new zip file from Base64 encoded original zip file).

Appreciate your help.

Thanks,

Chatura

Best Solution

Thanks Bueller for the reply. But without using any external libraries I could do it. Here is the code snippet. This is not the final code with all try/ catch etc. May be useful for others.

        private static void CreateZipFromText(string text)
    {            
        byte[] byteArray = ASCIIEncoding.ASCII.GetBytes(text);
        string encodedText = Convert.ToBase64String(byteArray);

        FileStream destFile = File.Create("C:\\temp\\testCreated.zip");

        byte[] buffer = Encoding.UTF8.GetBytes(encodedText);
        MemoryStream memoryStream = new MemoryStream();

        using (System.IO.Compression.GZipStream gZipStream = new System.IO.Compression.GZipStream(destFile, System.IO.Compression.CompressionMode.Compress, true))
        {
            gZipStream.Write(buffer, 0, buffer.Length);
        }
    }