简体   繁体   中英

Using zlib1.2.7 uncompress gzip data,how to get the files' name in the compression package

Using zlib version 1.2.7 uncompress gzip data, but I couldn't know how to get the files' name in the compression package, or some one you are extracting.The method I find,it looks like read all data to buffer, and then return it.

like this:

int gzdecompress(Byte *zdata, uLong nzdata, Byte *data, uLong *ndata)
{
    int err = 0;
    z_stream d_stream = {0}; /* decompression stream */
    static char dummy_head[2] = {
        0x8 + 0x7 * 0x10,
        (((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF,
    };
    d_stream.zalloc = NULL;
    d_stream.zfree = NULL;
    d_stream.opaque = NULL;
    d_stream.next_in  = zdata;
    d_stream.avail_in = 0;
    d_stream.next_out = data;
    //only set value "MAX_WBITS + 16" could be Uncompress file that have header or trailer text
    if(inflateInit2(&d_stream, MAX_WBITS + 16) != Z_OK) return -1;
    while(d_stream.total_out < *ndata && d_stream.total_in < nzdata) {
        d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
        if((err = inflate(&d_stream, Z_NO_FLUSH)) == Z_STREAM_END) break;
        if(err != Z_OK) {
            if(err == Z_DATA_ERROR) {
                d_stream.next_in = (Bytef*) dummy_head;
                d_stream.avail_in = sizeof(dummy_head);
                if((err = inflate(&d_stream, Z_NO_FLUSH)) != Z_OK) {
                    return -1;
                }
            } else return -1;
        }
    }
    if(inflateEnd(&d_stream) != Z_OK) return -1;
    *ndata = d_stream.total_out;
    return 0;
}

Using Example:

// file you want to extract
filename = "D:\\gzfile";

// read file to buffer
ifstream infile(filename, ios::binary);
if(!infile)
{
    cerr<<"open error!"<<endl;
}
int begin = infile.tellg();
int end = begin;
int FileSize = 0;
infile.seekg(0,ios_base::end);
end = infile.tellg();
FileSize = end - begin;

char* buffer_bin = new char[FileSize];
char buffer_bin2 = new char[FileSize * 2];

infile.seekg(0,ios_base::beg);
for(int i=0;i<FileSize;i++)
    infile.read(&buffer_bin[i],sizeof(buffer_bin[i]));
infile.close( );

// uncompress 
uLong ts = (FileSize * 2);
gzdecompress((Byte*)buffer_bin, FileSize, (Byte*)buffer_bin2, &ts);

Array "buffer_bin2" get the extracted data.Attribute "ts" is the data length.

The question is, I don't know what is it name, is there only one file.How can I get the infomation?

Your question is not at all clear, but if you are trying to get the file name that is stored in the gzip header, then it would behoove you to read the zlib documentation in zlib.h . In fact that would be good idea if you plan to use zlib in any capacity.

In the documentation, you will find that the inflate...() functions will decompress gzip data, and that there is an inflateGetHeader() function that will return the gzip header contents.

Note that when gzip decompresses a .gz file, it doesn't even look at the name in the header, unless explicitly asked to. gzip will decompress to the name of the .gz file, eg foo.gz becomes foo when decompressed, even if the gzip header says the name is bar . If you use gzip -dN foo.gz , then it will call it bar . It is not clear why you even care what the name in the gzip header is.

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