Skip to content

Instantly share code, notes, and snippets.

@ajgarlag
Created June 24, 2015 12:26
Show Gist options
  • Save ajgarlag/3b6f287c3010cf66c578 to your computer and use it in GitHub Desktop.
Save ajgarlag/3b6f287c3010cf66c578 to your computer and use it in GitHub Desktop.
Gzip seekable stream with zlib.deflate filter
<?php
$fp = fopen("/tmp/example", "rb+");
$fz = fopen("php://temp", "wb+");
fwrite($fz, "\x1f\x8b\x08\0\0\0\0\0\0\xFF", 10);
$fltr = stream_filter_append($fz, "zlib.deflate", STREAM_FILTER_WRITE, -1);
$size = stream_copy_to_stream($fp, $fz);
stream_filter_remove($fltr);
rewind($fp);
$hash = hash_init('crc32b');
hash_update_stream($hash, $fp);
$crc32 = hash_final($hash, TRUE);
fwrite($fz, $crc32[3].$crc32[2].$crc32[1].$crc32[0], 4);
fwrite($fz, pack('V', $size), 4);
rewind($fz);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment