Friday, April 19, 2013

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

HTML SOURCE OF PAGE
   1:  <asp:FileUpload ID="fileUpload1" runat="server"/>
   2:  <asp:Button ID="btnExtract" runat="server" 
   3:              onclick="btnExtract_Click" 
   4:              Text="Upload Zip Files" />
   5:      
   6:  <asp:Label ID="lblMessage" runat="server"/>
   7:   
   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>

C# CODE
01using System;
02using System.IO;
03using Ionic.Zip;
04protected void btnExtract_Click(object sender, EventArgs e)
05    {
06        if (fileUpload1.HasFile)
07        {
08            string uploadedFile = Path.GetFileName(fileUpload1.PostedFile.FileName);
09            string location = Server.MapPath("~/ZipFiles/" + uploadedFile);
10            fileUpload1.SaveAs(location);
11 
12            ZipFile fileToExtract = ZipFile.Read(location);
13            fileToExtract.ExtractAll(Server.MapPath("~/csharpdotnetfreak.blogspot.com"), ExtractExistingFileAction.DoNotOverwrite);
14            gridviewExtracted.DataSource = fileToExtract.Entries;
15            gridviewExtracted.DataBind();
16            lblMessage.Text = "Archive extracted successfully and containes following files";
17        }
18   }

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

VB.NET
01Protected Sub btnExtract_Click(sender As Object, e As EventArgs)
02 If fileUpload1.HasFile Then
03  Dim uploadedFile As String = Path.GetFileName(fileUpload1.PostedFile.FileName)
04  Dim location As String = Server.MapPath("~/ZipFiles/" & uploadedFile)
05  fileUpload1.SaveAs(location)
06 
07  Dim fileToExtract As ZipFile = ZipFile.Read(location)
08  fileToExtract.ExtractAll(Server.MapPath("~/csharpdotnetfreak.blogspot.com"), ExtractExistingFileAction.DoNotOverwrite)
09  gridviewExtracted.DataSource = fileToExtract.Entries
10  gridviewExtracted.DataBind()
11  lblMessage.Text = "Archive extracted successfully and containes following files"
12 End If
13End Sub

0 comments:

Post a Comment