extract a rar file by c code

narayanpatra picture narayanpatra · Aug 18, 2010 · Viewed 6.9k times · Source

Possible Duplicate:
opening a rar file by c

I have to extract a rar file using c code. First I tried out to find the libraries. I got it from http://www.unrarlib.org/. But it was built in 2002. So it do not support the current rar format. Then I checked http://www.rarlabs.com/rar_add.htm. It have libraries but in c++. I don't know anything about c++, So I can't use them. I tried to use the command line tool unrar by using system function. When I used unrar in CMD , It extracted the file but when I used it in C, (command was system("unrar -e -p password protected_file.rar");) It just opened the archieve. It did not extract the file. Now I don't know what to do next? Can anybody suggest me something??

This is the code I am using to open the rar file.In the system command ranjit is the password. It's giving the error undefined symbol_system in module+thefile name. Can anybody help me?? I am struggling on this since two days.

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char **argv)
     {
     char file[20];
     char file2[50] = "F:\\Program Files\\WinRAR\\unrar.exe";
     printf("enter the name of the rar file : ");
     gets(file);
     puts(file);
     system(("%s e -p ranjit %s >C:\stdout.log 2>C:\stderr.log",file2, file));
     getchar();
     return 0;
     }

Answer

bta picture bta · Aug 18, 2010

The manual page for unrar is here. According to the manual, the command syntax is:

unrar <command> [-<switch 1> -<switch N>] archive [files...] [path...]

You mention that you are using the command unrar -e -p password protected_file.rar to extract the archive. Try adding on the optional final parameter [path...] and see if that helps. Chances are, the path is assumed to be the current directory when it is omitted. When you run the command from the shell, everything works as expected. When you run using the system command from within C, you don't know what the current directory is. Manually specify a directory to extract the archive into and see if that changes anything.

If you are still having problems, try redirecting the standard output and standard error of your unrar command to a text file. The utility may be giving you important information on the console that you are not seeing because you are not launching it directly. Try:

system("unrar -e -p password file.rar >C:\stdout.log 2>C:\stderr.log");

and check the log files after your program exits.