简体   繁体   中英

Reading zip arhive header from memory using libarchive

I am trying to read headers of zip file, stored in memory using libarchive. Here is my try

ArchiveReader::ArchiveReader(std::string filename) {
    if (!boost::filesystem::exists(filename)) {
        throw FileDoesNotExistsException(filename);
    }
    file_buffer_str = read_file_into_memory(filename);
    file_buffer = file_buffer_str.c_str();
    archive = archive_read_new();
    archive_read_support_filter_all(archive);
    archive_read_support_format_all(archive);
    reading_result = archive_read_open_memory(archive,file_buffer, sizeof(file_buffer));
    auto k = archive_read_next_header(archive, &entry);
    while (archive_read_next_header(archive, &entry) == ARCHIVE_OK) {
        printf("%s\\n", archive_entry_pathname(entry));
        archive_read_data_skip(archive);  // Note 2
    }
    reading_result = archive_read_free(archive);
}
std::string ArchiveReader::read_file_into_memory(const std::string& current_file) {
    std::ifstream raw_file(current_file, std::ios::binary);
    auto buffer = [&raw_file]{
        std::ostringstream ss{};
        ss << raw_file.rdbuf();
        return ss.str();
    }();
    return buffer;
}

Unfortunately, archive_read_next_header return -30, which means truncated zip header. Can someone help?

sizeof does not return the length of a string. In any case don't use C strings in a C++ program. Here's how you should code it

reading_result = archive_read_open_memory(archive, 
    file_buffer_str.data(), 
    file_buffer_str.size());

The C string file_buffer is not required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM