Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Massive unzip application

, 22 Feb 2006 CPOL
An application to unzip multiple Zip files residing in a folder and subfolders. Demonstrates the use of the BackgroundWorker component.
Imports ICSharpCode.SharpZipLib.Zip
Imports System.Collections.Generic
Imports System.ComponentModel

' simple wrapper class for Unzipping by Philip
Public Class ZipUtils
  Private Shared fz As New FastZip

  Private _sourceDir As String
  Public Property SourceDir() As String
    Get
      Return _sourceDir
    End Get
    Set(ByVal value As String)
      _sourceDir = value
    End Set
  End Property

  Private _targetDir As String
  Public Property TargetDir() As String
    Get
      Return _targetDir
    End Get
    Set(ByVal value As String)
      _targetDir = value
    End Set
  End Property

  Private _testMode As Boolean
  Public Property TestMode() As Boolean
    Get
      Return _testMode
    End Get
    Set(ByVal value As Boolean)
      _testMode = value
    End Set
  End Property

  Private _worker As BackgroundWorker
  Public Property Worker() As BackgroundWorker
    Get
      Return _worker
    End Get
    Set(ByVal value As BackgroundWorker)
      _worker = value
    End Set
  End Property

  Private _dweArgs As DoWorkEventArgs
  Public Property DweArgs() As DoWorkEventArgs
    Get
      Return _dweArgs
    End Get
    Set(ByVal value As DoWorkEventArgs)
      _dweArgs = value
    End Set
  End Property

  Sub ExtractZip(ByVal zipFilename As String, ByVal targetDir As String)
    Try
      fz.ExtractZip(zipFilename, targetDir, FastZip.Overwrite.Always, Nothing, "", "")
    Catch ex As Exception
      Debug.WriteLine(ex.Message)
    End Try
  End 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
    Next
    For Each subd1 In d1.GetDirectories()
      subd2 = d2.CreateSubdirectory(subd1.Name)
      extrAllZipsRecursive(subd1, subd2)
    Next
  End Sub
  ' delete alll zips in directory
  Sub DeleteAllZipsInDir()
    Dim d1 As IO.DirectoryInfo
    d1 = New IO.DirectoryInfo(_sourceDir)
    delAllZipsRec(d1)
  End Sub
  Private Sub delAllZipsRec(ByVal d1 As IO.DirectoryInfo)
    Dim fi As IO.FileInfo, subd1 As IO.DirectoryInfo
    For Each fi In d1.GetFiles()
      If fi.Extension = ".zip" Then
        If Not _testMode Then fi.Delete()
      End If
    Next
    For Each subd1 In d1.GetDirectories()
      delAllZipsRec(subd1)
    Next
  End Sub
  ' find all zips in directory
  Dim _zipList As New List(Of String)
  Public ReadOnly Property ZipList() As List(Of String)
    Get
      Return _zipList
    End Get
  End Property

  Sub FindAllZipsInDir()
    Dim d1 As IO.DirectoryInfo
    d1 = New IO.DirectoryInfo(_sourceDir)
    _zipList.Clear()
    findAllZipsRec(d1)
  End Sub
  Private Sub findAllZipsRec(ByVal d1 As IO.DirectoryInfo)
    Dim fi As IO.FileInfo, subd1 As IO.DirectoryInfo
    For Each fi In d1.GetFiles()
      If fi.Extension = ".zip" Then
        _zipList.Add(fi.FullName)
      End If
    Next
    For Each subd1 In d1.GetDirectories()
      findAllZipsRec(subd1)
    Next
  End Sub
End Class

'MFC-like wait cursor!
Class WaitCursor
  Implements IDisposable

  Private disposedValue As Boolean = False    ' To detect redundant calls

  ' IDisposable
  Protected Overridable Sub Dispose(ByVal disposing As Boolean)
    If Not Me.disposedValue Then
      If disposing Then
        ' TODO: free unmanaged resources when explicitly called
      End If

      ' TODO: free shared unmanaged resources
      _frm.Cursor = Cursors.Default
    End If
    Me.disposedValue = True
  End Sub

#Region " IDisposable Support "
  ' This code added by Visual Basic to correctly implement the disposable pattern.
  Public Sub Dispose() Implements IDisposable.Dispose
    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    Dispose(True)
    GC.SuppressFinalize(Me)
  End Sub
#End Region

  Dim _frm As Form
  Public Sub New(ByVal frm As Form)
    _frm = frm
    _frm.Cursor = Cursors.WaitCursor
  End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Philipos Sakellaropoulos
Web Developer
Greece Greece
Software developer and Microsoft Trainer, Athens, Greece (MCT, MCSD.net, MCSE 2003, MCDBA 2000,MCTS, MCITP, MCIPD).
Follow on   Twitter   Google+

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 22 Feb 2006
Article Copyright 2006 by Philipos Sakellaropoulos
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid