473,395 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to unzip and extract file in vb.net

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
5 37304
Curtis Rutland
3,256 Expert 2GB
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
raids51
59
you can also set a reference to VJSLIB, it has built in classes for zipping and unzipping files
Feb 6 '09 #3
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
Cheeso
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
Cheeso,
Your example works with .rar files too ???
Sep 30 '10 #6

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

Similar topics

2
by: Yang | last post by:
hi guys. anyone can tell me how to unzip a file using php ? thank you
2
by: Aurelio | last post by:
I need to unzip a file from my c# Windows application. is it possible?. How can i do it?
1
by: Miki Peric | last post by:
Which .Net function I should use to extract file name from URL/path?
1
by: Jan | last post by:
Hi, Is there a way to unzip a file in a directory from C#. I need a program to do that since that program copies the file from a server to a local machine, and then has to verify the contents of...
3
by: Jan | last post by:
Hi, Is there a way to unzip a file from C# since I need to download a zip file from the server, unzip it and verify its contents, all through the same program. Thanks in advance, Jan
13
by: fniles | last post by:
In VB.NET 2003 how can you programmatically unzip a ZIP file ? Thank you.
1
by: kokilatejas | last post by:
Hi all Please kindly help me on this i need to unzip a folder and place the contents in another folder using vb as code . eg: c:\test as source c:\test\test1 as destination Thanks in...
7
by: moroccanplaya | last post by:
hi i have created a program which creates a file then lets u add other files on to it then it should be able to extract it back out in order. my program can only extract an archive with only one file...
0
by: maheen khan | last post by:
basically i extracted some attributes of file(name, file type, path etc) present in a directory and listed them in a c++ file, now i want to extract the file path only so that i can use it as an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.