Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > RAR / UNRAR
 

RAR / UNRAR

hello every one,
i was asked to make a program that does a RAR for certain files in a folder
i only care for the way to RAR or UNRAR a selected file that i provide to this program
i tried to search google for solutions or components but i didnt find any
imp notice: .zip is not an option because i need to apply the .rar to VERY large files ( >2 GB )

i hope i can find the solution
and thank you for teh help
DaMehio  Saturday, July 25, 2009 8:23 AM

Hi DaMehio,

We can use command to call a rar program to achieve your goal. We can use Process.Start to execute a command in C#. This is a code snippet shows how to call winrar to package or extract files:

//Rar path
private const string RAR_PATH = @"C:\Program Files (x86)\WinRAR\WinRAR.exe";
//Package files.
private void RarFiles(string rarPackagePath, List<string> files)
{
    for(int i = 0; i < files.Count; i++)
    {
        files[0] = string.Format("\"{0}\"", files[i]);
    }
    string fileList = string.Join(" ", files.ToArray());
    string cmdArgs = string.Format("A {0} {1}",
        String.Format("\"{0}\"", rarPackagePath),
        fileList);
    Process.Start(String.Format("\"{0}\"", RAR_PATH), cmdArgs);
}
//Extract files.
private void UnrarFiles(string rarPackagePath, string dir)
{
    string cmdArgs = string.Format("X {0} * {1}",
        String.Format("\"{0}\"", rarPackagePath),
        String.Format("\"{0}\"", dir));
    Process.Start(String.Format("\"{0}\"", RAR_PATH), cmdArgs);
}

You can get more support about winrar from:
http://www.win-rar.com/winrarsupport.html.

Let me know if this helps.
Aland Li

This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, July 27, 2009 1:35 PM

Hi DaMehio,

These are my replies
:

1. This is the path of the winrar program on my machine. My operation system is 64-bit, so it adds “x86�because the winrar program is 32-bit. On your machine, it may be different, so you need to replace this string to the winrar program path on your machine.

2. Yes, you are right. I am sorry for the mistake. I have correct it.

3. The command I show is to package some files. The code can be like below:
List<string> files = new List<string>();
files.Add(@"C:\my\test1.txt");
files.Add(@"C:\my\test2.txt");
files.Add(@"C:\my\test3.txt");
RarFiles(@"C:\my\test.rar", files);

If you would like to add all the files in a directory, the code can be like bellow:
List<string> files = new List<string>();
files.AddRange(System.IO.Directory.GetFiles(@"C:\my\test.rar"));
RarFiles(@"C:\my\test.rar", files);
If you would like to add a whole directory to a package, you need to change to another command. You can find related command via the link I provided in my last reply.

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, July 30, 2009 7:19 AM

Hi DaMehio,

We can use command to call a rar program to achieve your goal. We can use Process.Start to execute a command in C#. This is a code snippet shows how to call winrar to package or extract files:

//Rar path
private const string RAR_PATH = @"C:\Program Files (x86)\WinRAR\WinRAR.exe";
//Package files.
private void RarFiles(string rarPackagePath, List<string> files)
{
    for(int i = 0; i < files.Count; i++)
    {
        files[0] = string.Format("\"{0}\"", files[i]);
    }
    string fileList = string.Join(" ", files.ToArray());
    string cmdArgs = string.Format("A {0} {1}",
        String.Format("\"{0}\"", rarPackagePath),
        fileList);
    Process.Start(String.Format("\"{0}\"", RAR_PATH), cmdArgs);
}
//Extract files.
private void UnrarFiles(string rarPackagePath, string dir)
{
    string cmdArgs = string.Format("X {0} * {1}",
        String.Format("\"{0}\"", rarPackagePath),
        String.Format("\"{0}\"", dir));
    Process.Start(String.Format("\"{0}\"", RAR_PATH), cmdArgs);
}

You can get more support about winrar from:
http://www.win-rar.com/winrarsupport.html.

Let me know if this helps.
Aland Li

This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, July 27, 2009 1:35 PM
well ya thank you it seems very logical and works
but i still have few questions

1- in the RAR_Path string, is the "(x86)" to be always included or it just depends on the path
2- in the for loop for the RAR function, i think you should have put files[i] instead of files[0] ?

3- can you please show me how to pass the parameters of ( List<string> files ) When calling the function ?

thank you ...
(am Damehio)
  • Unmarked As Answer byDaMehio Thursday, July 30, 2009 8:00 AM
  • Marked As Answer byDaMehio Thursday, July 30, 2009 8:00 AM
  •  
themehio  Thursday, July 30, 2009 7:03 AM

Hi DaMehio,

These are my replies
:

1. This is the path of the winrar program on my machine. My operation system is 64-bit, so it adds “x86�because the winrar program is 32-bit. On your machine, it may be different, so you need to replace this string to the winrar program path on your machine.

2. Yes, you are right. I am sorry for the mistake. I have correct it.

3. The command I show is to package some files. The code can be like below:
List<string> files = new List<string>();
files.Add(@"C:\my\test1.txt");
files.Add(@"C:\my\test2.txt");
files.Add(@"C:\my\test3.txt");
RarFiles(@"C:\my\test.rar", files);

If you would like to add all the files in a directory, the code can be like bellow:
List<string> files = new List<string>();
files.AddRange(System.IO.Directory.GetFiles(@"C:\my\test.rar"));
RarFiles(@"C:\my\test.rar", files);
If you would like to add a whole directory to a package, you need to change to another command. You can find related command via the link I provided in my last reply.

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, July 30, 2009 7:19 AM
it worked perfectly thank you very much !

one notice, i think you should edit the for loop

files[i] =

string.Format("\"{0}\"", files[i]);

thanks again

themehio  Thursday, July 30, 2009 7:33 AM
Hi DaMehio,

I have corrected the mistake. Please mark my reply as answer if it solves your issue. Thanks for pointing out my mistake.

Best regards,
Aland Li

Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, July 30, 2009 7:36 AM
hi i was interested in this solution but i came across one problem.
What is "rarPackegePath" standing for ???? i mean when you call this method from outside what is the value that "rarPackegePath"is going to get???

itried this once by giving to rarPackagePath the path to where i was going to form mine rar file rarPackagePath=F:\test\ but this gave me the following error :
C:\Documents and Settings\drini.bregasi\My Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\F\test\.rar: Cannot open F\test\daemon4304-lite.exe
The system cannot find the path specified.
Cannot create F\test\.rar
The system cannot find the path specified.

drini  Monday, September 07, 2009 10:07 AM
Hi drini,

"rarPackagePath" refers to the path of the .rar file, for example "C:/test.rar".

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, September 07, 2009 1:11 PM
yes thank you for the reply
i had missed the call that you make to RarFiles three answer above.
drini  Monday, September 07, 2009 1:29 PM
hi, i changed a little the script to suit mine problem but when the rar process is finished I see that the rar has been performed till two directory above the directory where the file reside.
Ex:
the file is located at F:\test\winamp\winamp.exe after the rar winamp.rar is formed inside winamp directory but when i get inside it the rar process has started from:
test\winamp\winamp.exe

how can i fixe this in way that the rar process encapsulates only the file that i want to rar
drini  Wednesday, September 09, 2009 7:50 AM
Aland Li,

I followed this series and I have a few questions.
You mention in places that you corrected errors, but I see none made. If you did make corrections would it be possible to post those here for everyone to see, if you did and I missed them I apologize.

I am interested in adding only one file to an archive, could you possibly show how you would change your code to just rar a file named "filename.asc" (or anything.)

I noticed that you used the Winrar.exe, would the rar.exe be used differently, or would the syntax be the same?

Thank you for your assistance.

Danny
DanInFlorida  Thursday, October 01, 2009 12:39 PM
the answer to use Winrar just for one file is described in this thread:
http://social.msdn.microsoft.com/Forums/en-US/winformsapplications/thread/e2e354c8-0feb-4839-8efb-9f176e43d76f

if you follow the posts carefully and make some test with them you will find that they are the right ones.
drini  Thursday, October 01, 2009 12:59 PM
The post you sent me to only comes back to this one.

I was hoping to see the corrected code example, unless the code above has already been corrected.

In the link you told "For example, the command can be as follows:
cd C:\Users\v-shunli\Documents\MyFile
"C:\Program Files (x86)\WinRAR\WinRAR.exe" A "test.rar" "Rad.RegexDesigner.Setup.1.4.exe"


I admit I am very new to C# but this does not appear to be C# code.

Thank you for sharing,

Danny
DanInFlorida  Thursday, October 01, 2009 1:40 PM
that is not c# code, that is cmd code that it can be used from within c# to set the active directory to the place where the file you want to rar is
ex: if your files is in D:\test\file.exe than by using this code directory.SetCurrentDirectory(@"D:\test"); you put your active directory to "test".

now:
private const string RAR_PATH = @"C:\Program Files\WinRAR\WinRAR.exe";

static void Main(string[] args)
{
string[] rarName = null;

Directory.SetCurrentDirectory(
@"D:\test" );
string[] file = Directory.GetFiles(
@"D:\test" , "*.txt"); \\ this find you inside a directory all the files that meet the second parameter

string[] V = file[0].Split('\\');
for (int x = 0; x < V.Length; x++)
rarName = V[x].Split('.');


string cmdArgs = string.Format("A {0} {1}", rarName[0] + ".rar", rarName[0] + ".txt");
Process.Start(String.Format("\"{0}\"", RAR_PATH), cmdArgs);

}

hope this helps you if you have any questions regarding how it is working just post it here
drini  Thursday, October 01, 2009 2:49 PM

You can use google to search for other answers

Custom Search

More Threads

• three tier archetecture
• Security Exception introducting anything
• Unhandled Exception
• linking two forms in C#
• TaskListControl
• help in datagridview
• Terrarium Server Available
• IssueVision v.s. JavaScript
• Big Bug Question
• RemoveExistingProduct + Major Upgrade