Connecting Tech Pros Worldwide Forums | Help | Site Map

Using a Process to Extract a file from Winzip archive

Newbie
 
Join Date: Dec 2007
Posts: 3
#1: Dec 6 '07
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.Arguments
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

Expand|Select|Wrap|Line Numbers
  1. Public Function ZipExtract(ByVal pZippedFile As String, _
  2.                                ByVal pExtractFolder As String) As Boolean
  3.  
  4.         '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5.         '~~~ This function extracts files from a zipped archive
  6.         '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7.  
  8.         Dim psiProcess As New ProcessStartInfo
  9.         Dim udtProc As New Process
  10.  
  11.         Try
  12.             '~~~Set the startup information for the process.
  13.             psiProcess.FileName = "C:\Program Files\WinZip\Winzip32.exe"
  14.             psiProcess.WorkingDirectory = pExtractFolder
  15.             psiProcess.WindowStyle = ProcessWindowStyle.Hidden
  16.             psiProcess.ErrorDialog = False
  17.             psiProcess.CreateNoWindow = True
  18.             psiProcess.Arguments = "-min -e " & pExtractFolder & "\ '" & pZippedFile & "'  " & pExtractFolder & "\"
  19.             udtProc = Process.Start(psiProcess)
  20.             udtProc.WaitForExit()
  21.             udtProc.Close()
  22.             ZipExtract = True
  23.  
  24.         Catch ex As DuplicateNameException
  25.             '~~~ change name on the fly?
  26.         Catch ex As Exception
  27.             If ApplicationUtility.LoadArray(ex.Message) = True Then
  28.                 ZipExtract = False
  29.             End If
  30.         End Try
  31.  
  32.     End Function
  33.  

Member
 
Join Date: Nov 2007
Location: Iowa
Posts: 38
#2: Dec 6 '07

re: Using a Process to Extract a file from Winzip archive


Quote:

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.Arguments
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

Expand|Select|Wrap|Line Numbers
  1. Public Function ZipExtract(ByVal pZippedFile As String, _
  2.                                ByVal pExtractFolder As String) As Boolean
  3.  
  4.         '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5.         '~~~ This function extracts files from a zipped archive
  6.         '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7.  
  8.         Dim psiProcess As New ProcessStartInfo
  9.         Dim udtProc As New Process
  10.  
  11.         Try
  12.             '~~~Set the startup information for the process.
  13.             psiProcess.FileName = "C:\Program Files\WinZip\Winzip32.exe"
  14.             psiProcess.WorkingDirectory = pExtractFolder
  15.             psiProcess.WindowStyle = ProcessWindowStyle.Hidden
  16.             psiProcess.ErrorDialog = False
  17.             psiProcess.CreateNoWindow = True
  18.             psiProcess.Arguments = "-min -e " & pExtractFolder & "\ '" & pZippedFile & "'  " & pExtractFolder & "\"
  19.             udtProc = Process.Start(psiProcess)
  20.             udtProc.WaitForExit()
  21.             udtProc.Close()
  22.             ZipExtract = True
  23.  
  24.         Catch ex As DuplicateNameException
  25.             '~~~ change name on the fly?
  26.         Catch ex As Exception
  27.             If ApplicationUtility.LoadArray(ex.Message) = True Then
  28.                 ZipExtract = False
  29.             End If
  30.         End Try
  31.  
  32.     End Function
  33.  


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
Newbie
 
Join Date: Dec 2007
Posts: 3
#3: Dec 7 '07

re: Using a Process to Extract a file from Winzip archive


Quote:

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
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#4: Dec 7 '07

re: Using a Process to Extract a file from Winzip archive


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?
Reply