- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个包含多个压缩成员的数据缓冲区,它可以是 deflate 或 zlib 压缩成员。
我发现 zlib inflate
调用在处理完第一个压缩 block 后返回 Z_STREAM_END
,这里多个压缩成员可以是任意数量(在我的例子中是 3)。但是这些数据来自其他方面,这些方面没有传达有关数据中压缩成员数量的详细信息。
那么我如何实现 zlib inflate 功能的使用,以便它可以在多个压缩成员上工作?
以下是一个示例快速和肮脏的例子,我试图在其中详细说明我的问题。这涉及 zlib 1.2.5 库的情况。
/* example.c -- understanding zlib inflate/decompression operation
*/
#define CHECK_ERR(err, msg) { \
if (err != Z_OK) { \
std::cerr << msg << " error: " << err << std::endl; \
exit(1); \
} \
}
/* ===========================================================================
* deflate() to create compressed data
*/
void test_deflate(std::vector<uint8_t> & input_data, std::vector<uint8_t>& compr)
{
z_stream c_stream; /* compression stream */
int err;
compr.clear();
c_stream.zalloc = (alloc_func)0;
c_stream.zfree = (free_func)0;
c_stream.opaque = (voidpf)0;
err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
CHECK_ERR(err, "deflateInit");
c_stream.next_in = &input_data[0];
c_stream.avail_in = input_data.size();
for (;;) {
uint8_t c_buffer[10] = {};
c_stream.next_out = &c_buffer[0];
c_stream.avail_out = 10;
err = deflate(&c_stream, Z_FINISH);
if (err == Z_STREAM_END)
{
for (int i = 0; i < (10 - c_stream.avail_out); i++)
compr.push_back(c_buffer[i]);
break;
}
CHECK_ERR(err, "deflate");
for (int i = 0; i < (10 - c_stream.avail_out); i++)
compr.push_back(c_buffer[i]);
}
std::cout << "Compressed data (size = " << std::dec << compr.size() << ") = ";
for (int i = 0; i < compr.size(); i++)
std::cout << (uint32_t) compr[i];
std::cout << std::endl;
err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd");
}
/* ===========================================================================
* Test inflate()
*/
void test_inflate(std::vector<uint8_t> &compr,
std::vector<uint8_t> &uncompr)
{
int err;
z_stream d_stream; /* decompression stream */
uncompr.clear();
d_stream.zalloc = Z_NULL;
d_stream.zfree = Z_NULL;
d_stream.opaque = Z_NULL;
d_stream.avail_in = 0;
d_stream.next_in = Z_NULL;
err = inflateInit(&d_stream);
CHECK_ERR(err, "inflateInit");
d_stream.avail_in = compr.size();
d_stream.next_in = &compr[0];
for(;;) {
uint8_t d_buffer[10] = {};
d_stream.next_out = &d_buffer[0];
d_stream.avail_out = 10;
err = inflate(&d_stream, Z_NO_FLUSH);
if (err == Z_STREAM_END) {
for (int i = 0; i < (10 - d_stream.avail_out); i++)
uncompr.push_back(d_buffer[i]);
if (d_stream.avail_in == 0)
break;
}
CHECK_ERR(err, "inflate");
for (int i = 0; i < (10 - d_stream.avail_out); i++)
uncompr.push_back(d_buffer[i]);
}
err = inflateEnd(&d_stream);
CHECK_ERR(err, "inflateEnd");
std::cout << "Uncompressed data (size = " << std::dec << uncompr.size() << ") = ";
for (int i = 0; i < uncompr.size(); i++)
std::cout << (uint32_t) uncompr[i];
std::cout << std::endl;
}
/* ===========================================================================
* Usage: example
*/
int main(int argc, char **argv)
{
std::vector<uint8_t> input_data;
std::vector<uint8_t> compr, multiple_compr;
std::vector<uint8_t> uncompr;
std::cout << "Input Data (in hex) = ";
for (int i=0; i<32; i++) {
input_data.push_back((uint8_t)i);
if( i && (i % 2 == 0))
std::cout << " ";
std::cout << std::hex << (uint32_t)input_data[i];
}
std::cout << std::endl;
// create compressed buffer-1 from input data
test_deflate(input_data, compr);
// copy compressed buffer-1 data into multiple compressed member buffer
multiple_compr = compr;
compr.clear();
// create compressed buffer-2 from input data
test_deflate(input_data, compr);
// append data of compressed buffer-2 into multiple compressed member buffer
for(int i=0; i< compr.size(); i++)
{
multiple_compr.push_back(compr[i]);
}
// create decompressed output
test_inflate(multiple_compr, uncompr);
// compare decompressed data with input data
std::vector<uint8_t> final_data;
final_data.push_back(input_data);
final_data.push_back(input_data);
if (final_data == uncompr)
std::cout << "Matched" << std::endl;
else
std::cout << "Not Matched" << std::endl;
return 0;
}
1) 这里第二次 inflate 调用返回错误,但我希望它成功进行,为什么它会这样工作?
2) 当我在 inflate 调用参数中使用 Z_FINISH 时它返回错误,为什么我不能在这里使用 Z_FINISH?
请更正我的示例并提出一些优化方法来做同样的事情。
最佳答案
简单地对剩余数据重复膨胀操作。
您可以使用 inflateReset()
代替 inflateEnd()
和 inflateInit()
来节省一些不必要的 free 和 malloc。您可能在 next_in
和 avail_in
中有一些来自上次 inflate 的剩余数据,因此请先使用它,然后重新加载。
关于c++ - zlib inflate解压操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20033014/
起初,我只想在 python3.2 中使用 install feedparser,而它需要 Distribute。当我安装 Distribute 时 python3.2 setup.py instal
我正在尝试在另一台计算机上安装我的 Yesod Web 应用程序。 我已经在我当前的机器上很好地安装了它,并且可以cabal install它在那里没有任何问题。 我似乎在另一台机器上遇到了麻烦(这是
https://www.ietf.org/rfc/rfc1951.txt 的“3.2.7. 使用动态霍夫曼代码压缩(BTYPE=10)”部分描述了压缩期间使用的动态哈夫曼树的编码。可能出现在 DEFL
给定 Elixir 中代表压缩文件的二进制文件,我如何将它们传递到 Erlang 的 zlib 进行膨胀? compressed = > 我已经尝试过: z = :zlib.open() uncomp
我知道 zlib/miniz 提供了 compressBound,它根据纯文本大小返回压缩/压缩大小的上限。这很方便。 是否有用于返回膨胀/解压缩大小上限的膨胀函数(zlib/miniz)?还是一个简
我有一组存储在数据库中的 ZLIB 压缩/base64 编码字符串(在 C 程序中完成)。我编写了一个小型 PHP 页面,应该检索这些值并绘制它们(字符串最初是 float 列表)。 压缩/编码的 C
在https://www.rfc-editor.org/rfc/rfc1951 Note that in the "deflate" format, the Huffman codes for the
在https://www.rfc-editor.org/rfc/rfc1951 Note that in the "deflate" format, the Huffman codes for the
我正在处理处理较大文件的项目,在我们的代码库中,我们会返回寻找写入证书信息,这些寻找的范围大部分时间都非常小,我想在我的流写入器/读取器中使用 zlib为了节省磁盘空间,但由于这样的搜索我无法集成它,
我正在尝试使用以下命令升级 Node 版本:npm install npm@latest -g 命令。但它给出了 zlib 绑定(bind)关闭错误。 有办法解决这个问题吗? 最佳答案 你的 No
这个问题在这里已经有了答案: no module named zlib (9 个回答) 关闭 4 年前。 # pythonbrew venv create django1.5 Creating `d
本文整理了Java中io.gomint.server.jni.zlib.ZLib.process()方法的一些代码示例,展示了ZLib.process()的具体用法。这些代码示例主要来源于Github
本文整理了Java中io.gomint.server.jni.zlib.ZLib.init()方法的一些代码示例,展示了ZLib.init()的具体用法。这些代码示例主要来源于Github/Stack
我想使用 python zlib 压缩文本,并通过 Apache Thrift 发送压缩文本,最后我用 Java 解压了压缩文本。 但我不知道该怎么做。我找不到任何像 Java 中的 python z
是否有允许使用 Zlib 压缩数据的类,或者直接使用 zlib.dylib 是我唯一的可能吗? 最佳答案 NSData+Compression 是一个易于使用的 NSData 类别实现。 NSData
我使用 rvm 安装了 zlib 包和 ruby 1.9.3,但是每当我尝试安装时它说的 gem 无法加载此类文件--zlib 我用来安装的命令是 $ rvm install 1.9.3 $ rv
在 Django Design Patterns ,作者建议使用 zlib.crc32 来屏蔽 URL 中的主键。经过一些快速测试后,我注意到 crc32 大约有一半的时间会生成负整数,这似乎不适合在
我想以 ZLIB 格式在我的 C# 和 C++ 应用程序之间发送压缩数据。在 C++ 中,我使用 boost::iostreams 中可用的 zlib_compressor/zlib_decompre
我在 Python 和 C 中对 crc32 进行了一些试验,但我的结果不匹配。 C: #include #include #include #define NUM_BYTES 9 int ma
来自 ./configure --help: --with-zlib=DIR Include ZLIB support (requires zlib >= 1.0.9) --with-zlib-
我是一名优秀的程序员,十分优秀!