[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 10286
  • Last Modified:

Winrar - Command line to create ZIP Format

As far as I can tell I am currently using Winrar 3.62 on my Win XP Pro workstation
And with that version the interactive GUI allows me to designate a ZIP format output - and it works.

But I have been trying to run Winrar through the Command Line.
I can get an archive file successfully created, but when I attempt to open it with something else like WinZip, I get an error telling me that the file is not in a valid format.

I am guessing that regardless of the output file extension that I have designated, I am really getting a .RAR file which just happens to have a .ZIP extension.

I have found various Winrar command option references on the web to end up with a valid ZIP format archive file.
    -afzip
    -zip
None have resulted in a file which can be un-zipped with WinZip, but they do extract through WinRAR

Currently I am trying the following:
   C:\PROGRA~1\WINRAR\RAR.EXE a -zip "C:\Temp\ChkZip.zip" "C:\Temp\Temp*.dbf"

Any advice on how to correct my command line so as to get a valid ZIP Format archive file would be greatly appreciated.

Thanks

0
jrbbldr
Asked:
jrbbldr
  • 3
  • 2
2 Solutions
 
cheers4beersCommented:
This switch is supported only by WinRAR. Console RAR is not able to create ZIP archives.

Use WinRAR.exe from the command line instead of RAR.exe:

Switch -AF<type> - specify archive format

This switch allows to define a type of new archive in WinRAR command line. <Type> parameter can be 'rar' or 'zip'.

For example, 'winrar a -afzip arc' will create arc.zip archive.

This switch is supported only by WinRAR. Console RAR is not able to create ZIP archives.


Taken from the WinRAR.chm help file:

It is possible to perform WinRAR commands from the command line. Common command line syntax is described below:

WinRAR <command> -<switch1> -<switchN> <archive> <files...> <@listfiles...> <path_to_extract\>


Command Character combination denoted function to be performed by WinRAR
Switch Switches used to define a specific type of operation, compression strength, type of archive, etc.
Archive The name of the archive to process.
Files The name(s) of files to be processed.
Listfiles Listfiles are plain text files that contain names of files to process. File names should start at the first column. It is possible to put comments to the listfile after // characters. For example, you may create backup.lst containing the following strings:
c:\work\doc\*.txt//backup text documents

c:\work\image\*.bmp//backup pictures

c:\work\misc

and then run:

winrar a backup @backup.lst

You may specify both usual file names and list files in the same command line.
 
Path_to_extract Used only with commands e and x, indicating the folder in which to place extracted files. If this folder does not exist, it will be created.


Notes

a) If neither files nor listfiles are specified, then *.* is implied and WinRAR will process all files;

b) If archive extension is not specified, WinRAR will use the default archive format selected in the default compression profile, but you may override it specifying either .RAR or .ZIP extension;

c) Switches entered on the command line override the same configuration settings;

d) For commands c, e, s, t, rr, k and x wildcards may be used in the archive name. Thus it is possible to process more than one archive with a single command. Moreover, if you specify -r switch with these commands, they will search for archives in subfolders;

e) Some commands and switches are applicable only to RAR archives, some both to RAR and ZIP and some to all archive formats. This is dependent upon the functionality provided by the archive format;

f) Commands and switches are not case sensitive, you may write them both in upper and in lower case.
0
 
jrbbldrAuthor Commented:
Thank you for the quick reply.

I followed your advice and modified the command line.
I changed from RAR.EXE to WinRAR.EXE   and I changed from   -Zip to  -AFZip

Those changes resulted in the following command line:
 C:\PROGRA~1\WINRAR\WinRAR.EXE a -afzip "C:\Temp\ChkZip.zip" "C:\Temp\Temp*.dbf"

The above command line ran fine in the DOS Command window and created the output file   ChkZip.zip  as intended from the specified files.  I watched it run.

However, once again, when I attempt to open the file with WinZip, it gives me an "invalid format" error message.

The reason I need to check that the file can open correctly with WinZip is that I am sending the ZIP file to another vendor and they need it in a 'true' ZIP Format.    Running the Create Archive in the WinRar Windows GUI for a Zip formatted file works just fine.

Any other suggestions?

Thanks
0
 
jrbbldrAuthor Commented:
New development.

Apparently I forgot that I had one other option within the string that I executed for testing and it did not appear above - the    -ep    option

I removed that and I got an Archive file created which did indeed open correctly with WinZip.

Now the next associated question is, if I run the Command Line within my application, will it run synchronously or asynchronously?

I'd like it to run in synchronous mode so that I will 'know' when it is complete and my application can then go on to use the resultant archive file.   Yes, I can have my application go into a wait mode until the archive file 'appears' in the directory, but, if possible with option settings, I'd rather wait.

Any advice?

Thanks
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
BillDLCommented:
I suppose it depends entirely on your application and how it passes out commands to other programs.

Every process results in an Exit Code, Return Code, or Error Code when it completes.  You will hear this referred to as ErrorLevel in, for example, a basic "DOS" batch file.  Taking a simple Windows batch file (*.bat or *.cmd) that runs in a CMD window, you can perform a test to see if a process exited with a Success code like this example which tests for the existence of the Windows Calculator:

@echo off
dir %SystemRoot%\System32\calc.exe > nul 2>&1
    if not %ErrorLevel% equ 0 (
        echo Windows Calculator not found!
        pause
    ) else (
        start %SystemRoot%\System32\calc.exe
)

Note that an Exit Code of Zero signifies a success, anything other than that signifies a failure, a partial failure, or a warning, depending on whether specific exit codes have been programmed into the program.  For the above purposes, a simple directory listing is redirected into thin air, but nevertheless will result in an exit code that reflects whether the DIR listing command was able to locate Calc.exe where specified.  If it wasn't found, the exit code would be a 1 and the batch file should pause and tell you so, and if it was found it will be a 0 it would just launch the calculator and exit.  To test it, just change "calc.exe" to something like "clac.exe".

Looking at the exit codes of the RAR.EXE (note: RAR.exe not WinRAR.exe) in my very old WinRAR version 2.9, they are as follows:

255      USER BREAK      User stopped the process
8      MEMORY ERROR      Not enough memory for operation
7      USER ERROR      Command line option error
6      OPEN ERROR      Open file error
5      WRITE ERROR      Write to disk error
4      LOCKED ARCHIVE      Attempt to modify an archive previously locked
                  by the 'k' command
3      CRC ERROR      A CRC error occurred when unpacking
2      FATAL ERROR      A fatal error occurred
1      WARNING            Non fatal error(s) occurred
0      SUCCESS            Successful operation (User exit)

Obviously you should check your WinRAR help file for the exit codes when using the command line and get the correct ones.

So, If you were to run WinRAR in command line mode with switches set to "continue on errors" (or whatever the options might be), you can still check the exit code on completion and use that however you wish.

Going back to the basic "DOS" batch style ErrorLevel return code, you should be aware that if you do a check like this:

IF ErrorLevel 1 DO something

then it assumes that you mean a return code of 1 OR higher than 1.

To check each known ErrorLevel code in turn you would therefore start with the highest and work down, like this:

if errorlevel 255 do something
if errorlevel 8 do something else
if errorlevel 7 do something entirely different
etc, etc
if errorlevel 0 continue because it worked.

Again in basic batch files you can launch a process in different ways like this:

call ProgramName
or
call :LabelInBatchFile

This opens an external or internal process and returns to where it left off once that "called" process terminates.  So, if instead of the:
start calc.exe
command I used in the exaple batch file, I had used:
call calc.exe
then my "DOS" window would remain open until I closed the calculator.

There are a lot of ways to run commands and to trap and test return codes, and these depend entirely on what language the program launching the command was written/compiled with.  More advanced "scripted" languages would use a CASE statement which in some cases is like the kind of successive "IF" checks I mentioned above except that each "case" may use brackets or use an "Else If" structure.  Some programming languages would use a:
DO WHILE Return Code is not equal to 0
kind of idea.

It all depends on the programming language used and what you proposed doing if a "fail" return code is detected.

In the case of WinRAR at the command line I'm sure you wouldn't need to check the return code, because it would halt and fail on error unless specifically run in a "quiet" mode and told not to report errors.  I would suggest that testing the errorlevel would be the logical way of "knowing when the process completed".

You could always use the "Test Archive" switches to check the integrity of the zip file before allowing further processes to access it, or else use a "verify write" option during the zip file creation if such an option exists.  I can't recall all of the command line switches because I use the free 7-Zip and WinZip command line.

Hopefully this gives you a few ideas to pursue.
0
 
jrbbldrAuthor Commented:
The command line for WinRAR worked well.

As to getting things to work synchronously, I decided to just run a routine to check if WinRAR was still running and, if so, go into a Wait mode.  
When the WinRAR window is found to be no longer running in Windows  I then go on with the execution of the remainder of my application's code.

Thanks for the suggestions.
0
 
BillDLCommented:
Thank you jrbbldr
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now