I’m wondering what the PHP equivalent to this code is:
require "zlib"
foo = Zlib::Deflate.new()
bar = "bar"
puts foo.deflate(bar, Zlib::SYNC_FLUSH).unpack('H*')
puts foo.deflate(bar, Zlib::SYNC_FLUSH).unpack('H*')
puts foo.deflate(bar, Zlib::SYNC_FLUSH).unpack('H*')
foo.close()
Output:
789c4a4a2c02000000ffff
4a4a2c02000000ffff
022200000000ffff
I have tried using gzdeflate(), gzcompress(), a compress.zlib:// stream and more, but can’t find the correct equivalent.
Edit:
It should be noted that the use-case for this problem is a persistent Zlib stream for a TCP connection, not for simple web page compression. In other words, successive calls to the deflate should be using the same Zlib stream. PHP currently allows this, but provides no method for manually flushing (e.g. SYNC_FLUSH), and will wait to flush until the stream is closed. This is not acceptable for my use-case, as I need to be able to flush at any given point to send the compressed data up to that point to the remote client.
Edit #2:
Please see also this previous question for a more in-depth analysis into why PHP doesn’t allow SYNC_FLUSH. I’m looking for a hack or workaround to this, until PHP is eventually patched to address it (unfortunately, if it is patched, it will likely be in the 5.4 branch, which I won’t be able to use for quite a long time, making the workaround that much more important).
Well, there is a PECL
httpextension especially for your needs. 🙂Hope that helps. Manual: http://php.net/manual/en/class.httpdeflatestream.php