BrainDump
  Home arrow BrainDump arrow Page 2 - Working with Zip and ExcelXmlWriter
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? 
BRAINDUMP

Working with Zip and ExcelXmlWriter
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2007-08-29

    Table of Contents:
  • Working with Zip and ExcelXmlWriter
  • Using SharpZipLib
  • 4.11 Generating Excel Files from Code Using ExcelXmlWriter (Without Having Excel!)
  • Using ExcelXmlWriter

  • 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


    Working with Zip and ExcelXmlWriter - Using SharpZipLib


    (Page 2 of 4 )

    SharpZipLib’s GZip and Zip compression routines use the same basic process: create a stream for the zip type, write some bytes to it, and then close it. An example for GZip’s simplest incarnation is shown in Example 4-4.

    Example 4-4. Using GZip with a single file

    private void SingleGZip(string source)
    {
       string target = source + ".gz";

       using (Stream s = new GZipOutputStream(File.Create(target)))
      
    {
          using (FileStream fs = File.OpenRead(source))
          {
            
    byte[] buffer = new byte[fs.Length];
             fs.Read(buffer, 0, (int) fs.Length);
             s.Write(buffer, 0, buffer.Length);
          }
       }
    }

    Example 4-5 shows the simplicity of extracting a file from a GZip archive.

    Example 4-5. Extracting a GZip file

    using (Stream input = new GZipInputStream(File.OpenRead(source)))
    {
        using (FileStream output =
                File.Create(Path.GetFileNameWithoutExtension(source)))
        {
          int buffSize = 2048; 
          
    byte[] outBuffer = new byte[2048]; 
          while (true)
          { 
             
    buffSize = input.Read(outBuffer, 0, buffSize);       
            
     if (buffSize > 0)
             {
                output.Write(outBuffer, 0, buffSize);
             }
             else
             { 
               
    break;
             }
          }
       }
    }

    Working with BZip2 compression is even easier. Static methods give quick access to compression and decompression functionality. Compressing a file is a simple matter, as shown in Example 4-6.

    Example 4-6. Using BZip2 with a single file

    private void BZip2Compress(string source)
    {
       string target = source + ".bz2";
       int blockSize = 4096;
       BZip2.Compress(File.OpenRead(source), File.Create(target), blockSize);
    }

    Decompressing likewise involves just a quick call to a static method, as you can see in Example 4-7.

    Example 4-7. Extracting a BZip2 file

    private void BZip2Decompress(string source) {
       BZip2.Decompress(File.OpenRead(source),
               File.Create(Path.GetFileNameWithoutExtension(source)));
    }

    Zip compression works much the same; however,ZipOutputStreamobjects can make use of theZipEntryclass.ZipEntryobjects work nicely when you want to pass in a list of files to add to an archive (say, a list of files a user has selected from a menu). Example 4-8 shows how to archive a list of files.

    Example 4-8. Zipping multiple files

    private void ZipMultipleCompress(string target, string[] fileNames,
            int compressionLevel)
    {
        using (ZipOutputStream zipOutStream = new ZipOutputStream(File.Create(target)))
        {
          zipOutStream.SetLevel(compressionLevel);

           foreach (string file in fileNames)
           {
              using (FileStream inStream = File.OpenRead(file))
              {
                
    byte[] buffer = new byte[inStream.Length];
                 inStream.Read(buffer, 0, buffer.Length);

                 ZipEntry entry = new ZipEntry(file);
                 zipOutStream.PutNextEntry(entry);
                 zipOutStream.Write(buffer, 0, buffer.Length);
              }

           }
        }
    }

    Another handy feature of theZipOutputStreamenables you to add checksums via SharpZipLib’sCrc32class. Create a newCrc32object:

      Crc32 crc = new Crc32();

    and then make use of it for eachZipEntryitem in your list:

      // clear previous CRC, compute new one, add to entry
      crc.Reset();
      crc.Update(buffer); 
      entry.Crc = crc.Value;

    There’s also a handyFastZip class that offers quick creation and extraction of Zip files. It has a few restrictions, though:FastZipcan’t use absolute file paths, nor is it appropriate for collections of files—it’s better used for files within one directory tree.

    Tar archives are fully supported in SharpZipLib as well. The usage pattern is very similar to the Zip method shown in Example 4-8. You’ll have to deal with creating output streams for the different compression types (BZip2, GZip) yourself, but this is straightforward and is well documented in the distribution’s examples.

    Getting Support

    SharpZipLib has an active community in forums at the library’s home page, where most issues you run into can be resolved quickly.

    SharpZipLib in a Nutshell

    SharpZipLib gives developers great flexibility and power in creating archives in standard, well-known formats.

    More BrainDump Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Windows Developer Power Tools," published...
     

    Buy this book now. This article is excerpted from chapter four of the book Windows Developer Power Tools, written by James Avery and Jim Holmes (O'Reilly; ISBN: 0596527543). Check it out today at your favorite bookstore. Buy this book now.

    BRAINDUMP ARTICLES

    - Internet Explorer 8 Review
    - Nilpo`s Top Windows Add-Ons
    - Beginning Silverlight 2.0 Development using ...
    - Fixing Vista`s Troubles
    - Preparing Windows Images for Mass Deployment
    - The Trouble With Vista
    - Slipstreamed and Unattended Windows Installa...
    - Microsoft Office SharePoint Server
    - Microsoft Office SharePoint Designer
    - Microsoft Windows SharePoint Services 3.0
    - Microsoft Live Mesh Overview
    - XAML Brushes and Silverlight
    - Silverlight and XAML Basics
    - Immortal XP
    - XAML Basics





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