2012-11-22 5 views
1

ZIP 파일 압축 풀기에 대한 설명서와 게시물을 읽었지만 추가 질문이 있습니다. Qt에서 압축 파일의 압축을 풀어야합니다. 그것은 gzip으로 압축 된 XML 파일입니다. qUnCompress는 ZLIB로 준비된 zip 파일을 압축 할 수 있으며 ZLIB는 GZIP보다 다른 헤더를 가지고 있음을 알고 있습니다.Qt qUnCompress 함수로 파일 압축 해제

은 내가 문서 읽을 때 :

Note: If you want to use this function to uncompress external data that was compressed using zlib, you first need to prepend a four byte header to the byte array containing the data. The header must contain the expected length (in bytes) of the uncompressed data, expressed as an unsigned, big-endian, 32-bit integer.

가 내가 (가 bigEndian)를 시작하는 유일한 길이 및 압축 된 데이터보다 넣어야 할 것을 의미인가?

당신은 쓸 필요가

qUncompress: Z_DATA_ERROR: Input data is corrupted

답변

1

당신이 gUncompress() 중 하나 zlib를 사용하여 기능, 또는 DEFLATE 알고리즘을 구현하는 다른 라이브러리를 소유 : 내가 그것을했다하지만 난 qUncompress 기능에서 오류가 있습니다. 나는 개인적으로 miniz을 선호 :

http://code.google.com/p/miniz/

여기 당신을 위해 몇 가지 코드입니다 :

#include <stdexcept> 

#include <QtCore> 

#ifndef TINFL_HEADER_FILE_ONLY 
# define TINFL_HEADER_FILE_ONLY 
#endif // TINFL_HEADER_FILE_ONLY 
extern "C" { 
# include "tinfl.h" 
} 

#include "guncompress.hpp" 

static tinfl_decompressor inflator; 

static QByteArray result(TINFL_LZ_DICT_SIZE, 0); 

////////////////////////////////////////////////////////////////////////////// 
QByteArray gUncompress(QByteArray const& data) 
{ 
    mz_uint8 const* inPtr(reinterpret_cast<mz_uint8 const*>(data.data()) + 10); 

    tinfl_init(&inflator); 

    size_t inAvail(data.size()); 
    size_t outTotal(0); 

    tinfl_status ret; 

    do 
    { 
    size_t inSize(inAvail); 
    size_t outSize(result.size() - outTotal); 

    ret = tinfl_decompress(&inflator, 
     inPtr, 
     &inSize, 
     reinterpret_cast<mz_uint8*>(result.data()), 
     reinterpret_cast<mz_uint8*>(result.data()) + outTotal, 
     &outSize, 
     0 
    ); 

    switch (ret) 
    { 
     case TINFL_STATUS_HAS_MORE_OUTPUT: 
     inAvail -= inSize; 
     inPtr += inSize; 

     result.resize(2 * result.size()); 

     case TINFL_STATUS_DONE: 
     outTotal += outSize; 
     break; 

     default: 
     throw std::runtime_error("error decompressing gzipped content"); 
    } 
    } 
    while (TINFL_STATUS_DONE != ret); 

    return QByteArray::fromRawData(result.data(), outTotal); 
} 

는 또한 동일한 형식을 공유하지 않는 zip 파일와 gzip 파일을 확인합니다. Zip 파일은 포함 된 파일의 디렉토리를 포함하므로 다르게 처리해야합니다.

0

Qt 소스에서 qzip.cpp, qzipreader_p.h, qzipwriter_p.h를 찾으십시오. zip 파일을 읽고 쓰는 데 사용할 수 있습니다.