This isn't a Visual Studio project. It's a single source file, that you can compile with the VBC.exe compiler. You need to invoke it with two arguments, the name of a zip file, and the name of a directory. It will unzip the zipfile into the specified directory, and display a progressbar while doing so.

'' qunzip.vb
'' ------------------------------------------------------------------
'' A simple app that unzips a file, showing a progress bar. 
'' It is both a console app, and a winforms app.
'' It correctly does the multi-threading to allow smooth UI update. 
'' compile it with:
''      c:\.net3.5\vbc.exe /t:exe /debug:full /optimize- /R:System.dll /R:System.Data.dll  /R:Ionic.Zip.dll
''                                /out:Qunzip.exe Qunzip.vb
'' built on host: DINOCH-2
'' Created Thu Aug 06 15:34:17 2009
'' last saved: 
'' Time-stamp: <2009-August-13 16:40:07>
'' ------------------------------------------------------------------
'' Copyright (c) 2009 by Dino Chiesa
'' All rights reserved!
'' Licensed under the Microsoft Public License.
'' see
'' ------------------------------------------------------------------

Imports System
Imports System.Reflection
Imports System.Windows.Forms
Imports Ionic.Zip

'' to allow fast ngen
<assembly: AssemblyTitle("Qunzip.cs")>
<assembly: AssemblyDescription("quick winforms unzip tool")>
<assembly: AssemblyConfiguration("")>
<assembly: AssemblyCompany("Dino Chiesa")>
<assembly: AssemblyProduct("Tools")>
<assembly: AssemblyCopyright("Copyright © Dino Chiesa 2009")>
<assembly: AssemblyTrademark("")>
<assembly: AssemblyCulture("")>
<assembly: AssemblyVersion("")>

Namespace Ionic.ToolsAndTests.VB

      Friend Class QuickUnzip
        ' Methods
        <System.Runtime.InteropServices.DllImport("kernel32.dll")> _
        Private Shared Function AllocConsole() As Boolean
        End Function

        <System.Runtime.InteropServices.DllImport("kernel32.dll")> _
        Private Shared Function AttachConsole(ByVal pid As Integer) As Boolean
        End Function

        <STAThread> _
        Public Shared Function Main(ByVal args As String()) As Integer
            If Not QuickUnzip.AttachConsole(-1) Then
            End If
            If (args.Length <> 2) Then
                Return QuickUnzip.Usage(args)
                Dim f As New QuickUnzipForm(args(0), args(1))
            End If
            Return 0 
        End Function

        Private Shared Function Usage(ByVal args As String()) as Integer
            Console.WriteLine("QuickUnzip.  Usage:  QuickUnzip <zipfile> <extractDirectory>")
            Return 1
        End Function

    End Class

    Public Class QuickUnzipForm
        Inherits Form
        ' Methods
        Public Sub New()
            Me.components = Nothing
        End Sub

        Public Sub New(ByVal zipfile As String, ByVal directory As String)
            Me.zipfileName = zipfile
            Me.extractDirectory = directory
        End Sub

        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If (disposing AndAlso (Not Me.components Is Nothing)) Then
            End If
        End Sub

        Private Sub FixTitle()
            Me.Text = String.Format("Quick Unzip {0}", Me.zipfileName)
        End Sub

        Private Sub InitializeComponent()
            Me.components = New System.ComponentModel.Container
            Me.label1 = New Label
            Me.progressBar1 = New ProgressBar
            Me.label1.AutoSize = True
            Me.label1.Location = New System.Drawing.Point(12, 12)
            Me.label1.Name = "label1"
            Me.label1.Size = New System.Drawing.Size(50, 13)
            Me.label1.TabIndex = 2
            Me.label1.Text = "Progress"
            Me.progressBar1.Anchor = (AnchorStyles.Right Or (AnchorStyles.Left Or AnchorStyles.Top))
            Me.progressBar1.Location = New System.Drawing.Point(12, 36)
            Me.progressBar1.Name = "progressBar1"
            Me.progressBar1.Size = New System.Drawing.Size(436, 18)
            Me.progressBar1.Step = 1
            Me.progressBar1.TabIndex = 7
            MyBase.AutoScaleDimensions = New System.Drawing.SizeF(6!, 13!)
            MyBase.AutoScaleMode = AutoScaleMode.Font
            MyBase.ClientSize = New System.Drawing.Size(460, 80)
            MyBase.Name = "QuickUnzipForm"
            Me.Text = "QuickUnzip"
            AddHandler MyBase.Load, New EventHandler(AddressOf Me.QuickUnzipForm_Load)
            AddHandler MyBase.Shown, New EventHandler(AddressOf Me.QuickUnzipForm_Shown)
        End Sub

        Public Sub OnTimerEvent(ByVal source As Object, ByVal e As EventArgs)
        End Sub

        Private Sub QuickUnzipForm_Load(ByVal sender As Object, ByVal e As EventArgs)
        End Sub

        Private Sub QuickUnzipForm_Shown(ByVal sender As Object, ByVal e As EventArgs)
            '' For info on running long-running tasks in response to button clicks,
            '' in  VB.NET WinForms, see
            Dim args(2) As String
            args(0) = Me.zipfileName
            args(1) = Me.extractDirectory
            Dim worker As System.Threading.Thread
            worker = New System.Threading.Thread(New System.Threading.ParameterizedThreadStart(AddressOf UnzipFile))
        End Sub
        Private Sub UnzipFile(ByVal args As String())
                Using zip As ZipFile = ZipFile.Read(args(0))
                    Me.progressBar1.Maximum = zip.Entries.Count
                    Dim entry As ZipEntry
                    For Each entry In zip
                        entry.Extract(args(1), ExtractExistingFileAction.OverwriteSilently)
                        '' Sleep a little because it's really fast
                    UpdateUi(String.Format("Finished unzipping {0} entries", zip.Entries.Count))
                End Using
            Catch ex1 As Exception
                Me.label1.Text = ("Exception: " & ex1.ToString)
            End Try
            '' close the form (and application) 1000 ms after completion...
            Dim timer1 As New System.Timers.Timer(1000)
            timer1.Enabled = True
            timer1.AutoReset = False
            AddHandler timer1.Elapsed, New System.Timers.ElapsedEventHandler(AddressOf Me.OnTimerEvent)
        End Sub

        Private Sub UpdateUi(ByVal filename As String)
            If Me.InvokeRequired  Then
                '' invoke on the proper thread 
                Me.Invoke(New Action(Of String)(AddressOf UpdateUi),  New Object() { filename })
                Me.label1.Text = filename
            End If            
        End Sub
        ' Fields
        Private extractDirectory As String
        Private zipfileName As String
        Private components As System.ComponentModel.IContainer
        Private label1 As Label
        Private progressBar1 As ProgressBar
    End Class

End Namespace

Last edited Aug 13, 2009 at 8:47 PM by Cheeso, version 5


No comments yet.