Using a Process to Extract a file from Winzip archive

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • learningvbnet
    New Member
    • Dec 2007
    • 3

    Using a Process to Extract a file from Winzip archive

    Hi,

    I am trying to extract zipped files using Winzip in my VB.net application and I ran into 2 stone walls.

    1. How do you handle file names with spaces. See psiProcess.Argu ments
    For example "My Data file Apr2007.zip"? In the example below, the
    parameter pZippedFile contains the file name with spaces and the
    pExtractFolder contains the folder where it should be extracted.
    2. Is there anyway to get the extracted file name so that I can rename it? For
    example: The zipped file is "NYC Data.zip", the extracted file is Data.txt and
    It would like to rename it as NYC_Data.txt

    Platform: Windows 2000
    Language: VB.net
    Source Code: See below
    Input Used: Any zip file named as I specified in bullet point #1
    Error Codes: None but the application hangs and does not extract files
    however if I rename all the zip files to have no spaces it works
    In addition the minimized zip file shows the zip file name as
    my.zip ... everything after the first spaces is truncated.
    Please note I tried quotes and brackets. I also tried to find
    answers googling (sp?) and came up empty.

    Any help on this would greatly be appreciated. I have been pulling out my hair
    on this problem for quite awhile.

    Thanks again,
    LearningVB.net

    Code:
    Public Function ZipExtract(ByVal pZippedFile As String, _
                                   ByVal pExtractFolder As String) As Boolean
    
            '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            '~~~ This function extracts files from a zipped archive
            '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
            Dim psiProcess As New ProcessStartInfo
            Dim udtProc As New Process
    
            Try
                '~~~Set the startup information for the process.
                psiProcess.FileName = "C:\Program Files\WinZip\Winzip32.exe"
                psiProcess.WorkingDirectory = pExtractFolder
                psiProcess.WindowStyle = ProcessWindowStyle.Hidden
                psiProcess.ErrorDialog = False
                psiProcess.CreateNoWindow = True
                psiProcess.Arguments = "-min -e " & pExtractFolder & "\ '" & pZippedFile & "'  " & pExtractFolder & "\"
                udtProc = Process.Start(psiProcess)
                udtProc.WaitForExit()
                udtProc.Close()
                ZipExtract = True
    
            Catch ex As DuplicateNameException
                '~~~ change name on the fly?
            Catch ex As Exception
                If ApplicationUtility.LoadArray(ex.Message) = True Then
                    ZipExtract = False
                End If
            End Try
    
        End Function
  • Semajthewise
    New Member
    • Nov 2007
    • 38

    #2
    Originally posted by learningvbnet
    Hi,

    I am trying to extract zipped files using Winzip in my VB.net application and I ran into 2 stone walls.

    1. How do you handle file names with spaces. See psiProcess.Argu ments
    For example "My Data file Apr2007.zip"? In the example below, the
    parameter pZippedFile contains the file name with spaces and the
    pExtractFolder contains the folder where it should be extracted.
    2. Is there anyway to get the extracted file name so that I can rename it? For
    example: The zipped file is "NYC Data.zip", the extracted file is Data.txt and
    It would like to rename it as NYC_Data.txt

    Platform: Windows 2000
    Language: VB.net
    Source Code: See below
    Input Used: Any zip file named as I specified in bullet point #1
    Error Codes: None but the application hangs and does not extract files
    however if I rename all the zip files to have no spaces it works
    In addition the minimized zip file shows the zip file name as
    my.zip ... everything after the first spaces is truncated.
    Please note I tried quotes and brackets. I also tried to find
    answers googling (sp?) and came up empty.

    Any help on this would greatly be appreciated. I have been pulling out my hair
    on this problem for quite awhile.

    Thanks again,
    LearningVB.net

    Code:
    Public Function ZipExtract(ByVal pZippedFile As String, _
                                   ByVal pExtractFolder As String) As Boolean
    
            '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            '~~~ This function extracts files from a zipped archive
            '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
            Dim psiProcess As New ProcessStartInfo
            Dim udtProc As New Process
    
            Try
                '~~~Set the startup information for the process.
                psiProcess.FileName = "C:\Program Files\WinZip\Winzip32.exe"
                psiProcess.WorkingDirectory = pExtractFolder
                psiProcess.WindowStyle = ProcessWindowStyle.Hidden
                psiProcess.ErrorDialog = False
                psiProcess.CreateNoWindow = True
                psiProcess.Arguments = "-min -e " & pExtractFolder & "\ '" & pZippedFile & "'  " & pExtractFolder & "\"
                udtProc = Process.Start(psiProcess)
                udtProc.WaitForExit()
                udtProc.Close()
                ZipExtract = True
    
            Catch ex As DuplicateNameException
                '~~~ change name on the fly?
            Catch ex As Exception
                If ApplicationUtility.LoadArray(ex.Message) = True Then
                    ZipExtract = False
                End If
            End Try
    
        End Function

    Hi I have a suggestion for you. I'm still somewhat new to VB but it should work in theory. I know that VB can find the files even when they heve spaces so..... why not use FileCopy command and create a temporary copy of the file with a name like "UnzipTemp" . Then use your function to unzip this temp file and delete the temporary.
    (Not sure if this is last bit can be done)
    Save the old filename when you copy it as a string, then change the string slightly to make it a new filename.
    (edit) I looked it up it can be done with only very little code

    Hope this helps

    James

    Comment

    • learningvbnet
      New Member
      • Dec 2007
      • 3

      #3
      Originally posted by Semajthewise
      Hi I have a suggestion for you. I'm still somewhat new to VB but it should work in theory. I know that VB can find the files even when they heve spaces so..... why not use FileCopy command and create a temporary copy of the file with a name like "UnzipTemp" . Then use your function to unzip this temp file and delete the temporary.
      (Not sure if this is last bit can be done)
      Save the old filename when you copy it as a string, then change the string slightly to make it a new filename.
      (edit) I looked it up it can be done with only very little code

      Hope this helps

      James
      James ...
      Thank you for the suggestion. I will try it out and let you know what the outcome is.

      Thanks again
      Learningvbnet

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Winzip should have some command line parameters to chose a destination directory (and maybe a filename)
        But since you're just using the program and not API calls, they might not have included a rename feature in it.


        Why not just use the built in ZIP/UNZIP objects?

        Comment

        Working...