Your fly down? Zip it up natively.

Published 11 August 08 11:45 PM | Coding4Fun 

zipper Jaime Olivares got sick and tired of having to use the java.util.zip namespace or invoking shell API’s to produce a Zip file.  Jaime decided to create a pure .Net 3.0 / 3.5 class that can create / append to zip file.

Here is a quick example on how he zips a file.

ZipStorer zip;

if (this.RadioCreate.Checked)
    // Creates a new zip file
    zip = ZipStorer.Create(TextStorage.Text, "Generated by ZipStorer class");
else
    // Creates a new zip file
    zip = ZipStorer.Open(TextStorage.Text, FileAccess.Write);

// Stores all the files into the zip file
foreach (string path in listBox1.Items)
{
    zip.AddFile(path, Path.GetFileName(path), "");
}

// Creates a memory stream with text
MemoryStream readme = new MemoryStream(
System.Text.Encoding.UTF8.GetBytes(string.Format("{0}\r\nThis file
    has been {1} using the ZipStorer class, by Jaime Olivares.",
DateTime.Now, this.RadioCreate.Checked ? "created" : "appended")));

// Stores a new file directly from the stream
zip.AddStream("readme.txt", readme, DateTime.Now, "Please read");
readme.Close();

// Updates and closes the zip file
zip.Close();

Another awesome thing is you can use this class with the .Net Compact Framework.

Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Your fly down? Zip it up natively. | Easycoded said on August 12, 2008 2:50 AM:

PingBack from http://www.easycoded.com/your-fly-down-zip-it-up-natively

# DotNetInterop said on August 12, 2008 11:11 AM:

See also

http://www.codeplex.com/DotNetZip , which actually does compression.  

# szurgot said on August 12, 2008 2:52 PM:

http://sharpdevelop.net/OpenSource/SharpZipLib/Default.aspx

Has worked pretty well for all my uses.

# Jaime Olivares said on July 30, 2009 11:22 PM:

Now ZIPSTORER can compress files, while keeping its minimalistic approach: zipstorer.codeplex.com

# Jaime Olivares said on August 2, 2009 8:44 PM:

Hi all,

I have just released a new version of ZipStorer with Deflate support at: zipstorer.codeplex.com

Just a little change needed:

   zip.AddFile(path, Path.GetFileName(path), "");

shall be:

   zip.AddFile(ZipStorer.Compression.Deflate, path, Path.GetFileName(path), "");

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

Search

This Blog

Syndication

Page view tracker