Developing Matt

My Technical Journal


leave a comment »

unzipping sounds easy, and for the most part, it is…unless you are automating on a server and don’t want to have the application hold up with any strange prompts.  The problem is, even when you shell out to unzip it will prompt you if you want to overwrite if the file exists.  There are two ways to manage this situation.  1.  use a third party tool.  2.  create an exe that you call with a hidden command that suppresses any prompts.  Here’s the executable that I use.  I’ve used this for two years without a hitch.  Disclaimer: I only call it when I need it: i don’t make several recursive calls to unzip, so that functionality hasn’t been tested.

    Public Function Main(ByVal commandArgs() As String) As Integer
Dim sourcefile As String = Command$() ‘    commandArgs(0).ToString
If sourcefile = “” Then
Console.WriteLine(“Please specify a sourcefile (ex: unzip.exe c:\temp\”)
Return 555
Exit Function
End If
If InStr(sourcefile, “?”) Then
Console.WriteLine(“This unzip utility extracts the files  from the file that you pass.  example: c:\temp\ will ” & _
“extract all files in to the temp directory.  If the files exist already, they will be overwritten.”)
Exit Function
End If
If Right(sourcefile, 1) = “\” Then sourcefile = Left(sourcefile, Len(sourcefile) – 1)
Dim sPath As String = Left(sourcefile, InStrRev(sourcefile, “\”))

Dim myShell As New Shell32.Shell
Dim sourceFolder As Shell32.Folder = myShell.NameSpace(sourcefile)
Dim destinationFolder As Shell32.Folder = myShell.NameSpace(sPath)
Dim i As Integer
For i = 0 To sourceFolder.Items.Count – 1
If System.IO.File.Exists(destinationFolder.Items.Item.Path & “\” & sourceFolder.Items.Item(i).Name) Then _
System.IO.File.Delete(destinationFolder.Items.Item.Path & “\” & sourceFolder.Items.Item(i).Name)
Console.WriteLine(“Unzipped ” & sourceFolder.Items.Item(i).Name & ” to ” & destinationFolder.Items.Item.Path)
Return 0
Catch ex As Exception
Console.WriteLine(“Could not unzip from ” & sourcefile & ” to ” & destinationFolder.Items.Item.Path)
Console.WriteLine(“Error: ” & Err.Description)
Return Err.Number
destinationFolder = Nothing
sourceFolder = Nothing
myShell = Nothing
End Try
End Function


Written by matt

January 24, 2008 at 2:15 pm

Posted in

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: