Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse the DotNetZip library to compress and decompress files in Visual Basic .NET
DescriptionThis example shows how to use the DotNetZip library to compress and decompress files in Visual Basic .NET.
Keywordscompress, uncompress, decompress, zip, archive, extract
CategoriesFiles and Directories, Utilities
 

Normally I don't like to require third-party libraries. I don't know what tools you have loaded and don't want to assume you can load new tools without creating conflicts. If third-party tools get out of synch, it can also be hard to upgrade appropriately to get them all working again.

However, this tool seems worth adding to your toolkit. The .NET Framework comes with a System.IO.Compression.GZipStream class that lets you compress and uncompress files. Unfortunately it works only with GZip files (with a .gz extension) not regular .zip files. You can open these files with many other third-party tools but the Windows operating system doesn't let you browse them the way it can Zip files. (I don't see the logic in providing a way to make compressed files that the operating system can't read. Perhaps it's a licensing issue.)

The DotNetZip library, which is free, provides .NET managed classes that you can use from C# to compress and decompress true Zip files. This example only shows a couple of the library's features.

Enter a file name and the path you want it to have in the archive. (Use \ to place the file in the archive's root.) Enter the archive's name and click Add to Archive to add the file to the archive. You can then open the archive with Windows Explorer and see what's inside it.

Enter an Extract To Path and click Extract Archive to extract all of the files in the archive into the path.

The following code shows how the program adds the file to the archive. The code in the using block is all there is to actually adding the file to the archive.

 
' Add the file to the archive.
Private Sub btnAddToArchive_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnAddToArchive.Click
    Try
        Using zip As ZipFile = New _
            ZipFile(txtArchiveName.Text)
            ' Add the file to the Zip archive's root folder.
            zip.AddFile(txtFileName.Text, _
                txtPathInArchive.Text)

            ' Save the Zip file.
            zip.Save()
        End Using

        ' Display the file sizes.
        Dim old_fi As New FileInfo(txtFileName.Text)
        lblOriginalSize.Text = old_fi.Length.ToString("#,#")
        Dim new_fi As New FileInfo(txtArchiveName.Text)
        lblCompressedSize.Text = _
            new_fi.Length.ToString("#,#")
        MessageBox.Show("Done")
    Catch ex As Exception
        MessageBox.Show("Error adding file to archive.\n" + _
            ex.Message)
    End Try
End Sub
 
The following code shows how the program extracts the files from the archive. It simply loops through the archive's entries and calls their Extract methods.
 
' Extract the files from the archive.
Private Sub btnExtractArchive_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnExtractArchive.Click
    Try
        Using zip As ZipFile = _
            ZipFile.Read(txtArchiveName.Text)
            ' Loop through the archive's files.
            For Each zip_entry As ZipEntry In zip
                zip_entry.Extract(txtExtractTo.Text)
            Next zip_entry
        End Using

        MessageBox.Show("Done")
    Catch ex As Exception
        MessageBox.Show("Error extracting archive.\n" + _
            ex.Message)
    End Try
End Sub
 
The library has lots of other features such as AddProgress and ExtractProgress event handlers to let you know how a long operation is progressing.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated