How to create a zip file using encoded string in C#
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 开发者_StackOverflow社区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
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);
}
}
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
精彩评论