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.
HTML SOURCE OF PAGE
C# CODE
We can use ExtractSelectedEntries method instead of ExtractAll to extract specific files such as *.jpg
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.
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
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); fileUpload1.SaveAs(location); ZipFile fileToExtract = ZipFile.Read(location); fileToExtract.ExtractAll(Server.MapPath("~/csharpdotnetfreak.blogspot.com"), ExtractExistingFileAction.DoNotOverwrite); gridviewExtracted.DataSource = fileToExtract.Entries; gridviewExtracted.DataBind(); lblMessage.Text = "Archive extracted successfully and containes following files"; } }
We can use ExtractSelectedEntries method instead of ExtractAll to extract specific files such as *.jpg
VB.NET
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) fileUpload1.SaveAs(location) Dim fileToExtract As ZipFile = ZipFile.Read(location) fileToExtract.ExtractAll(Server.MapPath("~/csharpdotnetfreak.blogspot.com"), ExtractExistingFileAction.DoNotOverwrite) gridviewExtracted.DataSource = fileToExtract.Entries gridviewExtracted.DataBind() lblMessage.Text = "Archive extracted successfully and containes following files" End If End SubBuild and run the application
If you like this post than join us or share
1 comments:
Extracting files from ZIP archive is really useful, but is there any way to do this without using DotNetZip?
Post a Comment