ZLib не будет сжимать за пределы выхода ~ 600kb в D - PullRequest
0 голосов
/ 12 марта 2019

Я пробовал как std.zlib, так и напрямую использовать zlib, но, похоже, он не сжимает ничего выше отметки 600 КБ. Я в основном следовал всем интерактивным руководствам, за исключением входного буфера, поскольку мне приходится сжимать предварительно помеченное пространство памяти, а не файл на диске.

Забыли код, этот не идет дальше ~ 600 КБ:

        int ret, flush;
        zlib.z_stream strm;
        strm.zalloc = null;
        strm.zfree = null;
        strm.opaque = null;
        ret = zlib.deflateInit(&strm, compLevel);
        if (ret != zlib.Z_OK)
            throw new Exception("Compressor initialization error");
        ubyte[] output;
        output.length = cast(uint)imageData.length;
        strm.next_in = imageData.ptr;
        strm.avail_in = cast(uint)imageData.length;
        strm.next_out = output.ptr;
        strm.avail_out = cast(uint)output.length;
        do{
            ret = zlib.deflate(&strm, zlib.Z_FINISH);
            if(!(ret == zlib.Z_OK || ret == zlib.Z_STREAM_END)){
                //version(unittest) std.stdio.writeln(strm.total_out);
                zlib.deflateEnd(&strm);
                throw new Exception("Compressor output error: " ~ cast(string)std.string.fromStringz(strm.msg));
            }
        } while (ret != zlib.Z_STREAM_END);
        writeBuffer = cast(void[])[Chunk(cast(uint)strm.total_out, DATA_INIT).nativeToBigEndian] ~ output[0..cast(size_t)strm.total_out];
        file.rawWrite(writeBuffer);
        crc = crc32Of(writeBuffer[4..$]).dup.reverse;
        file.rawWrite(crc);

Ранее я пробовал это, но сразу же возникает ошибка потока без вывода:

int ret, flush;
//uint have;
zlib.z_stream strm;
strm.zalloc = null;
strm.zfree = null;
strm.opaque = null;
ret = zlib.deflateInit(&strm, compLevel);
if (ret != zlib.Z_OK)
    throw new Exception("Compressor initialization error");
ubyte[] output;
static if(writeblocksize < 2048)
    output.length = 2048;
strm.next_in = imageData.ptr;
strm.avail_in = cast(uint)imageData.length;
do {
    flush = strm.avail_in ? zlib.Z_NO_FLUSH : zlib.Z_FINISH;
    strm.next_out = output.ptr;
    strm.avail_out = cast(uint)output.length;
    ret = zlib.deflate(&strm, flush);
    if(ret == zlib.Z_STREAM_ERROR){
        version(unittest) std.stdio.writeln(ret);
        zlib.deflateEnd(&strm);
        throw new Exception("Compressor output error: " ~ cast(string)std.string.fromStringz(strm.msg));
    }
    //version(unittest) std.stdio.writeln(strm.total_out);
    //writeBuffer = output[0..$-strm.avail_out];
    writeBuffer = cast(void[])[Chunk(cast(uint)writeBuffer.length, DATA_INIT).nativeToBigEndian] ~ output;
    file.rawWrite(writeBuffer);
    crc = crc32Of(writeBuffer[4..$]).dup.reverse;
    file.rawWrite(crc);
    //writeBuffer.length = 0;
} while (flush != zlib.Z_FINISH);
zlib.deflateEnd(&strm);
...