Click here to Skip to main content
13,597,195 members
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 22 Feb 2006
Licenced CPOL

Massive unzip application

Rate this:
Please Sign up or sign in to vote.
An application to unzip multiple Zip files residing in a folder and subfolders. Demonstrates the use of the BackgroundWorker component.

Massive unzip application image


This small application recursively extracts multiple zip files from a source folder and its subfolders to a destination folder. The source and destination folders can be the same. After the extraction, the user is asked to delete the zip files. Beyond its utility as an application, the code demonstrates the BackgroundWorker component, introduced in .NET framework v.2.


For the unzip functionality, I used the SharpZipLib library freely available from the Sharpdevelop site. To understand the code, you have to read the documentation of the BackgroundWorker component. The BackgroundWorker component gives you the ability to execute time-consuming operations asynchronously ("in the background"), on a thread different from your application's main UI thread. Programming with the BackgroundWorker does not involve thread programming, you simple have to fill code in the appropriate event handlers. I used the design pattern of the component to keep the UI responsive while the files are unzipped in the background. The user also has the chance to cancel the operation.

The code

Functionality is implemented in the ZipUtils class file and specifically in the ExtractAllZips sub.

Sub ExtractAllZips()
    Dim d1, d2 As IO.DirectoryInfo
    d1 = New IO.DirectoryInfo(_sourceDir)
    d2 = New IO.DirectoryInfo(_targetDir)
    _iUnzip = 0
    extrAllZipsRecursive(d1, d2)
End Sub

Dim _iUnzip As Integer
Private Sub extrAllZipsRecursive(ByVal d1 As IO.DirectoryInfo, _
                                 ByVal d2 As IO.DirectoryInfo)
    Dim fi As IO.FileInfo, subd1, subd2 As IO.DirectoryInfo
    For Each fi In d1.GetFiles()
      If _worker.CancellationPending Then
        _dweArgs.Cancel = True
        Exit Sub
      End If
      If fi.Extension = ".zip" Then
        'increase decompress counter
        _iUnzip += 1
        Dim pctProg As Integer = (_iUnzip * 100) / ZipList.Count
        Worker.ReportProgress(pctProg, fi.FullName)
        ' extract zip or sleep for a while
        If Not _testMode Then
          ExtractZip(fi.FullName, d2.FullName)
        Else : System.Threading.Thread.Sleep(50)
        End If
      End If
    For Each subd1 In d1.GetDirectories()
      subd2 = d2.CreateSubdirectory(subd1.Name)
      extrAllZipsRecursive(subd1, subd2)
End Sub
Sub ExtractZip(ByVal zipFilename As String, ByVal targetDir As String)
      fz.ExtractZip(zipFilename, targetDir, FastZip.Overwrite.Always, Nothing, "", "")
    Catch ex As Exception
    End Try
End Sub

The ExtractAllZips sub is called from the handler of the BackgroundWorker's DoWork event, to be executed in the background.

Private Sub bkgWork1_DoWork(ByVal sender As Object, _
        ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bkgWork1.DoWork
    ' Get the BackgroundWorker object that raised this event.
    Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
    ' get the zip utils that will do the work
    Dim zu As ZipUtils = CType(e.Argument, ZipUtils)
    zu.Worker = worker
    zu.DweArgs = e
End Sub

The process is initiated in the button's handler that calls the RunWorkerAsync method. Cancelling can be done by calling the CancelAsync method.

' unzip button, asynchronously
Private Sub btnUnzip_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnUnzip.Click
    _zipUtils.SourceDir = TextBox1.Text
    _zipUtils.TargetDir = TextBox2.Text
    ' prepare interface
    If _bWorking Then
      _bWorking = True
    End If
End Sub


I enjoyed creating this small program, I hope you'll find it useful!


  • Version 1.0.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Philipos Sakellaropoulos
Web Developer Forthnet
Greece Greece
Software developer and Microsoft Trainer, Athens, Greece (MCT,, MCSE 2003, MCDBA 2000,MCTS, MCITP, MCIPD).

You may also be interested in...


Comments and Discussions

GeneralSimpler code available here Pin
Cheeso9-Mar-09 4:00
memberCheeso9-Mar-09 4:00 
GeneralUnable to download the source code Pin
Fresh Mexican Food Fan12-Dec-07 10:23
memberFresh Mexican Food Fan12-Dec-07 10:23 
GeneralMissing Argument Pin
MatrixCoder18-Nov-06 18:19
memberMatrixCoder18-Nov-06 18:19 
GeneralRe: Missing Argument Pin
azyet2430-Aug-07 16:13
memberazyet2430-Aug-07 16:13 
GeneralNumber of File Extraction Problem Pin
karthik_887-Mar-06 16:56
memberkarthik_887-Mar-06 16:56 
GeneralRe: Number of File Extraction Problem Pin
Philipos Sakellaropoulos15-Mar-06 1:46
memberPhilipos Sakellaropoulos15-Mar-06 1:46 
GeneralRe: Number of File Extraction Problem Pin
karthik_8815-Mar-06 22:21
memberkarthik_8815-Mar-06 22:21 
Thank you for your reply. The problem is actually caused by an empty shortcut file named spline.mat, and this file could not be overwrite. So I have modified the code to delete the file before archiving and extracting. Thanks.

QuestionWork with .rar files? Pin
bleeding.american28-Feb-06 22:32
memberbleeding.american28-Feb-06 22:32 
QuestionFastZip? Pin
torial23-Feb-06 8:32
membertorial23-Feb-06 8:32 
AnswerRe: FastZip? Pin
Philipos Sakellaropoulos23-Feb-06 22:55
memberPhilipos Sakellaropoulos23-Feb-06 22:55 
GeneralRe: FastZip? Pin
jpazgier24-Feb-06 6:23
memberjpazgier24-Feb-06 6:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.180621.3 | Last Updated 22 Feb 2006
Article Copyright 2006 by Philipos Sakellaropoulos
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid