.NET
  Home arrow .NET arrow SmartZip Archive File Library – Creating a...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

SmartZip Archive File Library – Creating and Using Archive Files
By: Michael Swanson
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    2005-02-28

    Table of Contents:
  • SmartZip Archive File Library – Creating and Using Archive Files
  • The ZipEntry Class
  • Opening a Zip File
  • Other Formats

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    Iron Speed
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    SmartZip Archive File Library – Creating and Using Archive Files


    (Page 1 of 4 )

    Have you ever wanted to work with zip or other archive file formats in the .NET Framework? There is a free library that allows you to do just that. Keep reading to find out more.

    Introduction

    The .NET Framework has no built-in way to work with Zip or other archive file formats. However, this is often a useful option to have. You may want to send compressed files over the Internet, or create a feature on a program to automatically archive certain groups of files (such as logs) for better organization and space management.

    Luckily, there is a free library which does this for us. Created by Mike Krueger, it is called the SmartZip library. It is open source and governed by the GPL. This library supports regular Zip format, as well as Gzip, Tar, and Bzip2. The code in this document will deal specifically with the regular Zip format; however, the class structures and methods for working with the other file formats are similar.

    Creating a Zip File

    In order to create a zip file, there are several steps you must perform. They are as follows:

    1. Create the ZipOutputStream object in the code.

    2. Set the compression ratio on the ZipOutputStream object. Now, for each file you want to add to the ZipOutputStream you must do the following:

      1. Open the file to add in a file stream.

      2. Create a byte buffer to store the file in memory

      3. Create a ZipEntry object to hold metadata for the file

      4. Use the write method to put the file into the output stream.

    3. After all the files you want to add have been added, you must call the finish and close methods to complete the file creation.

    Overall, these steps are relatively simple, and don’t contain any major surprises. The only area where care is required surrounds keeping the directory structure intact within a zip file, and much of this care is handled by a method provided by the ZipEntry class, called “CleanName.” I will now describe this process in detail, and give some code snippits to illustrate each step.

    First of all, we must create the ZipOutputStream object and set its compression ratio:

    ZipOutputStream outStream=new ZipOutputStream(File.Create(“temp.zip”);
    outStream.SetLevel(9);

    These statements create an output stream object that is attached to the file “temp.zip” in the local working directory. This also sets the compression ratio, in this case to the highest compression. The compression level ranges from 0, which performs no compression and basically just combines several files into one, up to 9, which performs the most compression available in the Zip file standard.

    After creating the ZipOutputStream object, you must begin adding files to the stream so they can be written. The following code does this for a single file:

    FileStream fs = new FileStream(filename);
    byte[] buffer = new byte[fs.Length];
    fs.Read(buffer,0,buffer.Length);

    This code opens the file and puts it into a byte buffer. The write method for adding files to the output stream only accepts a byte buffer, so this is necessary. Now, we must create the ZipEnty class to hold the metadata about this particular file entry in the ZipOutputStream and write the byte buffer to the stream.

    String entryName = ZipEntry.CleanName(filename);
    ZipEntry entry = new ZipEntry(entryName);
    outStream.PutNextEntry(entry);
    outStream.Write(buffer,0,buffer.Length);

    In this code block, we first create a string to hold the “clean” name of the file being added. This cleaned name is an important step because it strips away the parts of the .NET filename representation that don’t work with the Zip standard. For example, it cleans off the windows volume labels (such as “C:\”) or windows file share names (such as “\\server\dir”). You can add files to the Zip file and preserve their directory structure by using the complete relative filename from the current working directory.

    This process above is then repeated for each file you wish to add to the Zip archive. It is also possible to build a function to walk through each directory in directory structure and add each one of those files to a zip archive and maintain that directory structure.

    outStream.finish();
    outStream.close();

    The above lines of code are the last required after having finished everything you want to do with the zip file. The “finish” method is important because it writes out the final information for closing the zip file. If this method doesn’t get called, anything else that tries to access your zip file won’t be able to, as the file won’t be formed correctly. The close method closes the stream connection to the file and releases it for use by other processes.

    More .NET Articles
    More By Michael Swanson


       · Hello Michael, Very nice article man :)Thanks,Michael Youssef
       · Thanks for the praise, I appreciate it.Any further comments on the article,...
       · I'm telling you the truth ;-). The article is good. You know what, maybe we...
       · That sounds like a great idea. PM me on the forums or email me and we can talk...
       · FileStream fs2 = new FileStream("c:\\reportsjs1.txt",FileMode.Open); byte[]...
       · Hello michael,I am getting an error in this line of code Dim s As...
       · I got an error in this part...Dim entryName As String =...
     

    .NET ARTICLES

    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries
    - Introducing LINQ to SQL Designer using Visua...
    - Beginning LINQ to SQL Using Visual Studio 20...
    - Coding an AjaxPro.NET Based Search Engine fo...
    - Building an AjaxPro.NET Based Search Engine ...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway