tags:

views:

271

answers:

1

I am using the boost filtering stream object to read gzipped files. Works great! I would like to display a progress bar for the amount of the file that has been processed. I need find the input uncompressed file size. Does the gzip decompressor have access to the original file size from the gzipped file? I couldn't find it on the boost gzip_decompressor reference page. Really the progress dialog is the goal, is there another way to figure out position in the compressed file?

    // gets compressed file size, need uncompressed size
    boost::uintmax_t fs = boost::filesystem::file_size (
        boost::filesystem::path (fname)
        );

    std::ifstream file (fname, std::ios_base::in | std::ios_base::binary);
    boost::iostreams::filtering_istream in;
    in.push (boost::iostreams::gzip_decompressor());
    in.push (file);

    std::string line;
    size_t bytes_read = 0;
    while (in)
    {
        std::getline (in, line);
        bytes_read += line.size ();
        // progress dlg with bytes_read / uncompressed size
    }
A: 

Hi, The information you are after is definitely there (uncompressed data size is recorded into the last 4 bytes of a gzip file, (see GZIP spec) but taking a look at the headers for the boost library (seen here) it is not exposed anywhere. The only place it seems to be even looked at is when doing checks to make sure there was no corruption in read_footer. You could read the value yourself directly out of the file (just assemble the last 4 bytes into an int, being mindful of their ordering (see GZIP spec again)), or use a different library to do the unzipping.

David Hay
It's easy enough to seek to the end of the file and read the size. I guess I'll do that. Thanks.
Dan
Or, you could modify boost to add this information and submit a patch for it. Then everybody benefits from the work.
KeithB