0

I want to extract a .rar file to a folder using c++ with zlib.

My Code:

int main (){
    gzFile infile = gzopen("C:\\Users\\Nico\\Desktop\\a.rar", "rb");
    FILE *outfile = fopen("C:\\Users\\Nico\\Desktop\\ToThisFolder", "wb");

    if (!infile || !outfile) {
        return -1;
    }

    char buffer[128];
    int num_read = 0;
    while ((num_read = gzread(infile, buffer, sizeof(buffer))) > 0) {
        fwrite(buffer, 1, num_read, outfile);
    }

    gzclose(infile);
    fclose(outfile);
}

If I run the code, my program always go to return -1, because he don't accept the output file.

If you look to my outfile, i want to put the output into a folder.

How can I do this?

ThecCode is from http://www.codeguru.com/cpp/cpp/algorithms/compression/article.php/c11735/zlib-Add-Industrial-Strength-Compression-to-Your-CC-Apps.htm

Thanks

Søny
  • 109
  • 1
  • 7

1 Answers1

0

You can't use fopen() to open a folder, only a file. Thus outFile is NULL.

To extract a RAR (or any other archive format) to a folder, you have to open the archive and enumerate its stored files, extracting each one individually to a separate opened file.

open archive
for (each file in archive)
{
    create output file
    read archive file data, write to output file
    close output file
}
close archive

That being said, you can't use the ZLib library to process RAR archives. It only supports ZLib and GZip files. You need a library that actually supports RAR, such as the one provided by WinRAR, 7-Zip, etc.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770