繁体   English   中英

为什么我使用 zlib 解压缩不正确?

[英]Why does my use of zlib decompress incorrectly?

请解释这是 Zlib 的错误还是我误解了 Zlib 的使用。

我正在尝试执行以下操作:

-我有两个字符串——我需要从中压缩的数据:string_data_1 和 string_data_2,我用 Zlib 将其压缩为原始数据。

-接下来,我创建第三个字符串并将已经压缩的数据复制到这一行中。

-现在我正在解压缩这个组合的压缩数据并且有问题。

Zlib 只解压压缩数据的“第一”部分,不解压第二部分。 应该是这样吗?

对于facebook/zstd:Zstandard库中的示例 - 完全相同的操作 - 导致解包 - 所有压缩数据以及第一部分和第二部分。

这是一个简单的代码:

#include <iostream>
#include <string>
#include <zlib.h>




int my_Zlib__compress__RAW(std::string& string_data_to_be_compressed, std::string& string_compressed_result, int level_compressed)
{

    //-------------------------------------------------------------------------
    uLong zlib_uLong = compressBound(string_data_to_be_compressed.size());         

    string_compressed_result.resize(zlib_uLong);
    //-------------------------------------------------------------------------


     //this is the standard Zlib compress2 function - with one exception: the deflateInit2 function is used instead of the deflateInit function and the windowBits parameter is set to "-15" so that Zlib compresses the data as raw data:
    int status = my_compress2((Bytef*)&string_compressed_result[0], &zlib_uLong, (const Bytef*)&string_data_to_be_compressed[0], string_data_to_be_compressed.size(), level_compressed);



    if (status == Z_OK)
    {
        string_compressed_result.resize(zlib_uLong);     

        return 0;
    }
        else
        { 
        return 1;
        }

}

int my_Zlib__uncompress__RAW(std::string& string_data_to_be_uncompressed, std::string& string_compressed_data, size_t size_uncompressed_data)
{

    //-------------------------------------------------------------------------
    string_data_to_be_uncompressed.resize(size_uncompressed_data);
    //-------------------------------------------------------------------------


    //this is the standard Zlib uncompress function - with one exception: the inflateInit2 function is used instead of the inflateInit function and the windowBits parameter is set to "-15" so that Zlib uncompresses the data as raw data:
    int status = my_uncompress((Bytef*)&string_data_to_be_uncompressed[0], (uLongf*)&size_uncompressed_data, (const Bytef*)&string_compressed_data[0], string_compressed_data.size());   

    if (status == Z_OK)
    {
        return 0;
    }
}




int main()
{

int level_compressed = 9;     


//------------------------------------------Compress_1-------------------------------------------
std::string string_data_1 = "Hello12_Hello12_Hello125";              //The data to be compressed.

std::string string_compressed_result_RAW_1;                             //Compressed data will be written here

int status = my_Zlib__compress__RAW(string_data_1 , string_compressed_result_RAW_1, level_compressed);
//----------------------------------------------------------------------------------------------


//--------------------------------------Compress_2----------------------------------------------
std::string string_data_2= "BUY22_BUY22_BUY223";              //The data to be compressed.

std::string string_compressed_result_RAW_2;                         //Compressed data will be written here

status = my_Zlib__compress__RAW(string_data_2 , string_compressed_result_RAW_2, level_compressed);
//----------------------------------------------------------------------------------------------


std::string Total_compressed_data = string_compressed_result_RAW_1 + string_compressed_result_RAW_2; //Combine two compressed data into one string

//Now I want to uncompress the data in a string - "Total_compressed_data"


//--------------------------------------Uncompress--------------------------------
std::string string_uncompressed_result_RAW;                         //Uncompressed data will be written here

int size_that_should_be_when_unpacking = string_data_1.size() + string_data_2.size();

status = my_Zlib__uncompress__RAW(string_uncompressed_result_RAW, Total_compressed_data, size_that_should_be_when_unpacking , level_compressed);
//--------------------------------------------------------------------------------

std::cout<<string_uncompressed_result_RAW<<std::endl;  //Hello12_Hello12_Hello125

}

Zlib 只解压压缩数据的“第一”部分,不解压“第二”部分。

应该是这样吗?

如评论中所述,zlib 流的串联不是 zlib 流。 您需要为第二个 zlib 流再次解压缩。 或者首先将整个东西压缩为一个 zlib 流。

您需要使用uncompress2()的变体,而不是uncompress() ,因为前者将在最后一个参数中返回第一个解压缩的 zlib 流的大小,以便您知道从哪里开始解压缩第二个。

更好的是,您应该为您的应用程序使用inflate()函数。 保留未压缩的大小以用于解压缩意味着您在另一端需要它。 你是怎么得到的? 是分开传的吗? 你不需要那个。 您应该使用inflate()一次解压缩一个块,然后您不需要提前知道未压缩的大小。

您还应该使用deflate()函数进行压缩。 然后你可以保持流打开,并继续压缩直到你完成。 然后你将有一个 zlib 流。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM