티스토리 뷰

출처 : http://moyaria.tistory.com/24


넘겨 받은 xml 데이타 중에 gzip 으로 압축후 base64 로 컨버팅된 string 값 데이타가 있어서,

이를 php 에서 디코딩 하는 방법을 찾아 보았다. 


우선, base64 를 디코딩..

이는 php 기본 제공 함수인 base64_decode 로 가능하다. 

$compressed = base64_decode($data);


다음은 이 gzip 으로 압축된 데이타를 풀어서 안에 내용을 봐야 하는데... 

이를 위해 php 에서 제공해 주는 함수가 여러개가 있긴 하다. 

gzinflate, gzuncompress, gzdecode 등등이 있는데, gzdedoce 는 php 5.4 이상버전만 있단다. ㅡㅡ

근데, 문제는 위 함수들을 적용을 시키는데 오류가 계속 발생한다. 

PHP Warning : gzinflate(): data error in ... 어쩌고 저쩌고..

PHP Warning : gzuncompress(): data error in ... 어쩌고 저쩌고..

이런 에러들.... 

위에서 base64_decode 시킨 값이 binary 가 아니어서 그런가 해서 string to binary 쪽으로 찾아 보았으니,
그쪽은 답이 아닌 것 같다. 

결국 gzuncompress(): data error in .. 쪽으로 검색해 보니,
wordpress 사이트에서 관련 문의사항들이 있어서 해당 소스를 찾아 봤다. 
wordpress 의 소스코드에서 보니, php 의 unzip 관련된 함수들을 묶어 놓은 함수를 만들어 쓰고 있었다. 


function decompress( $compressed, $length = null ) {

if ( empty($compressed) )
return $compressed;

if ( false !== ($decompressed = @gzinflate( $compressed ) ) )
return $decompressed;

if ( false !== ( $decompressed = @compatible_gzinflate( $compressed ) ) )
return $decompressed;

if ( false !== ( $decompressed = @gzuncompress( $compressed ) ) )
return $decompressed;

if ( function_exists('gzdecode') ) {
$decompressed = @gzdecode( $compressed );

if ( false !== $decompressed )
return $decompressed;
}

return $compressed;
}

function compatible_gzinflate($gzData) {

// Compressed data might contain a full header, if so strip it for gzinflate()
if ( substr($gzData, 0, 3) == "\x1f\x8b\x08" ) {
$i = 10;
$flg = ord( substr($gzData, 3, 1) );
if ( $flg > 0 ) {
if ( $flg & 4 ) {
list($xlen) = unpack('v', substr($gzData, $i, 2) );
$i = $i + 2 + $xlen;
}
if ( $flg & 8 )
$i = strpos($gzData, "\0", $i) + 1;
if ( $flg & 16 )
$i = strpos($gzData, "\0", $i) + 1;
if ( $flg & 2 )
$i = $i + 2;
}
$decompressed = @gzinflate( substr($gzData, $i, -8) );
if ( false !== $decompressed )
return $decompressed;
}

// Compressed data from java.util.zip.Deflater amongst others.
$decompressed = @gzinflate( substr($gzData, 2) );
if ( false !== $decompressed )
return $decompressed;

return false;
}


댓글
안내
궁금한 점을 댓글로 남겨주시면 답변해 드립니다.