C libarchive error: Unsupported ZIP Compression

Asked

Viewed 84 times

1

I am trying to read a file. zip using libarchive but am getting the following error on archive_read_data()

Unsupported ZIP Compression method (deflation)

Same error occurs with all other compression formats.

follows the code:

archive* a;
archive_entry* entry;

a = archive_read_new();
assert(a != NULL);

archive_read_support_filter_all(a);
archive_read_support_format_all(a);

int result = archive_read_open_filename(a, "Data.zip", 0);

if (result != ARCHIVE_OK) {
    std::cerr << archive_error_string(a) << std::endl;
    return 1;
}

while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
    auto file_size = archive_entry_size(entry);

    std::cout << archive_entry_pathname(entry) << ", size: " << file_size << std::endl;

    auto buffer = new char[file_size];

    auto read_result = archive_read_data(a, buffer, file_size);

    if (read_result <= 0) {
        std::cerr << archive_error_string(a) << std::endl;
        return 1;
    } else if (read_result > 0) {
        std::ofstream file(archive_entry_pathname(entry), std::ios::binary);
        file.write(buffer, file_size);
        file.close();
    }

    delete[] buffer;    
}

archive_read_finish(a);

What I’m doing wrong?

  • You are reading a file of arbitrary size into a buffer, This makes the reading of the data limited to the size of the available memory. You need to segment the reading of the file into smaller blocks. Behold in this example.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.