Winrar - Command line to create ZIP Format

Posted on 2011-04-22
Last Modified: 2013-12-01
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.
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\" "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.


Question by:jrbbldr
    LVL 5

    Accepted Solution

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


    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.


    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.
    LVL 12

    Author Comment

    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\" "C:\Temp\Temp*.dbf"

    The above command line ran fine in the DOS Command window and created the output file  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?

    LVL 12

    Author Comment

    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?

    LVL 38

    Assisted Solution

    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!
        ) 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
    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.
    LVL 12

    Author Closing Comment

    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.
    LVL 38

    Expert Comment

    Thank you jrbbldr

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    I previously wrote an article addressing the use of UBCD4WIN and SARDU. All are great, but I have always been an advocate of SARDU. Recently it was suggested that I go back and take a look at Easy2Boot in comparison.
    Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now