Sunday, March 11, 2012

Extract zip files in VB.NET

Here is one way to extract zip files. The example is done in VB.NET using the Shell COM.

Declarations needed:

Imports System.IO
' Add COM reference "Microsoft Shell Controls And Automation"
Imports Shell32

Code snippet:

Dim ZipFile As String = "D:\temp\"
Dim ZipDir As String = "D:\temp\files\"
Dim Shell As Shell32.IShellDispatch2
Dim ShellFolder As Shell32.Folder

If File.Exists(ZipFile) Then
    Shell = CType(CreateObject("Shell.Application"), IShellDispatch2)

    If Not Directory.Exists(ZipDir) Then Directory.CreateDirectory(ZipDir)
    ShellFolder = Shell.NameSpace(ZipDir)
    If ShellFolder IsNot Nothing Then
    End If
End If

One downside with this approach is that a dialog box can show up while extracting the zip file.

Other ways to Zip and Unzip in C#, VB, any .NET language is using DotNetZip (recommended) or SharpZipLib as well as SevenZipSharp for 7z (7-zip). There is also the GZipStream Class that comes with .NET Framework but only works with .gz files. System.IO.Packaging.ZipPackage is available in .NET Framework 3 and newer but is not straightforward to use and not recommended.

Some of the latest blog posts

Subscribe to RSS headline updates from:
Powered by FeedBurner