Elmub
Elmub

Reputation: 141

How to rar/unrar with command line parameters

I've looked through the internet on how winrar's command line parameters work, and this is what I have so far

        void LOCK(string fld, string pw)
    {
        Process p = new Process();
        p.StartInfo.FileName = @"C:\Program Files\WinRAR\WinRAR.exe";
        p.StartInfo.Arguments = "rar a -p" + pw + " PL_LOCKED_ARCHIVE.rar " + fld;
        p.Start();
    }

    void UNLOCK(string fld, string pw)
    {
        Process p = new Process();
        p.StartInfo.FileName = @"C:\Program Files\WinRAR\WinRAR.exe";
        p.StartInfo.Arguments = "unrar x -p" + pw + " PL_LOCKED_ARCHIVE.rar";
        p.Start();
    }

However it doesn't seem to create any archive anywhere, with a test folder being C:\PicsAndStuff

Upvotes: 3

Views: 20868

Answers (2)

Mofi
Mofi

Reputation: 49086

The StartInfo you define results in running WinRAR.exe with command line:

C:\Program Files\WinRAR\WinRAR.exe unrar x -p pw PL_LOCKED_ARCHIVE.rar

That is of course wrong as you do not want to run WinRAR.exe with first argument being a reference to console version Rar.exe or UnRAR.exe. The result is most likely an error message because of invalid command rar respectively unrar as the first argument must be a or x for WinRAR.exe.

So first of all you need to correct StartInfo:

void LOCK(string fld, string pw)
{
    Process p = new Process();
    p.StartInfo.FileName = @"C:\Program Files\WinRAR\Rar.exe";
    p.StartInfo.Arguments = "a -p" + pw + " PL_LOCKED_ARCHIVE.rar " + fld;
    p.Start();
}

void UNLOCK(string fld, string pw)
{
    Process p = new Process();
    p.StartInfo.FileName = @"C:\Program Files\WinRAR\UnRAR.exe";
    p.StartInfo.Arguments = "x -p" + pw + " PL_LOCKED_ARCHIVE.rar";
    p.Start();
}

Further all commands and switches of console version Rar.exe are briefly explained when simply running Rar.exe without any parameter in a command prompt window. Also UnRAR.exe outputs a brief help if executed without any parameter.

Last but not least there is a complete manual for Rar.exe which of course can also extract files and folders from a RAR archive which makes additional usage of UnRAR.exe useless. The manual is text file Rar.txt in program files folder of WinRAR which you should read from top to bottom. I suggest to build the command line while reading it and test the command line first from within a command prompt window.

Note 1: Rar.exe is shareware. Only UnRAR.exe is freeware.

Note 2: GUI version WinRAR.exe supports more than console version Rar.exe and therefore the list of switches differ slightly. Complete documentation for WinRAR.exe can be found in help of WinRAR opened with Help - Help Topics or pressing key F1. Open in help on tab Contents the item Command line mode and read. WinRAR.exe is also shareware.

Upvotes: 4

Shaharyar
Shaharyar

Reputation: 12439

You need to encrypt both file data and headers.

According to Documentation (Command line mode > Switches > "-hp[pwd] - encrypt both file data and headers"):

This switch is similar to -p[p], but switch -p encrypts only file data and leaves other information like file names visible. This switch encrypts all sensitive archive areas including file data, file names, sizes, attributes, comments and other blocks, so it provides a higher security level.

This is how you can access to it using command line:

Syntax: rar a -hp[MyPassword] -r [filepath] [folderpath]

"C:\Program Files\WinRAR\WinRAR.exe" a -hp12345678 -r d:\zipProject d:\Project

C# Code:

void LOCK(string fld, string pw)
{
    Process p = new Process();
    p.StartInfo.FileName = @"C:\Program Files\WinRAR\WinRAR.exe";
    p.StartInfo.Arguments = "rar a -hp" + pw + " PL_LOCKED_ARCHIVE.rar " + fld;
    p.Start();
}

Upvotes: 1

Related Questions