From 8c37b04bacfdaaaedc736c1f949cdec0c5fa0e26 Mon Sep 17 00:00:00 2001 From: pabs Date: Wed, 22 Aug 2007 22:39:07 -0400 Subject: Added send_http_headers and constructor. --- zipstream.php | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/zipstream.php b/zipstream.php index 03fb96b..6a7b374 100644 --- a/zipstream.php +++ b/zipstream.php @@ -24,6 +24,11 @@ class ZipStream { $cdr_ofs = 0, $ofs = 0; + function ZipStream($name = 'untitled.zip', $send_http_headers = false) { + $this->output_name = $name; + $this->need_headers = $send_http_headers; + } + # # add_file - add a file to the archive # @@ -73,7 +78,7 @@ class ZipStream { $this->add_to_cdr($name, $time, $crc, $zlen, $len, $full_len); # print data - echo $ret; + $this->send($ret); } # @@ -94,12 +99,18 @@ class ZipStream { $this->clear(); } - function clear() { - $this->files = array(0; - $this->ofs = 0; - $this->cdr_ofs = 0; - } + function send_http_headers($name = 'untitled.zip') { + $headers = array( + 'Content-Type' => 'application/x-zip', + 'Content-Disposition' => "attachment; filename=\"{$name}\"", + 'Pragma' => 'public', + 'Cache-Control' => 'public, must-revalidate', + 'Content-Transfer-Encoding' => 'binary', + ); + foreach ($headers as $key => $val) + header("$key: $val"); + } ################### # PRIVATE METHODS # @@ -135,6 +146,8 @@ class ZipStream { # pack fields and append name $ret = $this->pack_fields($fields) . $name; + $this->send($ret); + # increment cdr offset $this->cdr_ofs += strlen($ret); } @@ -162,6 +175,20 @@ class ZipStream { $this->add_cdr_eof(); } + function clear() { + $this->files = array(); + $this->ofs = 0; + $this->cdr_ofs = 0; + } + + function send($str) { + if ($this->need_headers) + $this->send_http_headers($this->output_name); + $this->need_headers = false; + + echo $str; + } + function pack_fields($fields) { # populate format string and argument list list ($pack_fmt, $args) = array('', array()); -- cgit v1.2.3