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

Massive unzip application

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

Public Class Form1
  Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

  Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call

  End Sub

  'Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        components.Dispose()
      End If
    End If
    MyBase.Dispose(disposing)
  End Sub

  'Required by the Windows Form Designer
  Private components As System.ComponentModel.IContainer

  'NOTE: The following procedure is required by the Windows Form Designer
  'It can be modified using the Windows Form Designer.  
  'Do not modify it using the code editor.
  Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
  Friend WithEvents btn1 As System.Windows.Forms.Button
  Friend WithEvents Label1 As System.Windows.Forms.Label
  Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
  Friend WithEvents Label2 As System.Windows.Forms.Label
  Friend WithEvents btn2 As System.Windows.Forms.Button
  Friend WithEvents btnUnzip As System.Windows.Forms.Button
  Friend WithEvents StatusStrip1 As System.Windows.Forms.StatusStrip
  Friend WithEvents tsPB1 As System.Windows.Forms.ToolStripProgressBar
  Friend WithEvents tsSB1 As System.Windows.Forms.ToolStripStatusLabel
  Friend WithEvents bkgWork1 As System.ComponentModel.BackgroundWorker
  Friend WithEvents fbd1 As System.Windows.Forms.FolderBrowserDialog
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
    Me.TextBox1 = New System.Windows.Forms.TextBox
    Me.btn1 = New System.Windows.Forms.Button
    Me.Label1 = New System.Windows.Forms.Label
    Me.TextBox2 = New System.Windows.Forms.TextBox
    Me.Label2 = New System.Windows.Forms.Label
    Me.btn2 = New System.Windows.Forms.Button
    Me.btnUnzip = New System.Windows.Forms.Button
    Me.fbd1 = New System.Windows.Forms.FolderBrowserDialog
    Me.StatusStrip1 = New System.Windows.Forms.StatusStrip
    Me.tsSB1 = New System.Windows.Forms.ToolStripStatusLabel
    Me.tsPB1 = New System.Windows.Forms.ToolStripProgressBar
    Me.bkgWork1 = New System.ComponentModel.BackgroundWorker
    Me.StatusStrip1.SuspendLayout()
    Me.SuspendLayout()
    '
    'TextBox1
    '
    Me.TextBox1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.TextBox1.Location = New System.Drawing.Point(8, 40)
    Me.TextBox1.Name = "TextBox1"
    Me.TextBox1.Size = New System.Drawing.Size(490, 20)
    Me.TextBox1.TabIndex = 0
    '
    'btn1
    '
    Me.btn1.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.btn1.Location = New System.Drawing.Point(504, 40)
    Me.btn1.Name = "btn1"
    Me.btn1.Size = New System.Drawing.Size(24, 23)
    Me.btn1.TabIndex = 2
    Me.btn1.Text = "..."
    '
    'Label1
    '
    Me.Label1.Location = New System.Drawing.Point(8, 21)
    Me.Label1.Name = "Label1"
    Me.Label1.Size = New System.Drawing.Size(200, 16)
    Me.Label1.TabIndex = 3
    Me.Label1.Text = "Source folder"
    '
    'TextBox2
    '
    Me.TextBox2.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.TextBox2.Location = New System.Drawing.Point(8, 104)
    Me.TextBox2.Name = "TextBox2"
    Me.TextBox2.Size = New System.Drawing.Size(490, 20)
    Me.TextBox2.TabIndex = 0
    '
    'Label2
    '
    Me.Label2.Location = New System.Drawing.Point(8, 85)
    Me.Label2.Name = "Label2"
    Me.Label2.Size = New System.Drawing.Size(200, 16)
    Me.Label2.TabIndex = 3
    Me.Label2.Text = "Destination folder"
    '
    'btn2
    '
    Me.btn2.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.btn2.Location = New System.Drawing.Point(504, 104)
    Me.btn2.Name = "btn2"
    Me.btn2.Size = New System.Drawing.Size(24, 23)
    Me.btn2.TabIndex = 2
    Me.btn2.Text = "..."
    '
    'btnUnzip
    '
    Me.btnUnzip.Location = New System.Drawing.Point(206, 145)
    Me.btnUnzip.Name = "btnUnzip"
    Me.btnUnzip.Size = New System.Drawing.Size(101, 23)
    Me.btnUnzip.TabIndex = 4
    Me.btnUnzip.Text = "Unzip"
    '
    'StatusStrip1
    '
    Me.StatusStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.tsSB1, Me.tsPB1})
    Me.StatusStrip1.Location = New System.Drawing.Point(0, 192)
    Me.StatusStrip1.Name = "StatusStrip1"
    Me.StatusStrip1.Size = New System.Drawing.Size(540, 22)
    Me.StatusStrip1.TabIndex = 5
    Me.StatusStrip1.Text = "StatusStrip1"
    '
    'tsSB1
    '
    Me.tsSB1.Name = "tsSB1"
    Me.tsSB1.Size = New System.Drawing.Size(525, 17)
    Me.tsSB1.Spring = True
    '
    'tsPB1
    '
    Me.tsPB1.Name = "tsPB1"
    Me.tsPB1.Size = New System.Drawing.Size(100, 16)
    Me.tsPB1.Step = 1
    Me.tsPB1.Visible = False
    '
    'bkgWork1
    '
    Me.bkgWork1.WorkerReportsProgress = True
    Me.bkgWork1.WorkerSupportsCancellation = True
    '
    'Form1
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(540, 214)
    Me.Controls.Add(Me.StatusStrip1)
    Me.Controls.Add(Me.btnUnzip)
    Me.Controls.Add(Me.Label1)
    Me.Controls.Add(Me.btn1)
    Me.Controls.Add(Me.TextBox1)
    Me.Controls.Add(Me.TextBox2)
    Me.Controls.Add(Me.Label2)
    Me.Controls.Add(Me.btn2)
    Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
    Me.Name = "Form1"
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
    Me.Text = "Philip Software - Recursively extract zip files"
    Me.StatusStrip1.ResumeLayout(False)
    Me.StatusStrip1.PerformLayout()
    Me.ResumeLayout(False)
    Me.PerformLayout()

  End Sub

#End Region

  Dim WithEvents _zipUtils As New ZipUtils
  Dim _wc As WaitCursor
  Dim _bWorking As Boolean

  Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    My.Settings.DirSource = TextBox1.Text
    My.Settings.DirTarget = TextBox2.Text
    My.Settings.Save()
  End Sub

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    TextBox1.Text = My.Settings.DirSource
    TextBox2.Text = My.Settings.DirTarget
    _zipUtils.TestMode = False
    tsPB1.Maximum = 100
    adjustInterface(False)
  End Sub

  Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
    If fbd1.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub
    TextBox1.Text = fbd1.SelectedPath
  End Sub

  Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
    If fbd1.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub
    TextBox2.Text = fbd1.SelectedPath
  End Sub

  ' 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
    adjustInterface(True)
    If _bWorking Then
      Me.bkgWork1.CancelAsync()
    Else
      Me.bkgWork1.RunWorkerAsync(_zipUtils)
      _bWorking = True
    End If
  End Sub
  Private Sub adjustInterface(ByVal bWorking As Boolean)
    If bWorking Then
      _wc = New WaitCursor(Me)
    Else
      If _wc IsNot Nothing Then _wc.Dispose() 'set default cursor
    End If
    tsPB1.Visible = bWorking
    If Not bWorking Then Me.tsSB1.Text = ""
    Me.btnUnzip.Text = IIf(Not bWorking, My.Resources.UnzipButton1_1, My.Resources.UnzipButton1_2)
  End Sub

  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
    zu.FindAllZipsInDir()
    zu.ExtractAllZips()
  End Sub

  Private Sub bkgWork1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bkgWork1.ProgressChanged
    tsSB1.Text = CStr(e.UserState)
    tsPB1.Value = e.ProgressPercentage
  End Sub

  Private Sub bkgWork1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bkgWork1.RunWorkerCompleted
    _bWorking = False
    If e.Cancelled Then
      MessageBox.Show("Work was cancelled by user", "Massive Unzip", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    ElseIf MessageBox.Show("Delete zips", "Delete zips", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
      _zipUtils.DeleteAllZipsInDir()
    End If
    adjustInterface(False)
  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 | Mobile
Web02 | 2.8.140926.1 | Last Updated 22 Feb 2006
Article Copyright 2006 by Philipos Sakellaropoulos
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid