Zip a File Using vb.net

Sometimes in a web site you might need the user to upload a zipped file for space issues but the user can’t or doesn’t know how.  You might want to conserve space on the server if you have a lot of images.  This is also a good way to keep Google from indexing your images from their search.  In another article I will show you how to unzip or uncompress but today here is how you can zip an uploaded file. I found a similar article but it was in c#.

As always you need to create a new web application project in Visual Studio and name the project ‘UploadNZip’.  Then add a form and add a web reference to iconic.dll.  You can get the file from here.

Add the following code to the page.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:FileUpload ID="file2Upload" runat="server" />
<asp:Button ID="btnZipFile" Text="ZipFile" runat="server" OnClick="btnZipFile_Click" />
<asp:Label ID="lblStatus" runat="server" ForeColor="Red" />

    </div>
    </form>

In the code behind of this form add the following code.

 Protected Sub btnZipFile_Click(sender As Object, e As EventArgs) Handles btnZipFile.Click
        'Zip a File after uploading

        If file2Upload.HasFile Then
            'Store the directory for the Zipped File
            Dim CurrentPath As String = Server.MapPath("~/ZipFiles/")

            'Get the variables to save the file first to the location
            Dim curpath As String = CurrentPath & Path.GetFileName(file2Upload.PostedFile.FileName)
            file2Upload.SaveAs(curpath)

            Dim filenames As String() = Directory.GetFiles(CurrentPath)

            'As files to the zipped file.  This would be one file for this example but could be more
            Using zip As New Ionic.Zip.ZipFile()
                Dim [date] As String = DateTime.Now.ToString("d")
                [date] = [date].Replace("/", "")
                zip.AddFiles(filenames, [date])
                zip.Save(CurrentPath & [date] & ".zip")
                lblStatus.Text = "File Added Successfully"
            End Using
            If lblStatus.Text = "File Added Successfully" Then
                'Removes the non-zipped file from the folder
                File.Delete(curpath)
            End If
        End If
    End Sub

Note: See the comments in the code for better understanding

That’s it. It is very simple to implement and can save a lot of space depending on the files you are trying to compress. This library can also allow you to add password protection to the files you compress. A great way to store files that need protection. One scenario would be if someone paid for a document you can then allow them to get the file, enter the password and track how many times this is done.

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , | Posted under Internet, Programming/Coding | RSS 2.0

Author Spotlight

David Bauernschmidt

David Bauernschmidt

I live in the historical triangle of Virginia where I am married with two daughters. I have spent over 13 years working for a Fortune 500 company in the computer area. I started in VB 6.0 and by the time I ended my employment I was supervising a development team where we built many web applications. When my first daughter was born I wanted to spend more time with her so I left and became a programmer analyst for local government as well as launch my own company. Since then I have grown James River Webs into a profitable web design and application company helping small businesses create a big presence on the internet. As an employee I have created web application used by citizens and other companies. I enjoy fly fishing, and spending time with my family. I also enjoy learning new approaches and development tools when it comes to developing applications.

One Comment on Zip a File Using vb.net

  1. Thanks for this and other articles on VB.net. Very helpful. Keep them coming, please!

Leave a Reply

Your email address will not be published. Required fields are marked *


9 × 9 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>