zlib - Cut string and gunzip using PHP -
i have router config export file, contains header of 20 bytes, followed zlib compressed data. once uncompressed should contain plain xml content.
my code strips first 20 bytes, , decompresses file. exported data still binary. used file_get_contents , file_put_contents first, assumed (wrongly) wasn't binary-safe. i've tried change 20 in 1-1000 without avail.
<? $fp_orig = fopen('config.cfg', "rb"); $data_orig = fread($fp_orig, filesize('config.cfg')); fclose($fp_orig); $bytes = 20; // tried 1-1000 $data_gz = substr($data_orig,$bytes); $fp_gz = fopen('config.cfg.gz', 'w'); fwrite($fp_gz, $data_gz); fclose($fp_gz); $fp_gz = gzopen('config.cfg.gz', 'rb'); $fp_xml = fopen('config.cfg.xml', 'wb'); while(!gzeof($fp_gz)) { fwrite($fp_xml, gzread($fp_gz, 4096)); } fclose($fp_xml); gzclose($fp_gz); echo file_get_contents('config.cfg.xml'); // gives binary data ?>
i'm not particularly looking turnkey working code, rather push right direction.
Comments
Post a Comment