使用zlib1.2.7解压gzip数据,如何获取压缩包中的文件名
Using zlib1.2.7 uncompress gzip data,how to get the files' name in the compression package
使用zlib version 1.2.7解压gzip数据,但不知道如何获取压缩包中的文件名,或者你是我找到的extracting.The方法,看起来像读取所有数据到缓冲区,然后return它。
像这样:
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;
}
使用示例:
// 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);
数组"buffer_bin2"得到提取出来的data.Attribute"ts"是数据长度
问题是,我不知道它叫什么,只有一个吗file.How我可以得到信息吗?
你的问题一点都不清楚,但如果你想获取存储在 gzip header 中的文件名,那么你应该阅读 [=20= 中的 zlib 文档].事实上,如果您打算以任何身份使用 zlib,那将是个好主意。
在文档中,您会发现 inflate...()
函数将解压缩 gzip 数据,并且有一个 inflateGetHeader()
函数将 return gzip header内容。
请注意,当 gzip
解压缩 .gz
文件时,它甚至不会查看 header 中的名称,除非明确要求。 gzip 将解压缩到 .gz
文件的名称,例如foo.gz
在解压缩时变成 foo
,即使 gzip header 说名称是 bar
。如果您使用 gzip -dN foo.gz
,那么它将调用它 bar
。不清楚为什么你甚至关心 gzip header 中的名称是什么。
使用zlib version 1.2.7解压gzip数据,但不知道如何获取压缩包中的文件名,或者你是我找到的extracting.The方法,看起来像读取所有数据到缓冲区,然后return它。
像这样:
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;
}
使用示例:
// 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);
数组"buffer_bin2"得到提取出来的data.Attribute"ts"是数据长度
问题是,我不知道它叫什么,只有一个吗file.How我可以得到信息吗?
你的问题一点都不清楚,但如果你想获取存储在 gzip header 中的文件名,那么你应该阅读 [=20= 中的 zlib 文档].事实上,如果您打算以任何身份使用 zlib,那将是个好主意。
在文档中,您会发现 inflate...()
函数将解压缩 gzip 数据,并且有一个 inflateGetHeader()
函数将 return gzip header内容。
请注意,当 gzip
解压缩 .gz
文件时,它甚至不会查看 header 中的名称,除非明确要求。 gzip 将解压缩到 .gz
文件的名称,例如foo.gz
在解压缩时变成 foo
,即使 gzip header 说名称是 bar
。如果您使用 gzip -dN foo.gz
,那么它将调用它 bar
。不清楚为什么你甚至关心 gzip header 中的名称是什么。