By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,219 Members | 1,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,219 IT Pros & Developers. It's quick & easy.

How to unzip and extract file in vb.net

P: 40
Hi,
i want to know how to unzip a zip file and extract the files to a particular location

please help me

thanks in advance
varinder
Jan 30 '09 #1
Share this Question
Share on Google+
5 Replies


Curtis Rutland
Expert 2.5K+
P: 3,256
OK, this isn't my code, I found it as an example, so I haven't tested it.

It uses a 3rd party library called SharpZipLib. You will need to download it. It can be registered as an assembly.

Here's some code:
Expand|Select|Wrap|Line Numbers
  1.     Public Sub ExtractArchive(ByVal zipFilename As String, ByVal ExtractDir As String)
  2.         Dim Redo As Integer = 1
  3.         Dim MyZipInputStream As ZipInputStream
  4.         Dim MyFileStream As FileStream
  5.         MyZipInputStream = New ZipInputStream(New FileStream(zipFilename, FileMode.Open, FileAccess.Read))
  6.         Dim MyZipEntry As ZipEntry = MyZipInputStream.GetNextEntry
  7.         Directory.CreateDirectory(ExtractDir)
  8.         While Not MyZipEntry Is Nothing
  9.             If (MyZipEntry.IsDirectory) Then
  10.                 Directory.CreateDirectory(ExtractDir & "\" & MyZipEntry.Name)
  11.             Else
  12.                 If Not Directory.Exists(ExtractDir & "\" & _
  13.                 Path.GetDirectoryName(MyZipEntry.Name)) Then
  14.                     Directory.CreateDirectory(ExtractDir & "\" & _
  15.                     Path.GetDirectoryName(MyZipEntry.Name))
  16.                 End If
  17.                 MyFileStream = New FileStream(ExtractDir & "\" & _
  18.                   MyZipEntry.Name, FileMode.OpenOrCreate, FileAccess.Write)
  19.                 Dim count As Integer
  20.                 Dim buffer(4096) As Byte
  21.                 count = MyZipInputStream.Read(buffer, 0, 4096)
  22.                 While count > 0
  23.                     MyFileStream.Write(buffer, 0, count)
  24.                     count = MyZipInputStream.Read(buffer, 0, 4096)
  25.                 End While
  26.                 MyFileStream.Close()
  27.             End If
  28.             Try
  29.                 MyZipEntry = MyZipInputStream.GetNextEntry
  30.             Catch ex As Exception
  31.                 MyZipEntry = Nothing
  32.             End Try
  33.         End While
  34.         If Not (MyZipInputStream Is Nothing) Then MyZipInputStream.Close()
  35.         If Not (MyFileStream Is Nothing) Then MyFileStream.Close()
  36.     End Sub
Jan 30 '09 #2

P: 59
you can also set a reference to VJSLIB, it has built in classes for zipping and unzipping files
Feb 6 '09 #3

P: 1
under XP you can use shell32 directly without 3rd Party software

you have to add the COM reference "Microsoft Shell Controls And Automation" into your Project

see following (handy) sample code:

Expand|Select|Wrap|Line Numbers
  1. Imports System.IO
  2. Imports Shell32
  3.  
  4. ---
  5.  
  6. Private Sub Button303_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button303.Click
  7.  
  8.         ' Unzip Variables
  9.         Dim UserShell As Shell32.IShellDispatch2
  10.         Dim ZipContent As Shell32.FolderItems
  11.         Dim ExportLocation As Shell32.Folder
  12.  
  13.         ' -----------------------------------------------
  14.         ' Unzip File  -> only XP ?!
  15.         ' -----------------------------------------------
  16.  
  17.         If File.Exists("c:\temp\zipfile.zip") = False Then
  18.             ' error mesage
  19.         End If
  20.  
  21.         UserShell = CType(CreateObject("Shell.Application"), IShellDispatch2)
  22.  
  23.         ' if you like, you can get some properties of your content
  24.         ' for ex. filenames?
  25.  
  26.         ZipContent = UserShell.NameSpace(("c:\temp\zipfile.zip")).Items
  27.         Dim i As Integer
  28.         Dim Filename As String
  29.         For i = 0 To ZipContent.Count - 1
  30.             Filename = ZipContent.Item(i).Name.ToString
  31.         Next
  32.  
  33.         ' unpack the thing 
  34.         ExportLocation = UserShell.NameSpace(("c:\temp\"))
  35.         If ExportLocation IsNot Nothing Then
  36.             ExportLocation.CopyHere(UserShell.NameSpace(("c:\temp\zipfile.zip")).Items)
  37.         End If
  38.     End Sub
  39.  
this is it...
Feb 25 '09 #4

P: 1
Here's some example code:

Expand|Select|Wrap|Line Numbers
  1.   Dim ZipToUnpack As String = "C1P3SML.zip"  
  2.    Dim TargetDir As String = "C1P3SML"  
  3.    Console.WriteLine("Extracting file {0} to {1}", ZipToUnpack, TargetDir)   
  4.    Using zip1 As ZipFile = ZipFile.Read(ZipToUnpack)   
  5.        AddHandler zip1.ExtractProgress, AddressOf MyExtractProgress   
  6.        Dim e As ZipEntry   
  7.        ' here, we extract every entry, but we could extract    
  8.        ' based on entry name, size, date, etc.   
  9.        For Each e In zip1   
  10.            e.Extract(TargetDir, ExtractExistingFileAction.OverwriteSilently)   
  11.        Next  
  12.    End Using  
  13.  
There are some nice options... Progress Events, ZIP passwords, renaming entries in Zip files, ZIP64, AES encryption, lots more. There's a good help file and forums, too.

Many more examples at: http://dotnetzip.codeplex.com/Wiki/V...le=VB-examples

Download DotNetZip at http://dotnetzip.codeplex.com
Free.
May 22 '09 #5

P: n/a
Cheeso,
Your example works with .rar files too ???
Sep 30 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.