Deflate and inflate for PDF, using zlib C++

柔情痞子 提交于 2019-12-13 01:30:03

问题


I am trying to implement the "zlib.h" deflate and inflate functions to compress and decompress streams in PDF-file. Input: compressed stream from PDF-file. I implemented inflate function -- it's all right, I have uncopressed stream, after that I try to compress this stream again with deflate function, as output I have compressed stream, but it is not equal to input compressed stream and they are not equal to the length. What I'm doing wrong? This is a part of my code:

     size_t outsize = (streamend - streamstart) * 10;
            char* output = new char[outsize]; ZeroMemory(output, outsize);

            z_stream zstrm; ZeroMemory(&zstrm, sizeof(zstrm));
            zstrm.avail_in = streamend - streamstart + 1;
            zstrm.avail_out = outsize;
            zstrm.next_in = (Bytef*)(buffer + streamstart);//block of date to infalte 
            zstrm.next_out = (Bytef*)output; 

            int rsti = inflateInit(&zstrm);
            if (rsti == Z_OK)
            {
                int rst2 = inflate(&zstrm, Z_FINISH);
                if (rst2 >= 0)
                {
                    cout << output << endl;//inflated data
                }
            }

            char* deflate_output = new char[streamend - streamstart];           
            ZeroMemory(deflate_output, streamend - streamstart);
            z_stream d_zstrm; ZeroMemory(&d_zstrm, sizeof(d_zstrm));

            d_zstrm.avail_in = (uInt) (strlen(output)+1);
            d_zstrm.avail_out = (uInt) (streamend - streamstart);
            d_zstrm.next_in = (Bytef*)(output);
            d_zstrm.next_out = (Bytef*)(deflate_output);
            int rsti1 = deflateInit(&d_zstrm, Z_DEFAULT_COMPRESSION);

            if (rsti1 == Z_OK)
            {
                int rst22 = deflate(&d_zstrm, Z_FINISH);
                out << deflate_output << endl << "**********************" << endl;
//I try to write deflated stream to file
                printf("New size of stream: %lu\n", (char*)d_zstrm.next_out - deflate_output);
            }

回答1:


There is nothing wrong. There is not a unique compressed stream for a given uncompressed stream. All that is required is that the decompression give you back exactly what was compressed (hence "lossless").

It may simply be caused by different compression parameters, different compression code, or even a different version of the same compression code.

If you can't reproduce the original compressed data, so what? All that matters is that you can make a valid PDF file that can be decompressed and has the content that you want.



来源:https://stackoverflow.com/questions/35335473/deflate-and-inflate-for-pdf-using-zlib-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!