Krunal Mevada

Because there’s always more to learn

Extract Zip File In ASP.NET

Summary
Extract your zip file in aspspider.net using OpenSource Zip Library in asp.net (SharpZipLib)

.NET Classes used :

  • using System;
  • using System.IO;
  • using ICSharpCode.SharpZipLib.Zip;

  • I have used open source zip library. Which you have to download from http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx

    Put ICSharpCode.SharpZipLib.DLL into your bin folder of asp.net application.

    Use Following function to extract zip file. Assign your zipfilename in ZipFileName variable.

    You Need Folder Rights to Create File Programmatically.

    string ZipFileName = "FCKeditor_2.4.3.zip";

           try
           {

               if (!File.Exists(Server.MapPath(ZipFileName)))
               {
                   lstProcess.Items.Add("File Does Not Exists.");
                   return;
               }

               using (ZipInputStream s = new ZipInputStream(File.OpenRead(Server.MapPath(ZipFileName))))
               {

                   ZipEntry theEntry;
                   while ((theEntry = s.GetNextEntry()) != null)
                   {
                      string directoryName = Path.GetDirectoryName(theEntry.Name);
                       string fileName = Path.GetFileName(theEntry.Name);

                       // create directory
                       if (directoryName.Length > 0)
                       {
                           Directory.CreateDirectory(Server.MapPath(directoryName));
                       }

                       if (fileName != String.Empty)
                       {
                           using (FileStream streamWriter = File.Create(Server.MapPath(theEntry.Name)))
                           {

                               int size = 2048;
                               byte[] data = new byte[2048];
                               while (true)
                               {
                                   size = s.Read(data, 0, data.Length);
                                   if (size > 0)
                                   {
                                       streamWriter.Write(data, 0, size);
                                   }
                                   else
                                   {
                                       break;
                                   }
                               }
                           }
                       }
                   }
               }
           }
           catch (Exception ex)
           {
               //Display Error
           }
           finally
           {
               //Update Status
           }
    Enjoy!! 

    Posted: Oct 09 2008, 07:37 AM by krunalm | with 2 comment(s)
    Filed under: , ,

    Comments

    Herman said:

    How about using the FastZip class?

    I unzip entire archives with only two lines.

    # October 9, 2008 9:09 AM

    krunalm said:

    hi herman,

    yes, that's good one.

    i think this is what u talking about

    FastZip fastZip = new FastZip();

    bool recurse = true;

    fastZip.CreateZip("fileName.zip", @"C:\SourceDirectory", recurse, "");

    Krunal

    # October 10, 2008 1:48 AM