c++ zlib 解压gzip字符串,只解压了678字节
时间: 2023-07-09 14:10:51 浏览: 148
您好,可能是由于您的代码实现不正确导致了解压缩失败。下面是一份简单的C++代码,可以用于解压gzip字符串:
```c++
#include <iostream>
#include <fstream>
#include <sstream>
#include <zlib.h>
int main() {
std::string gzip_str = "H4sIAAAAAAAAAG2QzUrDIBBE93yXURiQqEYDctbT+4QzVJWQC0bga8WjQgW4i6Dy+QY8T8F2gSW\n"
"2Gz4jh8e7kM4vN8J5pV1Vms3S8eBpK7ZGnJwEJ2g2r6JqKJ4rEYlMwz7pC5ZwOuTc2fK6z5lO6L\n"
"v2j5yqQ1X9H1eOnqvmJi8jf0HmMhNq8rKqJv5OvBePQIy6aJp6W4yBWBv+O8F6v8Ov8pXf+7vOv\n"
"w+k1guf2QzUu8wzVW+4sV5a9kzgWZ7t1gAAAA==";
std::stringstream compressed_ss(gzip_str);
std::stringstream uncompressed_ss;
std::string uncompressed_str;
const int BUFFER_SIZE = 1024;
char buffer[BUFFER_SIZE];
z_stream deflate_stream;
deflate_stream.zalloc = Z_NULL;
deflate_stream.zfree = Z_NULL;
deflate_stream.opaque = Z_NULL;
deflate_stream.avail_in = 0;
deflate_stream.next_in = Z_NULL;
inflateInit2(&deflate_stream, 47);
while (true) {
deflate_stream.avail_in = compressed_ss.readsome(buffer, BUFFER_SIZE);
if (deflate_stream.avail_in == 0) {
break;
}
deflate_stream.next_in = (Bytef *) buffer;
int ret;
do {
deflate_stream.avail_out = BUFFER_SIZE;
deflate_stream.next_out = (Bytef *) buffer;
ret = inflate(&deflate_stream, 0);
if (ret != Z_OK && ret != Z_STREAM_END) {
std::cerr << "error: " << ret << std::endl;
inflateEnd(&deflate_stream);
return 1;
}
uncompressed_ss.write(buffer, BUFFER_SIZE - deflate_stream.avail_out);
} while (deflate_stream.avail_out == 0);
}
inflateEnd(&deflate_stream);
uncompressed_str = uncompressed_ss.str();
std::cout << uncompressed_str << std::endl;
return 0;
}
```
您可以将 `gzip_str` 替换为您的gzip字符串,然后运行该程序进行解压缩。如果您的gzip字符串确实只有678字节,那么解压缩后的数据应该也只有678字节。
阅读全文