Showing posts with label DotNetZip. Show all posts
Showing posts with label DotNetZip. Show all posts
1

Download Multiple Files As Zip File Archive In Asp.Net

This Example Explains How To Download Multiple Files As Zip File Archive From Server In Asp.Net.

I have placed one CheckBoxList on asp.net page to Display Files From Server And One button to Download Zip File Archives.

I have used DotNetZip Library to Create Zip File from selected file(s), Add Ionic.Zip.dll in Bin folder in application root.
Download Multiple files As Zip FIle In Asp.Net

HTML SOURCE OF PAGE
   1:  <form id="form1" runat="server">
   2:  <div>
   3:  <asp:CheckBoxList ID="checkBoxList" runat="server"/>
   4:   
   5:  <asp:Button ID="btnDownload" runat="server" 
   6:              onclick="btnDownload_Click" 
   7:              Text="Download Files" />
   8:  </div>
   9:  </form>

C# CODE
using System;
using System.Web.UI.WebControls;
using System.IO;
using Ionic.Zip;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DirectoryInfo directory = new DirectoryInfo(Server.MapPath("~/csharpdotnetfreak.blogspot.com"));
            FileInfo[] filesInFolder = directory.GetFiles("*.*", SearchOption.AllDirectories);
            foreach (FileInfo fileInfo in filesInFolder)
            {
                checkBoxList.Items.Add(fileInfo.Name);
            }
        }

    }
    protected void btnDownload_Click(object sender, EventArgs e)
    {
        ZipFile multipleFiles = new ZipFile();
        Response.AddHeader("Content-Disposition", "attachment; filename=DownloadedFile.zip");
        Response.ContentType = "application/zip";
        foreach (ListItem fileName in checkBoxList.Items)
        {
            if (fileName.Selected)
            {
                string filePath = Server.MapPath("~/csharpdotnetfreak.blogspot.com" + fileName.Value);
                multipleFiles.AddFile(filePath,string.Empty);
            }
        }
        multipleFiles.Save(Response.OutputStream);
    }
}

VB.NET
Imports System.Web.UI.WebControls
Imports System.IO
Imports Ionic.Zip

Public Partial Class _Default
 Inherits System.Web.UI.Page
 Protected Sub Page_Load(sender As Object, e As EventArgs)
  If Not IsPostBack Then
   Dim directory As New DirectoryInfo(Server.MapPath("~/csharpdotnetfreak.blogspot.com"))
   Dim filesInFolder As FileInfo() = directory.GetFiles("*.*", SearchOption.AllDirectories)
   For Each fileInfo As FileInfo In filesInFolder
    checkBoxList.Items.Add(fileInfo.Name)
   Next
  End If

 End Sub
 Protected Sub btnDownload_Click(sender As Object, e As EventArgs)
  Dim multipleFiles As New ZipFile()
  Response.AddHeader("Content-Disposition", "attachment; filename=DownloadedFile.zip")
  Response.ContentType = "application/zip"
  For Each fileName As ListItem In checkBoxList.Items
   If fileName.Selected Then
    Dim filePath As String = Server.MapPath("~/csharpdotnetfreak.blogspot.com" & fileName.Value)
    multipleFiles.AddFile(filePath, String.Empty)
   End If
  Next
  multipleFiles.Save(Response.OutputStream)
 End Sub
End Class


Download Sample Code


1

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

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 Sub
Build and run the application

Download Sample Code


1

Create Zip Files Archive In Asp.Net C# VB

This example explains how to Create Zip Files In Asp.Net Using C# And VB.Net. Several times while developing Asp.Net applications we need to upload files to server and Create Zip File or Archive from those uploaded files.

We can easily Create Zip Files Archives using DotNetZip

Create a Bin Folder in solution explorer and put Ionic.Zip.dll in it.

Place one FileUpload control on aspx page and one button to upload files and create zip file in it's Click Event.

We can Delete Files From Server Or Directory After creating Zip File by writing code after calling Save() method of zipfile object,and Display Files In GridView From Server.

Create Zip File In Asp.Net 2.0,3.5,4.0


HTML SOURCE OF PAGE
   1:  <asp:FileUpload ID="fileUpload1" runat="server" />
   2:  <asp:Button ID="btnUpload" runat="server" 
   3:              onclick="btnUpload_Click" 
   4:              Text="Upload Files"/>


C# CODE
protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (fileUpload1.HasFile)
        {
            string fileName = Path.GetFileName(fileUpload1.PostedFile.FileName);
            string fileLocation = Server.MapPath("~/UploadedFiles/" + fileName);
            fileUpload1.SaveAs(fileLocation);

            ZipFile createZipFile = new ZipFile();
            createZipFile.AddFile(fileLocation, string.Empty);
            createZipFile.Save(Server.MapPath("~/CsharpAspNetArticles/CsharpAspNetArticles.zip"));
        }  
    }
VB.NET
Protected Sub btnUpload_Click(sender As Object, e As EventArgs)
 If fileUpload1.HasFile Then
  Dim fileName As String = Path.GetFileName(fileUpload1.PostedFile.FileName)
  Dim fileLocation As String = Server.MapPath("~/UploadedFiles/" & fileName)
  fileUpload1.SaveAs(fileLocation)

  Dim createZipFile As New ZipFile()
  createZipFile.AddFile(fileLocation, String.Empty)
  createZipFile.Save(Server.MapPath("~/CsharpAspNetArticles/CsharpAspNetArticles.zip"))
 End If
End Sub


Download Sample Code


Find More Articles