473,321 Members | 1,778 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,321 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 37269
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.