Upload Unzip Extract Zip Files Archive In Asp.Net

This example explains how to Upload And Unzip Or Extract Zip Files Archive In Asp.Net to a Server Directory Folder then display extracted files in Gridview using C# VB.NET.

I'm using DotNetZip library for extracting zip archives, you need to putIonic.Zip.dll in BIN folder of your application.

Place one FileUpload control and Button on page to upload files, we will Upload and unzip in Click Event of Button using ExtractAll method of ZipFile object.

Place one GridView on page to Display files after extraction to a folder or directory on server.

You can Delete uploaded file from server once unzipped.

Upload and Extract Zip Files Archive on server in asp.net

   1:  <asp:FileUpload ID="fileUpload1" runat="server"/>
   2:  <asp:Button ID="btnExtract" runat="server" 
   3:              onclick="btnExtract_Click" 
   4:              Text="Upload Zip Files" />
   6:  <asp:Label ID="lblMessage" runat="server"/>
   8:  <asp:GridView ID="gridviewExtractedFiles" runat="server" 
   9:                AutoGenerateColumns="False">
  10:  <Columns>
  11:  <asp:BoundField DataField="FileName" 
  12:                  HeaderText="File Name"/>
  13:  <asp:BoundField DataField="UncompressedSize" 
  14:                  HeaderText="Size"/>
  15:  <asp:BoundField DataField="CompressedSize"   
  16:                  HeaderText="Compressed Size">
  17:  </asp:BoundField>
  18:  </Columns>
  19:  </asp:GridView>

using System;
using System.IO;
using Ionic.Zip;
protected void btnExtract_Click(object sender, EventArgs e)
        if (fileUpload1.HasFile)
            string uploadedFile = Path.GetFileName(fileUpload1.PostedFile.FileName);
            string location = Server.MapPath("~/ZipFiles/" + uploadedFile);
            ZipFile fileToExtract = ZipFile.Read(location);
            fileToExtract.ExtractAll(Server.MapPath("~/csharpdotnetfreak.blogspot.com"), ExtractExistingFileAction.DoNotOverwrite);
            gridviewExtracted.DataSource = fileToExtract.Entries;
            lblMessage.Text = "Archive extracted successfully and containes following files";

We can use ExtractSelectedEntries method instead of ExtractAll to extract specific files such as *.jpg

Protected Sub btnExtract_Click(sender As Object, e As EventArgs)
 If fileUpload1.HasFile Then
  Dim uploadedFile As String = Path.GetFileName(fileUpload1.PostedFile.FileName)
  Dim location As String = Server.MapPath("~/ZipFiles/" & uploadedFile)

  Dim fileToExtract As ZipFile = ZipFile.Read(location)
  fileToExtract.ExtractAll(Server.MapPath("~/csharpdotnetfreak.blogspot.com"), ExtractExistingFileAction.DoNotOverwrite)
  gridviewExtracted.DataSource = fileToExtract.Entries
  lblMessage.Text = "Archive extracted successfully and containes following files"
 End If
End Sub
Build and run the application

Download Sample Code

If you like this post than join us or share


Apex said...

Extracting files from ZIP archive is really useful, but is there any way to do this without using DotNetZip?

Find More Articles