2017-03-19 7 views
0

https://panthema.net/2007/0328-ZLibString.html에서 발췌 문장을 사용하여 문자열을 압축합니다. 내 문제는 deflateInit() 항상 Z_STREAM_ERROR 반환합니다.zlib deflateInit은 항상 Z_STREAM_ERROR을 반환합니다.

zlib manual에 따르면 이는 압축 수준이 잘못되었음을 의미합니다.

deflateInit 성공한다면 Z_OK를 반환 Z_MEM_ERROR 메모리가 충분하지 않은 경우 Z_STREAM_ERROR 레벨 유효한 압축 레벨이 아닌 경우 바로 압축 레벨은 매뉴얼에 따라 0 사이의 값이다

-9.

압축 레벨 Z_DEFAULT_COMPRESSION이거나 9 0 내지 : 최고 속도를 제공 하나,도 9는 0 (입력 데이터가 단순히 한번에 블록을 복사 전혀 압축을 제공하지 최상의 압축을 준다). Z_DEFAULT_COMPRESSION은 속도와 압축 사이의 기본 절충을 요청합니다 (현재 레벨 6과 같습니다).

그러나 압축 할 때 어떤 값을 지정하든 항상 -2(Z_STREAM_ERROR)을 반환합니다.

#include <string> 
#include <stdexcept> 
#include <iostream> 
#include <iomanip> 
#include <sstream> 

#include "zlib.h" 

std::string compress_string(const std::string& str, int compressionlevel) 
{ 
    z_stream zs;      // z_stream is zlib's control structure 
    memset(&zs, 0, sizeof(zs)); 

    //std::cout << deflateInit(&zs, 9); //Always returns -2, no matter if the value is between 0-9 
    if (deflateInit(&zs, compressionlevel) != Z_OK) 
     throw(std::runtime_error("deflateInit failed while compressing.")); 

    zs.next_in = (Bytef*)str.data(); 
    zs.avail_in = str.size();   // set the z_stream's input 

    int ret; 
    char outbuffer[32768]; 
    std::string outstring; 

    // retrieve the compressed bytes blockwise 
    do { 
     zs.next_out = reinterpret_cast<Bytef*>(outbuffer); 
     zs.avail_out = sizeof(outbuffer); 

     ret = deflate(&zs, Z_FINISH); 

     if (outstring.size() < zs.total_out) { 
      // append the block to the output string 
      outstring.append(outbuffer, 
       zs.total_out - outstring.size()); 
     } 
    } while (ret == Z_OK); 

    deflateEnd(&zs); 

    if (ret != Z_STREAM_END) {   // an error occurred that was not EOF 
     std::ostringstream oss; 
     oss << "Exception during zlib compression: (" << ret << ") " << zs.msg; 
     throw(std::runtime_error(oss.str())); 
    } 

    return outstring; 
} 

저는 Windows GnuWin32 설치 프로그램에서 얻은 zlib 1.2.3을 사용하고 있습니다. 내가 zconf.h에서 변경했고, #if 1을 주석 처리했고, 대신에 을 넣었습니다. 리눅스가 아니기 때문에, unistd.h이 없기 때문입니다.

은 그래서

//#if 1   /* HAVE_UNISTD_H -- this line is updated by ./configure */ 
#if HAVE_UNISTD_H 
# include <sys/types.h> /* for off_t */ 
# include <unistd.h> /* for SEEK_* and off_t */ 

추가 노트처럼 보이는 : 나는 Z_SOLO 정의가 없습니다.

답변

1

에서 Z_STREAM_ERROR을 얻는 방법은 두 가지입니다. 첫 번째 인수가 NULL이거나 두 번째 인수가 -1..9 범위에없는 경우입니다. 검사와 전달 된 compressionLevel에 대한 귀하의 주장에 따르면 코드가 어느 조건에도 위배 될 수있는 것으로 보이지 않습니다.

내가 할 수있는 유일한 추측은 zlib의 컴파일과 링크가 어딘가에서 엉망이되어 전달 된 유형이 루틴이 기대하는 것과 일치하지 않아 루틴이 보낸 것과 다른 레벨을 수신하게된다는 것입니다.