简体   繁体   中英

Decompressing gzip file to memory using libarchive

I'm trying to decompress programmatically a gzip file into memory and mimic the command gzip -d file.gz using libarchive project . The file is actually taken from http response with the following header Accept-Encoding: gzip, deflate

Here my attempt to read the file. I don't expect not to work since the gzipped file has no entries (it's compressed as a stream) and archive_read_next_header tries to read the next file out of the arcihve.

Are there any alternative to this function that extract the entire data out of the compressed file.

archive_read_support_format_raw(archive); 
archive_read_support_filter_all(archive);
archive_read_support_compression_all(archive)

archive_read_open_memory(archive, file_data, file_size);
struct archive_entry *entry;
la_ssize_t total, size;
char *buf;    
int status = archive_read_next_header(archive, &entry);

Perhaps someone can post minimal code example that resolve this issue ? Also, Is there an option to find out whether a gzip archive file has entries or not ?

One possible alternative is to use boost::iostreams library which comes with an inbuilt gzip filter and allows exactly what you want - streaming decompression from a gzip file in memory. Here is the reference to the gzip filter , and a snippet from the same:

ifstream file("hello.gz", ios_base::in | ios_base::binary);
filtering_streambuf<input> in;
in.push(gzip_decompressor());
in.push(file);
boost::iostreams::copy(in, cout);

Edit: Actually a much better snippet is available here https://stackoverflow.com/a/16693807/3656081

There are two ways to do this using zlib :

  1. Using the inbuilt GzFile API: Coliru Link - Read more on this here
int inf(FILE* fp) {
    auto gzf = ::gzdopen(fileno(fp), "r");
    assert(::gztell(gzf) == 0);
    std::cout << "pos: " << ::gztell(gzf) << std::endl;
    ::gzseek(gzf, 18L, SEEK_SET);
    char buf[768] = {0};
    ::gzread(gzf, buf, sizeof(buf)); // use a custom size as needed
    std::cout << buf << std::endl; // Print file contents from 18th char onward
    ::gzclose(gzf);
    return 0;
}
  1. The native inflate API: Coliru Link . More on this in the manual link above and here . My code is almost completely a duplicate of the provided link and pretty long, so I wont repost.

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