2

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

0

2 Answers 2

3

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);
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

I don't think this code creates a valid zip file (which isn't a gz file in disguise).
1

I am assuming you actually mean using the .zip archive format versus another format such as .gz. The .Net 2.0 compression libraries do not out of the box support zip format archives. The GZipStream and such compress to and from gzip format. There is an article here that shows how to support zip in C# without external libraries but I have not tried or tested it. You can find similar articles around but you have to dig to find the pure C# .NET no external library articles. http://www.codeproject.com/KB/recipes/ZipStorer.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.