Unzipping a file to disk using libzip

Lucas B

I'm trying to unzip an executable file using c++ and the libzip library. Starting with the answer by rodrigo on a similar question, I come to this sample code:

#include <zip.h>
int main()
{
    //Open the ZIP archive
    int err = 0;
    zip *z = zip_open("foo.zip", 0, &err);

    //Search for the file of given name
    const char *name = "file.txt";
    struct zip_stat st;
    zip_stat_init(&st);
    zip_stat(z, name, 0, &st);

    //Alloc memory for its uncompressed contents
    char *contents = new char[st.size];

    //Read the compressed file
    zip_file *f = zip_fopen(z, "file.txt", 0);
    zip_fread(f, contents, st.size);
    zip_fclose(f);

    //And close the archive
    zip_close(z);
}

from what I understand, this code does work to unzip a file, but I do not know how to write that file to the disk, much like extracting a zip file would so using a tool like winzip does. Having the unzipped data in memory doesn't help me, but I've been unable to figure out how to actually get the files onto the disk.

Galik

Something like this should do it:

if(!std::ofstream("file1.txt").write(contents, st.size))
{
    std::cerr << "Error writing file" << '\n';
    return EXIT_FAILURE;
}

Look up std::ofstream.

Of course you should check all your zip file functions to see if they returned errors before proceeding.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unzipping with libzip loses file metadata

From Dev

Unzipping a file before saving to the disk

From Dev

unzipping bzip file using bash

From Dev

How to write a zip file using Haskell LibZip?

From Dev

How to write a zip file using Haskell LibZip?

From Dev

Unzipping file in phonegap using zip.js

From Dev

Unzipping a file from Base64 to string using DotNetZip

From Dev

Error unzipping a file with javaFX

From Dev

Error when unzipping a file

From Dev

Unzipping results in a file, not a folder

From Dev

Combined decryption and unzipping of a file

From Dev

Unzipping bz2 file

From Dev

Having issues unzipping a .tgz file

From Dev

Unzipping a single file from Archive

From Dev

Get progress of unzipping file in java

From Dev

Unzipping a TAR file, causing an error

From Dev

trouble unzipping file under Windows

From Dev

unzipping large files using python

From Dev

unzipping large files using python

From Dev

Unzipping using tar: --exclude too greedy: how to exclude only root-level file?

From Dev

How to read zip file content without unzipping (in compressed format) using Python

From Dev

Unzipping a .gz file without removing the gzipped file

From Dev

Cpp force write zip using libzip

From Dev

FileNotFound when unzipping file with sub folders

From Dev

Unable to remove zipped file after unzipping

From Dev

Unzipping a .7z file in Azure Automation

From Dev

unzipping ePub file doesn't work

From Dev

Unzipping parts of file with GSUTIL before downloading

From Dev

Unzipping a password protected file in Node.js