使用zlib C ++为PDF压缩和膨胀
我正在尝试实现"zlib.h" deflate和inflate函数来压缩和解压缩PDF文件中的流. 输入:来自PDF文件的压缩流.我实现了inflate函数-没关系,我有未压缩的流,之后我尝试使用deflate函数再次压缩该流,因为输出我有压缩流,但它不等于输入的压缩流,并且不等于长度.我做错了什么?这是我的代码的一部分:
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);
}
没有错.给定的未压缩流没有唯一的压缩流.只需要做的就是减压就可以将压缩后的内容完全归还给您(因此无损").
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.
如果您无法再现原始的压缩数据,那又如何呢?重要的是,您可以制作一个可以解压缩并具有所需内容的有效PDF文件.
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.