views:

131

answers:

2

I have to write code in C to extract a password protected rar file in windows. I don't have any clue about how to do this. can anybody suggest me something or provide a sample piece of code? I will be very thankful.

EDIT:

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. EDIT: This code opens the archive but do not extract it. If I uses the unrar command in command line, it extracts the file. What I should I do?

#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;
     }
+1  A: 

http://stackoverflow.com/questions/2327784/using-unrar-library-extracting-files-into-a-filestream-buffer

But if you're looking for a pure C solution, take a look at: http://www.unrarlib.org/

Quote from their FAQ: The URARFileLib (short name for UniquE RAR File Library, also called unrarlib) is a free library for C programmers to access RAR archives.

Another approach, which I just tested successfully, doesn't require the use of external libraries to decompress rar files. Use system() to invoke a command-line tool (such as unrar ) already installed on your system to do the job:

system("unrar x -ppassword protected_file.rar /destination_directory");

For instance, let's say the protected file was named file.rar, the password was 1234 and the destination directory was /home/user, you would call system() with the following parameters:

system("unrar x -p1234 file.rar /home/user/");
karlphillip
Thanks for help but that's in c++ and I don't know anything in c++.
also, filestream sux. don't mislead the innocent ;)
Matt Joiner
@karlphillip I visited http://www.unrarlib.org/. They are providing a exe file viz. unrarlib. Can I install it and use it someway for my code? I thought it will be a header file. They released it in 2002, would it work now?
Thanks man, I got the header file from unrarlib.org. It have 2 files.First is unrarlib.c andunrarlib.h. Do I have to include both the files in my code?
@karlphillipI am trying to use use the Unrar command line tool. When I using it from command prompt., It's extracting the file. But when I use the system fuenction to call it, it just open the rar file but do not extarct. Can anybody help me in this?
Help please. I am struggling a lot in this.
I updated my answer with code I just tried out. It worked.
karlphillip
@karlphilipyou tried it in windows?? I tried your code, it just opens the archive. It do not extract it.
+1  A: 

In addition to what karlphilip's suggestions there's also a couple of potentialliy interesting looking resources at http://www.rarlabs.com/rar_add.htm.

In particular I am thinking UnRAR.dll and UnRAR source may be relevant. I can't really check it out at the momment though.

torak
I visited http://www.rarlabs.com/rar_add.htm. but their header files are ic c++. Can I use them in c??
Unfortunately, like I said, I can't check it out at the momment. However, as a quick gude, if the header defines classes, you can't use it in C. If instead it defines a bunch of functions defined in `extern "C"` wrappers you're probably good to go, just compile your C program with a C++ compiler so that the runtime issues should come out in the wash.
torak