aboutsummaryrefslogtreecommitdiff
path: root/zipstream.php
blob: ec3c407c71e4d98ab2cfed4548e62f3609f6d47e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php

#
# ZipStream - Streamed, dynamically generated zip archives.
# Paul Duncan <pabs@pablotron.org>
#
# Usage:
#
#   # create a new zip stream object
#   $zs = new ZipStream();
#
#   # list of local files
#   $files = array('foo.txt', 'bar.jpg');
#
#   # read and add each file to the archive
#   foreach ($files as $path)
#     $zs->add_file($path, file_get_contents($path));
# 
#   # write archive footer to stream
#   $zs->finish();
#
class ZipStream {
  var $files = array(),
      $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
  #
  # Parameters:
  #   
  #  $name - path of file in archive (including directory).
  #  $data - contents of file
  #  $time - last-modified timestamp of file (optional)
  #
  # Example:
  #
  #   $data = file_get_contents('foo.txt');
  #   $zs->add_file('foo.txt', $data);
  # 
  function add_file($name, $data, $time = 0) {
    # compress data
    # $zdata = substr(gzcompress($data), 2, -4);
    $zdata = gzdeflate($data);

    # calculate header attributes
    $crc  = crc32($data);
    $zlen = strlen($zdata);
    $nlen = strlen($name);
    $len  = strlen($data);

    # TODO: create unix timestamp

    # build file header
    $fields = array(              # (from V.A of APPNOTE.TXT)
      array('V', 0x04034b50),     # local file header signature
      array('v', 0x14),           # version needed to extract
      array('v', 0x00),           # general purpose bit flag
      array('v', 0x08),           # compresion method (deflate)
      array('v', 0x00),           # file mod time (dos) FIXME
      array('v', 0x00),           # file mod date (dos) FIXME
      array('V', $crc),           # crc32 of data
      array('V', $zlen),          # compressed data length
      array('V', $len),           # uncompressed data length
      array('v', $nlen),          # filename length
      array('v', 0),              # extra data len
    );

    # pack fields
    $ret = $this->pack_fields($fields) . $name . $zdata;

    # add to central directory record and increment offset
    $this->add_to_cdr($name, $time, $crc, $zlen, $len, strlen($ret));

    # print data
    $this->send($ret);
  }

  #
  # finish - Write zip footer to stream.
  #
  # Example:
  #
  #   # add a list of files to the archive
  #   $files = array('foo.txt', 'bar.jpg');
  #   foreach ($files as $path)
  #     $zs->add_file($path, file_get_contents($path));
  # 
  #   # write footer to stream
  #   $zs->finish();
  # 
  function finish() {
    $this->add_cdr();
    $this->clear();
  }

  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 #
  ###################

  function add_to_cdr($name, $time, $crc, $zlen, $len, $rec_len) {
    $this->files[] = array($name, $time, $crc, $zlen, $len, $this->ofs);
    $this->ofs += $rec_len;
  }

  function add_cdr_file($args) {
    list ($name, $time, $crc, $zlen, $len, $ofs) = $args;

    $fields = array(              # (from V,F of APPNOTE.TXT)
      array('V', 0x02014b50),     # central file header signature
      array('v', 0x00),           # version made by
      array('v', 0x14),           # version needed to extract
      array('v', 0x00),           # general purpose bit flag
      array('v', 0x08),           # compresion method (deflate)
      array('v', 0x00),           # file mod time (dos) FIXME
      array('v', 0x00),           # file mod date (dos) FIXME
      array('V', $crc),           # crc32 of data
      array('V', $zlen),          # compressed data length
      array('V', $len),           # uncompressed data length
      array('v', $nlen),          # filename length
      array('v', 0),              # extra data len
      array('v', 0),              # file comment length
      array('v', 0),              # disk number start
      array('v', 0),              # internal file attributes
      array('V', 32),             # external file attributes
      array('V', $ofs),           # relative offset of local header
    );

    # pack fields and append name
    $ret = $this->pack_fields($fields) . $name;

    $this->send($ret);

    # increment cdr offset
    $this->cdr_ofs += strlen($ret);
  }

  function add_cdr_eof() {
    $num = count($this->files);
    $cdr_len = $this->cdr_ofs;
    $cdr_ofs = $this->ofs;

    $fields = array(              # (from V,F of APPNOTE.TXT)
      array('V', 0x06054b50),     # end of central file header signature
      array('v', 0x00),           # this disk number
      array('v', 0x00),           # number of disk with cdr
      array('v', $num),           # number of entries in the cdr on this disk
      array('v', $num),           # number of entries in the cdr
      array('V', $cdr_len),       # cdr size
      array('V', $cdr_ofs),       # cdr ofs
      array('v', 0x00),           # zip file comment length
    );

    $ret = $this->pack_fields($fields);
    $this->send($ret);
  }

  function add_cdr() {
    foreach ($this->files as $file)
      $this->add_cdr_file($file);
    $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) {
    list ($fmt, $args) = array('', array());

    # populate format string and argument list
    foreach ($fields as $field) {
      $fmt .= $field[0];
      $args[] = $field[1];
    }

    # prepend format string to argument list
    array_unshift($args, $fmt);

    # build output string from header and compressed data
    return call_user_func_array('pack', $args);
  }
};

?>