简体   繁体   English

在C ++中使用Miniz将向量解压缩为另一个向量

[英]Decompressing a vector into another vector using miniz in c++

I'm trying to do the simplest thing here. 我正在尝试在这里做最简单的事情。 I want to create a method that will take in a byte (char) array, inflate it using miniz tinfl_decompress method and then return a byte array containing the inflated data. 我想创建一个将接收字节(char)数组的方法,使用miniz tinfl_decompress方法对其进行充气,然后返回包含膨胀数据的字节数组。

First things first. 首先是第一件事。 The arrays given will never be bigger than 100kB, vast majority will be smaller than 50k. 给定的阵列永远不会大于100kB,绝大多数将小于50k。 Hence, I don't think I need to use any kind of buffer for it. 因此,我认为我不需要使用任何类型的缓冲区。 Anyway, this is what I've got: 无论如何,这就是我所拥有的:

std::vector<unsigned char> unzip(std::vector<unsigned char> data)
{
    unsigned char *outBuffer = new unsigned char[1024 * 1024];

    tinfl_decompressor inflator;
    tinfl_status status;
    tinfl_init(&inflator);

    size_t inBytes = data.size() - 9;
    size_t outBytes = 1024 * 1024;

    status = tinfl_decompress(&inflator, (const mz_uint8 *)&data[9], &inBytes, outBuffer, (mz_uint8 *)outBuffer, &outBytes, 0);

    return ???
}

I know the output I want begins at memory location &outBuffer , but I don't know how long it is (I do happen to know it will be less than 1MB), so I cannot pack it into a vector and send it on it's way. 我知道我想要的输出从内存位置&outBuffer开始,但是我不知道它有多长(我碰巧知道它会小于1MB),所以我无法将其打包到向量中并以这种方式发送。 I had hoped that outBytes would hold the size of the output, but they are set to 1 after the decompression. 我曾希望outBytes可以保存输出的大小,但是在解压缩后将它们设置为1。 I know that decompression didn't fail, since status returned is TINFL_STATUS_DONE (0) . 我知道解压缩没有失败,因为返回的statusTINFL_STATUS_DONE (0)

Is this even the right way of doing it? 这是正确的方法吗? This is a method that will be called a lot in my program, so I want something that is as fast as possible. 这是在我的程序中经常调用的一种方法,因此我需要尽可能快的方法。

How do I get the vector out of it? 如何从中获取向量? Should I use a different data type? 我应该使用其他数据类型吗? An array (the [] type)? 数组([]类型)? The decompressed data will be read sequentially only once, after what it will be discarded. 解压缩后的数据在丢弃之后将仅被顺序读取一次。

EDIT: 编辑:

It seems that the file I was trying to decompress was not of the proper format; 我试图解压缩的文件似乎格式不正确; it was zip, this takes zlib. 它是zip,需要zlib。

Caveat: Totally untested code. 警告:完全未经测试的代码。

It should go something like exchange 它应该像交换一样

unsigned char *outBuffer = new unsigned char[1024 * 1024];

for 对于

std::vector<unsigned char> outBuffer(1024 * 1024);

to get a vector. 得到一个向量。 Then call tinfl_decompress using the data method to get the vector's underlying buffer. 然后使用data方法调用tinfl_decompress以获取向量的基础缓冲区。 It should look something like 它看起来应该像

status = tinfl_decompress(&inflator, 
                          (const mz_uint8 *)&data[9], 
                          &inBytes, 
                          (mz_uint8 *)outBuffer.data(), 
                          (mz_uint8 *)outBuffer.data(), 
                          &outBytes, 
                          0);

And then resize the vector to the number of bytes stored in the vector for convenience later. 然后将向量的resize为向量中存储的字节数,以方便以后使用。

outBuffer.resize(outBytes);

Note the vector will NOT be resized down. 请注意,矢量将不会缩小。 It will still have a capacity of 1 MiB. 它将仍然具有1 MiB的容量。 If this is a problem, an additional call to std::vector::shrink_to_fit is required . 如果这是一个问题,则需要另外调用std::vector::shrink_to_fit

Finally 最后

return outBuffer;

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

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