Upload Unzip Extract Zip Files In Asp.Net
This post explains how to Upload And Unzip Or Extract Zip File In asp.net to a directory on server then display extracted files in Gridview using C# VB.NET.
I'm using DotNetZip library for extracting zip archives, you need to put Ionic.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 CODE
Build and run the application
I'm using DotNetZip library for extracting zip archives, you need to put Ionic.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
01
using
System;
02
using
System.IO;
03
using
Ionic.Zip;
04
protected
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(
"~/www.csharpaspnetarticles.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 CODE
01
Protected
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(
"~/www.csharpaspnetarticles.com/"
), ExtractExistingFileAction.DoNotOverwrite)
09
gridviewExtracted.DataSource = fileToExtract.Entries
10
gridviewExtracted.DataBind()
11
lblMessage.Text =
"Archive extracted successfully and containes following files"
12
End
If
13
End
Sub
Hi Al-Salman Rahman
ReplyDeletehow the size of www.csharpaspnetarticles.com.xls has reduced from 13824 to 1160.
I'm using DotNetZip library for extracting zip archives.
DeleteDowanload from link http://dotnetzip.codeplex.com/releases/68268/download/258014
Deleteyou need to put Ionic.Zip.dll in Bin folder of your application.