.NET
  Home arrow .NET arrow Page 3 - 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 
Mobile Linux 
App Generation ROI 
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
     
     
    ADVERTISEMENT


    SmartZip Archive File Library – Creating and Using Archive Files - Opening a Zip File


    (Page 3 of 4 )

    Above, I presented the process required to write a zip archive to a file. Obviously, you might also want to open a zip file and manipulate its contents programmatically in some way. There are a couple of ways to do this. If you plan on actually extracting the file data, it is generally best to use the ZipInputStream class, as this allows you to step sequentially through a zip archive and extract each entry’s contents. If all you need to do is list the names and maybe some other metadata about each entry, the ZipFile class is best. If you want to actually extract data from a ZipFile class, you must get a stream from the ZipFile class for each entry you want to extract.

    First, I will address using the ZipFile class. You can create a Zip file by giving it either a FileStream, a regular Stream, or a string containing a file name. After opening the file into a ZipFile object, you can walk through the ZipEntry objects to get information on each individual file and get streams to uncompress each individual file. The code to do this looks like the following:

    Steam outFile = new Stream();
    ZipFile zip = new ZipFile(“test.zip”);
    foreach(ZipEntry file in zip)
    {
    outFile = zip.GetInputStream(file);
    }

    You can then take that stream and use it to write out to a file or do something else interesting with it. You can also not even get the input stream at all and simply just get the metadata for each entry.

    The other way to access a zip file is to open it into a ZipInputStream. This is simpler to walk through and extract each file. This method extracts each entry into a byte array. You can then take that byte array and pipe it into a file stream to write the file out to the hard drive.

    ZipInputStream zip = new ZipInputStream(File.OpenRead(“test.zip”));
    ZipEntry entry;
    while(entry = zip.GetNextEntry()))
    {
    byte[] data = new byte[2048];
    zip.read(data,0,data.length);
    FileStream fs = File.Create(entry.Name);
    fs.write(data,0,data.length);
    fs.close();
    }
    zip.close();

    The above code will open the zip file and read each entry and write the first 2048 bytes out to a file named correctly.

    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

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - 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





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT