Move if from ext/ to cpan/
[p5sagit/p5-mst-13.2.git] / ext / IO-Compress / lib / IO / Compress / Zip.pm
CommitLineData
25f0751f 1package IO::Compress::Zip ;
2
3use strict ;
4use warnings;
5use bytes;
6
10c2b2bb 7use IO::Compress::Base::Common 2.021 qw(:Status createSelfTiedObject);
8use IO::Compress::RawDeflate 2.021 ;
9use IO::Compress::Adapter::Deflate 2.021 ;
10use IO::Compress::Adapter::Identity 2.021 ;
11use IO::Compress::Zlib::Extra 2.021 ;
12use IO::Compress::Zip::Constants 2.021 ;
c70c1701 13
25f0751f 14
10c2b2bb 15use Compress::Raw::Zlib 2.021 qw(crc32) ;
2b4e0969 16BEGIN
17{
b0cda13f 18 eval { require IO::Compress::Adapter::Bzip2 ;
10c2b2bb 19 import IO::Compress::Adapter::Bzip2 2.021 ;
b0cda13f 20 require IO::Compress::Bzip2 ;
10c2b2bb 21 import IO::Compress::Bzip2 2.021 ;
22 } ;
23 eval { require IO::Compress::Adapter::Lzma ;
24 import IO::Compress::Adapter::Lzma 2.020 ;
25 require IO::Compress::Lzma ;
26 import IO::Compress::Lzma 2.020 ;
2b4e0969 27 } ;
28}
29
30
25f0751f 31require Exporter ;
32
33our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $ZipError);
34
10c2b2bb 35$VERSION = '2.021';
25f0751f 36$ZipError = '';
37
38@ISA = qw(Exporter IO::Compress::RawDeflate);
39@EXPORT_OK = qw( $ZipError zip ) ;
40%EXPORT_TAGS = %IO::Compress::RawDeflate::DEFLATE_CONSTANTS ;
41push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
2b4e0969 42
10c2b2bb 43$EXPORT_TAGS{zip_method} = [qw( ZIP_CM_STORE ZIP_CM_DEFLATE ZIP_CM_BZIP2 ZIP_CM_LZMA)];
2b4e0969 44push @{ $EXPORT_TAGS{all} }, @{ $EXPORT_TAGS{zip_method} };
45
25f0751f 46Exporter::export_ok_tags('all');
47
25f0751f 48sub new
49{
50 my $class = shift ;
51
52 my $obj = createSelfTiedObject($class, \$ZipError);
53 $obj->_create(undef, @_);
54}
55
56sub zip
57{
58 my $obj = createSelfTiedObject(undef, \$ZipError);
59 return $obj->_def(@_);
60}
61
62sub mkComp
63{
64 my $self = shift ;
25f0751f 65 my $got = shift ;
66
67 my ($obj, $errstr, $errno) ;
68
2b4e0969 69 if (*$self->{ZipData}{Method} == ZIP_CM_STORE) {
25f0751f 70 ($obj, $errstr, $errno) = IO::Compress::Adapter::Identity::mkCompObject(
25f0751f 71 $got->value('Level'),
72 $got->value('Strategy')
73 );
ea6efd2c 74 *$self->{ZipData}{CRC32} = crc32(undef);
25f0751f 75 }
2b4e0969 76 elsif (*$self->{ZipData}{Method} == ZIP_CM_DEFLATE) {
25f0751f 77 ($obj, $errstr, $errno) = IO::Compress::Adapter::Deflate::mkCompObject(
78 $got->value('CRC32'),
79 $got->value('Adler32'),
80 $got->value('Level'),
81 $got->value('Strategy')
82 );
83 }
2b4e0969 84 elsif (*$self->{ZipData}{Method} == ZIP_CM_BZIP2) {
85 ($obj, $errstr, $errno) = IO::Compress::Adapter::Bzip2::mkCompObject(
86 $got->value('BlockSize100K'),
87 $got->value('WorkFactor'),
88 $got->value('Verbosity')
89 );
90 *$self->{ZipData}{CRC32} = crc32(undef);
91 }
10c2b2bb 92 elsif (*$self->{ZipData}{Method} == ZIP_CM_LZMA) {
93 ($obj, $errstr, $errno) = IO::Compress::Adapter::Lzma::mkCompObject();
94 *$self->{ZipData}{CRC32} = crc32(undef);
95 }
25f0751f 96
97 return $self->saveErrorString(undef, $errstr, $errno)
98 if ! defined $obj;
99
10c2b2bb 100 if (! defined *$self->{ZipData}{SizesOffset}) {
101 *$self->{ZipData}{SizesOffset} = 0;
e7d45986 102 *$self->{ZipData}{Offset} = new U64 ;
25f0751f 103 }
104
10c2b2bb 105 *$self->{ZipData}{AnyZip64} = 0
106 if ! defined *$self->{ZipData}{AnyZip64} ;
107
25f0751f 108 return $obj;
109}
110
2b4e0969 111sub reset
112{
113 my $self = shift ;
114
115 *$self->{Compress}->reset();
116 *$self->{ZipData}{CRC32} = Compress::Raw::Zlib::crc32('');
117
118 return STATUS_OK;
119}
120
121sub filterUncompressed
122{
123 my $self = shift ;
25f0751f 124
2b4e0969 125 if (*$self->{ZipData}{Method} == ZIP_CM_DEFLATE) {
126 *$self->{ZipData}{CRC32} = *$self->{Compress}->crc32();
127 }
128 else {
129 *$self->{ZipData}{CRC32} = crc32(${$_[0]}, *$self->{ZipData}{CRC32});
130
131 }
132}
25f0751f 133
134sub mkHeader
135{
136 my $self = shift;
137 my $param = shift ;
138
10c2b2bb 139
140 *$self->{ZipData}{LocalHdrOffset} = U64::clone(*$self->{ZipData}{Offset});
c70c1701 141
25f0751f 142 my $filename = '';
143 $filename = $param->value('Name') || '';
144
145 my $comment = '';
146 $comment = $param->value('Comment') || '';
147
25f0751f 148 my $hdr = '';
149
150 my $time = _unixToDosTime($param->value('Time'));
25f0751f 151
c70c1701 152 my $extra = '';
153 my $ctlExtra = '';
e7d45986 154 my $empty = 0;
4e7676c7 155 my $osCode = $param->value('OS_Code') ;
156 my $extFileAttr = 0 ;
d54256af 157
158 # This code assumes Unix.
159 $extFileAttr = 0666 << 16
160 if $osCode == ZIP_OS_CODE_UNIX ;
e7d45986 161
162 if (*$self->{ZipData}{Zip64}) {
319fab50 163 $empty = 0xFFFFFFFF;
e7d45986 164
165 my $x = '';
166 $x .= pack "V V", 0, 0 ; # uncompressedLength
167 $x .= pack "V V", 0, 0 ; # compressedLength
10c2b2bb 168 $extra .= IO::Compress::Zlib::Extra::mkSubField(ZIP_EXTRA_ID_ZIP64, $x);
e7d45986 169 }
c70c1701 170
171 if (! $param->value('Minimal')) {
172 if (defined $param->value('exTime'))
173 {
174 $extra .= mkExtendedTime($param->value('MTime'),
175 $param->value('ATime'),
176 $param->value('CTime'));
177
178 $ctlExtra .= mkExtendedTime($param->value('MTime'));
179 }
180
4e7676c7 181 if ( $param->value('UID') && $osCode == ZIP_OS_CODE_UNIX)
182 {
183 $extra .= mkUnix2Extra( $param->value('UID'), $param->value('GID'));
184 $ctlExtra .= mkUnix2Extra();
185 }
186
4e7676c7 187 $extFileAttr = $param->value('ExtAttr')
188 if defined $param->value('ExtAttr') ;
c70c1701 189
190 $extra .= $param->value('ExtraFieldLocal')
191 if defined $param->value('ExtraFieldLocal');
192
193 $ctlExtra .= $param->value('ExtraFieldCentral')
194 if defined $param->value('ExtraFieldCentral');
195 }
196
c70c1701 197 my $gpFlag = 0 ;
198 $gpFlag |= ZIP_GP_FLAG_STREAMING_MASK
199 if *$self->{ZipData}{Stream} ;
200
2b4e0969 201 my $method = *$self->{ZipData}{Method} ;
202
e7d45986 203 my $version = $ZIP_CM_MIN_VERSIONS{$method};
204 $version = ZIP64_MIN_VERSION
205 if ZIP64_MIN_VERSION > $version && *$self->{ZipData}{Zip64};
206 my $madeBy = ($param->value('OS_Code') << 8) + $version;
207 my $extract = $version;
208
209 *$self->{ZipData}{Version} = $version;
210 *$self->{ZipData}{MadeBy} = $madeBy;
c70c1701 211
212 my $ifa = 0;
213 $ifa |= ZIP_IFA_TEXT_MASK
214 if $param->value('TextFlag');
25f0751f 215
2b4e0969 216 $hdr .= pack "V", ZIP_LOCAL_HDR_SIG ; # signature
25f0751f 217 $hdr .= pack 'v', $extract ; # extract Version & OS
c70c1701 218 $hdr .= pack 'v', $gpFlag ; # general purpose flag (set streaming mode)
25f0751f 219 $hdr .= pack 'v', $method ; # compression method (deflate)
220 $hdr .= pack 'V', $time ; # last mod date/time
221 $hdr .= pack 'V', 0 ; # crc32 - 0 when streaming
e7d45986 222 $hdr .= pack 'V', $empty ; # compressed length - 0 when streaming
223 $hdr .= pack 'V', $empty ; # uncompressed length - 0 when streaming
25f0751f 224 $hdr .= pack 'v', length $filename ; # filename length
c70c1701 225 $hdr .= pack 'v', length $extra ; # extra length
25f0751f 226
227 $hdr .= $filename ;
10c2b2bb 228
229 # Remember the offset for the compressed & uncompressed lengths in the
230 # local header.
231 if (*$self->{ZipData}{Zip64}) {
232 *$self->{ZipData}{SizesOffset} = *$self->{ZipData}{Offset}->get64bit()
233 + length($hdr) + 4 ;
234 }
235 else {
236 *$self->{ZipData}{SizesOffset} = *$self->{ZipData}{Offset}->get64bit()
237 + 18;
238 }
239
c70c1701 240 $hdr .= $extra ;
25f0751f 241
242
243 my $ctl = '';
244
2b4e0969 245 $ctl .= pack "V", ZIP_CENTRAL_HDR_SIG ; # signature
c70c1701 246 $ctl .= pack 'v', $madeBy ; # version made by
25f0751f 247 $ctl .= pack 'v', $extract ; # extract Version
c70c1701 248 $ctl .= pack 'v', $gpFlag ; # general purpose flag (streaming mode)
25f0751f 249 $ctl .= pack 'v', $method ; # compression method (deflate)
250 $ctl .= pack 'V', $time ; # last mod date/time
251 $ctl .= pack 'V', 0 ; # crc32
e7d45986 252 $ctl .= pack 'V', $empty ; # compressed length
253 $ctl .= pack 'V', $empty ; # uncompressed length
25f0751f 254 $ctl .= pack 'v', length $filename ; # filename length
10c2b2bb 255
256 *$self->{ZipData}{ExtraOffset} = length $ctl;
257 *$self->{ZipData}{ExtraSize} = length $ctlExtra ;
258
c70c1701 259 $ctl .= pack 'v', length $ctlExtra ; # extra length
25f0751f 260 $ctl .= pack 'v', length $comment ; # file comment length
261 $ctl .= pack 'v', 0 ; # disk number start
c70c1701 262 $ctl .= pack 'v', $ifa ; # internal file attributes
4e7676c7 263 $ctl .= pack 'V', $extFileAttr ; # external file attributes
10c2b2bb 264
265 # offset to local hdr
266 if (*$self->{ZipData}{LocalHdrOffset}->is64bit() ) {
267 $ctl .= pack 'V', 0xFFFFFFFF ;
e7d45986 268 }
269 else {
10c2b2bb 270 $ctl .= *$self->{ZipData}{LocalHdrOffset}->getPacked_V32() ;
e7d45986 271 }
25f0751f 272
273 $ctl .= $filename ;
c70c1701 274 $ctl .= $ctlExtra ;
25f0751f 275 $ctl .= $comment ;
276
e7d45986 277 *$self->{ZipData}{Offset}->add(length $hdr) ;
25f0751f 278
279 *$self->{ZipData}{CentralHeader} = $ctl;
280
281 return $hdr;
282}
283
284sub mkTrailer
285{
286 my $self = shift ;
287
2b4e0969 288 my $crc32 ;
289 if (*$self->{ZipData}{Method} == ZIP_CM_DEFLATE) {
e7d45986 290 $crc32 = pack "V", *$self->{Compress}->crc32();
2b4e0969 291 }
292 else {
e7d45986 293 $crc32 = pack "V", *$self->{ZipData}{CRC32};
2b4e0969 294 }
295
e7d45986 296 my $ctl = *$self->{ZipData}{CentralHeader} ;
297
298 my $sizes ;
299 if (! *$self->{ZipData}{Zip64}) {
300 $sizes .= *$self->{CompSize}->getPacked_V32() ; # Compressed size
301 $sizes .= *$self->{UnCompSize}->getPacked_V32() ; # Uncompressed size
302 }
303 else {
304 $sizes .= *$self->{CompSize}->getPacked_V64() ; # Compressed size
305 $sizes .= *$self->{UnCompSize}->getPacked_V64() ; # Uncompressed size
306 }
307
308 my $data = $crc32 . $sizes ;
25f0751f 309
10c2b2bb 310 my $xtrasize = *$self->{UnCompSize}->getPacked_V64() ; # Uncompressed size
311 $xtrasize .= *$self->{CompSize}->getPacked_V64() ; # Compressed size
25f0751f 312
313 my $hdr = '';
314
315 if (*$self->{ZipData}{Stream}) {
2b4e0969 316 $hdr = pack "V", ZIP_DATA_HDR_SIG ; # signature
25f0751f 317 $hdr .= $data ;
318 }
319 else {
10c2b2bb 320 $self->writeAt(*$self->{ZipData}{LocalHdrOffset}->get64bit() + 14, $crc32)
321 or return undef;
322 $self->writeAt(*$self->{ZipData}{SizesOffset},
323 *$self->{ZipData}{Zip64} ? $xtrasize : $sizes)
25f0751f 324 or return undef;
325 }
326
10c2b2bb 327 # Central Header Record/Zip64 extended field
328
329 substr($ctl, 16, length $crc32) = $crc32 ;
330
331 my $x = '';
332
333 # uncompressed length
334 if (*$self->{UnCompSize}->is64bit() ) {
335 $x .= *$self->{UnCompSize}->getPacked_V64() ;
336 } else {
337 substr($ctl, 24, 4) = *$self->{UnCompSize}->getPacked_V32() ;
338 }
339
340 # compressed length
341 if (*$self->{CompSize}->is64bit() ) {
342 $x .= *$self->{CompSize}->getPacked_V64() ;
343 } else {
344 substr($ctl, 20, 4) = *$self->{CompSize}->getPacked_V32() ;
345 }
346
347 # Local Header offset
348 $x .= *$self->{ZipData}{LocalHdrOffset}->getPacked_V64()
349 if *$self->{ZipData}{LocalHdrOffset}->is64bit() ;
350
351 # disk no - always zero, so don't need it
352 #$x .= pack "V", 0 ;
353
354 if (length $x) {
355 my $xtra = IO::Compress::Zlib::Extra::mkSubField(ZIP_EXTRA_ID_ZIP64, $x);
356 $ctl .= $xtra ;
357 substr($ctl, *$self->{ZipData}{ExtraOffset}, 2) =
358 pack 'v', *$self->{ZipData}{ExtraSize} + length $xtra;
359
360 *$self->{ZipData}{AnyZip64} = 1;
e7d45986 361 }
25f0751f 362
e7d45986 363 *$self->{ZipData}{Offset}->add(length($hdr));
364 *$self->{ZipData}{Offset}->add( *$self->{CompSize} );
25f0751f 365 push @{ *$self->{ZipData}{CentralDir} }, $ctl ;
366
367 return $hdr;
368}
369
370sub mkFinalTrailer
371{
372 my $self = shift ;
373
374 my $comment = '';
375 $comment = *$self->{ZipData}{ZipComment} ;
376
e7d45986 377 my $cd_offset = *$self->{ZipData}{Offset}->get32bit() ; # offset to start central dir
378
25f0751f 379 my $entries = @{ *$self->{ZipData}{CentralDir} };
380 my $cd = join '', @{ *$self->{ZipData}{CentralDir} };
e7d45986 381 my $cd_len = length $cd ;
382
383 my $z64e = '';
384
10c2b2bb 385 if ( *$self->{ZipData}{AnyZip64} ) {
e7d45986 386
387 my $v = *$self->{ZipData}{Version} ;
388 my $mb = *$self->{ZipData}{MadeBy} ;
10c2b2bb 389 $z64e .= pack 'v', $mb ; # Version made by
390 $z64e .= pack 'v', $v ; # Version to extract
e7d45986 391 $z64e .= pack 'V', 0 ; # number of disk
392 $z64e .= pack 'V', 0 ; # number of disk with central dir
393 $z64e .= U64::pack_V64 $entries ; # entries in central dir on this disk
394 $z64e .= U64::pack_V64 $entries ; # entries in central dir
395 $z64e .= U64::pack_V64 $cd_len ; # size of central dir
396 $z64e .= *$self->{ZipData}{Offset}->getPacked_V64() ; # offset to start central dir
397
398 $z64e = pack("V", ZIP64_END_CENTRAL_REC_HDR_SIG) # signature
399 . U64::pack_V64(length $z64e)
400 . $z64e ;
401
402 *$self->{ZipData}{Offset}->add(length $cd) ;
403
404 $z64e .= pack "V", ZIP64_END_CENTRAL_LOC_HDR_SIG; # signature
405 $z64e .= pack 'V', 0 ; # number of disk with central dir
406 $z64e .= *$self->{ZipData}{Offset}->getPacked_V64() ; # offset to end zip64 central dir
407 $z64e .= pack 'V', 1 ; # Total number of disks
408
10c2b2bb 409 $cd_offset = 0xFFFFFFFF ;
410 $cd_len = 0xFFFFFFFF if $cd_len >= 0xFFFFFFFF ;
411 $entries = 0xFFFF if $entries >= 0xFFFF ;
e7d45986 412 }
25f0751f 413
414 my $ecd = '';
2b4e0969 415 $ecd .= pack "V", ZIP_END_CENTRAL_HDR_SIG ; # signature
25f0751f 416 $ecd .= pack 'v', 0 ; # number of disk
e7d45986 417 $ecd .= pack 'v', 0 ; # number of disk with central dir
25f0751f 418 $ecd .= pack 'v', $entries ; # entries in central dir on this disk
419 $ecd .= pack 'v', $entries ; # entries in central dir
e7d45986 420 $ecd .= pack 'V', $cd_len ; # size of central dir
421 $ecd .= pack 'V', $cd_offset ; # offset to start central dir
25f0751f 422 $ecd .= pack 'v', length $comment ; # zipfile comment length
423 $ecd .= $comment;
424
e7d45986 425 return $cd . $z64e . $ecd ;
25f0751f 426}
427
428sub ckParams
429{
430 my $self = shift ;
431 my $got = shift;
432
433 $got->value('CRC32' => 1);
434
435 if (! $got->parsed('Time') ) {
436 # Modification time defaults to now.
437 $got->value('Time' => time) ;
438 }
439
ea6efd2c 440 if ($got->parsed('exTime') ) {
c70c1701 441 my $timeRef = $got->value('exTime');
442 if ( defined $timeRef) {
443 return $self->saveErrorString(undef, "exTime not a 3-element array ref")
444 if ref $timeRef ne 'ARRAY' || @$timeRef != 3;
445 }
446
447 $got->value("MTime", $timeRef->[1]);
448 $got->value("ATime", $timeRef->[0]);
449 $got->value("CTime", $timeRef->[2]);
450 }
d54256af 451
452 # Unix2 Extended Attribute
ea6efd2c 453 if ($got->parsed('exUnix2') ) {
d54256af 454 my $timeRef = $got->value('exUnix2');
455 if ( defined $timeRef) {
456 return $self->saveErrorString(undef, "exUnix2 not a 2-element array ref")
457 if ref $timeRef ne 'ARRAY' || @$timeRef != 2;
458 }
459
460 $got->value("UID", $timeRef->[0]);
461 $got->value("GID", $timeRef->[1]);
462 }
c70c1701 463
10c2b2bb 464 *$self->{ZipData}{AnyZip64} = 1
465 if $got->value('Zip64');
e7d45986 466 *$self->{ZipData}{Zip64} = $got->value('Zip64');
25f0751f 467 *$self->{ZipData}{Stream} = $got->value('Stream');
2b4e0969 468
469 my $method = $got->value('Method');
2b4e0969 470 return $self->saveErrorString(undef, "Unknown Method '$method'")
471 if ! defined $ZIP_CM_MIN_VERSIONS{$method};
472
473 return $self->saveErrorString(undef, "Bzip2 not available")
474 if $method == ZIP_CM_BZIP2 and
475 ! defined $IO::Compress::Adapter::Bzip2::VERSION;
476
10c2b2bb 477 return $self->saveErrorString(undef, "Lzma not available")
478 if $method == ZIP_CM_LZMA and
479 ! defined $IO::Compress::Adapter::Lzma::VERSION;
319fab50 480
2b4e0969 481 *$self->{ZipData}{Method} = $method;
482
25f0751f 483 *$self->{ZipData}{ZipComment} = $got->value('ZipComment') ;
484
c70c1701 485 for my $name (qw( ExtraFieldLocal ExtraFieldCentral ))
486 {
487 my $data = $got->value($name) ;
488 if (defined $data) {
489 my $bad = IO::Compress::Zlib::Extra::parseExtraField($data, 1, 0) ;
490 return $self->saveErrorString(undef, "Error with $name Parameter: $bad")
491 if $bad ;
492
493 $got->value($name, $data) ;
494 }
495 }
496
2b4e0969 497 return undef
498 if defined $IO::Compress::Bzip2::VERSION
499 and ! IO::Compress::Bzip2::ckParams($self, $got);
25f0751f 500
501 return 1 ;
502}
503
504#sub newHeader
505#{
506# my $self = shift ;
507#
508# return $self->mkHeader(*$self->{Got});
509#}
510
511sub getExtraParams
512{
513 my $self = shift ;
514
10c2b2bb 515 use IO::Compress::Base::Common 2.021 qw(:Parse);
516 use Compress::Raw::Zlib 2.021 qw(Z_DEFLATED Z_DEFAULT_COMPRESSION Z_DEFAULT_STRATEGY);
25f0751f 517
2b4e0969 518 my @Bzip2 = ();
519
520 @Bzip2 = IO::Compress::Bzip2::getExtraParams($self)
521 if defined $IO::Compress::Bzip2::VERSION;
25f0751f 522
523 return (
524 # zlib behaviour
525 $self->getZlibParams(),
526
527 'Stream' => [1, 1, Parse_boolean, 1],
2b4e0969 528 #'Store' => [0, 1, Parse_boolean, 0],
529 'Method' => [0, 1, Parse_unsigned, ZIP_CM_DEFLATE],
25f0751f 530
531# # Zip header fields
c70c1701 532 'Minimal' => [0, 1, Parse_boolean, 0],
e7d45986 533 'Zip64' => [0, 1, Parse_boolean, 0],
25f0751f 534 'Comment' => [0, 1, Parse_any, ''],
535 'ZipComment'=> [0, 1, Parse_any, ''],
536 'Name' => [0, 1, Parse_any, ''],
537 'Time' => [0, 1, Parse_any, undef],
c70c1701 538 'exTime' => [0, 1, Parse_any, undef],
d54256af 539 'exUnix2' => [0, 1, Parse_any, undef],
4e7676c7 540 'ExtAttr' => [0, 1, Parse_any, 0],
25f0751f 541 'OS_Code' => [0, 1, Parse_unsigned, $Compress::Raw::Zlib::gzip_os_code],
542
c70c1701 543 'TextFlag' => [0, 1, Parse_boolean, 0],
544 'ExtraFieldLocal' => [0, 1, Parse_any, undef],
545 'ExtraFieldCentral'=> [0, 1, Parse_any, undef],
2b4e0969 546
547 @Bzip2,
25f0751f 548 );
549}
550
551sub getInverseClass
552{
553 return ('IO::Uncompress::Unzip',
554 \$IO::Uncompress::Unzip::UnzipError);
555}
556
557sub getFileInfo
558{
559 my $self = shift ;
560 my $params = shift;
561 my $filename = shift ;
562
c70c1701 563 my ($mode, $uid, $gid, $atime, $mtime, $ctime)
564 = (stat($filename))[2, 4,5, 8,9,10] ;
25f0751f 565
566 $params->value('Name' => $filename)
567 if ! $params->parsed('Name') ;
568
c70c1701 569 $params->value('Time' => $mtime)
25f0751f 570 if ! $params->parsed('Time') ;
571
c70c1701 572 if ( ! $params->parsed('exTime'))
573 {
574 $params->value('MTime' => $mtime) ;
575 $params->value('ATime' => $atime) ;
4e7676c7 576 $params->value('CTime' => undef) ; # No Creation time
ea6efd2c 577 $params->value("exTime", [$mtime, $atime, undef]);
c70c1701 578 }
579
d54256af 580 # NOTE - Unix specific code alert
4e7676c7 581 $params->value('ExtAttr' => $mode << 16)
582 if ! $params->parsed('ExtAttr');
c70c1701 583
584 $params->value('UID' => $uid) ;
585 $params->value('GID' => $gid) ;
25f0751f 586
587}
588
c70c1701 589sub mkExtendedTime
590{
591 # order expected is m, a, c
592
593 my $times = '';
594 my $bit = 1 ;
595 my $flags = 0;
596
597 for my $time (@_)
598 {
599 if (defined $time)
600 {
601 $flags |= $bit;
602 $times .= pack("V", $time);
603 }
604
605 $bit <<= 1 ;
606 }
607
c70c1701 608 return IO::Compress::Zlib::Extra::mkSubField(ZIP_EXTRA_ID_EXT_TIMESTAMP,
609 pack("C", $flags) . $times);
610}
611
4e7676c7 612sub mkUnix2Extra
c70c1701 613{
614 my $ids = '';
615 for my $id (@_)
616 {
617 $ids .= pack("v", $id);
618 }
619
4e7676c7 620 return IO::Compress::Zlib::Extra::mkSubField(ZIP_EXTRA_ID_INFO_ZIP_UNIX2,
621 $ids);
c70c1701 622}
623
624
25f0751f 625# from Archive::Zip
626sub _unixToDosTime # Archive::Zip::Member
627{
628 my $time_t = shift;
629 # TODO - add something to cope with unix time < 1980
630 my ( $sec, $min, $hour, $mday, $mon, $year ) = localtime($time_t);
631 my $dt = 0;
632 $dt += ( $sec >> 1 );
633 $dt += ( $min << 5 );
634 $dt += ( $hour << 11 );
635 $dt += ( $mday << 16 );
636 $dt += ( ( $mon + 1 ) << 21 );
637 $dt += ( ( $year - 80 ) << 25 );
638 return $dt;
639}
640
6411;
642
643__END__
644
645=head1 NAME
646
cb7abd7f 647IO::Compress::Zip - Write zip files/buffers
648
25f0751f 649
650
651=head1 SYNOPSIS
652
653 use IO::Compress::Zip qw(zip $ZipError) ;
654
25f0751f 655 my $status = zip $input => $output [,OPTS]
656 or die "zip failed: $ZipError\n";
657
658 my $z = new IO::Compress::Zip $output [,OPTS]
659 or die "zip failed: $ZipError\n";
660
661 $z->print($string);
662 $z->printf($format, $string);
663 $z->write($string);
664 $z->syswrite($string [, $length, $offset]);
665 $z->flush();
666 $z->tell();
667 $z->eof();
668 $z->seek($position, $whence);
669 $z->binmode();
670 $z->fileno();
671 $z->opened();
672 $z->autoflush();
673 $z->input_line_number();
674 $z->newStream( [OPTS] );
675
676 $z->deflateParams();
677
678 $z->close() ;
679
680 $ZipError ;
681
682 # IO::File mode
683
684 print $z $string;
685 printf $z $format, $string;
686 tell $z
687 eof $z
688 seek $z, $position, $whence
689 binmode $z
690 fileno $z
691 close $z ;
692
693
694=head1 DESCRIPTION
695
25f0751f 696This module provides a Perl interface that allows writing zip
697compressed data to files or buffer.
698
258133d1 699The primary purpose of this module is to provide streaming write access to
700zip files and buffers. It is not a general-purpose file archiver. If that
701is what you want, check out C<Archive::Zip>.
702
4e7676c7 703At present three compression methods are supported by IO::Compress::Zip,
704namely Store (no compression at all), Deflate and Bzip2.
705
706Note that to create Bzip2 content, the module C<IO::Compress::Bzip2> must
707be installed.
708
25f0751f 709For reading zip files/buffers, see the companion module
710L<IO::Uncompress::Unzip|IO::Uncompress::Unzip>.
711
25f0751f 712=head1 Functional Interface
713
714A top-level function, C<zip>, is provided to carry out
715"one-shot" compression between buffers and/or files. For finer
716control over the compression process, see the L</"OO Interface">
717section.
718
719 use IO::Compress::Zip qw(zip $ZipError) ;
720
721 zip $input => $output [,OPTS]
722 or die "zip failed: $ZipError\n";
723
25f0751f 724The functional interface needs Perl5.005 or better.
725
25f0751f 726=head2 zip $input => $output [, OPTS]
727
25f0751f 728C<zip> expects at least two parameters, C<$input> and C<$output>.
729
730=head3 The C<$input> parameter
731
732The parameter, C<$input>, is used to define the source of
733the uncompressed data.
734
735It can take one of the following forms:
736
737=over 5
738
739=item A filename
740
741If the C<$input> parameter is a simple scalar, it is assumed to be a
742filename. This file will be opened for reading and the input data
743will be read from it.
744
745=item A filehandle
746
747If the C<$input> parameter is a filehandle, the input data will be
748read from it.
749The string '-' can be used as an alias for standard input.
750
751=item A scalar reference
752
753If C<$input> is a scalar reference, the input data will be read
754from C<$$input>.
755
756=item An array reference
757
758If C<$input> is an array reference, each element in the array must be a
759filename.
760
761The input data will be read from each file in turn.
762
763The complete array will be walked to ensure that it only
764contains valid filenames before any data is compressed.
765
25f0751f 766=item An Input FileGlob string
767
768If C<$input> is a string that is delimited by the characters "<" and ">"
769C<zip> will assume that it is an I<input fileglob string>. The
770input is the list of files that match the fileglob.
771
772If the fileglob does not match any files ...
773
774See L<File::GlobMapper|File::GlobMapper> for more details.
775
25f0751f 776=back
777
778If the C<$input> parameter is any other type, C<undef> will be returned.
779
25f0751f 780In addition, if C<$input> is a simple filename, the default values for
4e7676c7 781the C<Name>, C<Time>, C<ExtAttr> and C<exTime> options will be sourced from that file.
25f0751f 782
783If you do not want to use these defaults they can be overridden by
4e7676c7 784explicitly setting the C<Name>, C<Time>, C<ExtAttr> and C<exTime> options or by setting the
25f0751f 785C<Minimal> parameter.
786
25f0751f 787=head3 The C<$output> parameter
788
789The parameter C<$output> is used to control the destination of the
790compressed data. This parameter can take one of these forms.
791
792=over 5
793
794=item A filename
795
796If the C<$output> parameter is a simple scalar, it is assumed to be a
797filename. This file will be opened for writing and the compressed
798data will be written to it.
799
800=item A filehandle
801
802If the C<$output> parameter is a filehandle, the compressed data
803will be written to it.
804The string '-' can be used as an alias for standard output.
805
25f0751f 806=item A scalar reference
807
808If C<$output> is a scalar reference, the compressed data will be
809stored in C<$$output>.
810
25f0751f 811=item An Array Reference
812
813If C<$output> is an array reference, the compressed data will be
814pushed onto the array.
815
816=item An Output FileGlob
817
818If C<$output> is a string that is delimited by the characters "<" and ">"
819C<zip> will assume that it is an I<output fileglob string>. The
820output is the list of files that match the fileglob.
821
822When C<$output> is an fileglob string, C<$input> must also be a fileglob
823string. Anything else is an error.
824
825=back
826
827If the C<$output> parameter is any other type, C<undef> will be returned.
828
25f0751f 829=head2 Notes
830
831When C<$input> maps to multiple files/buffers and C<$output> is a single
c70c1701 832file/buffer the input files/buffers will each be stored
833in C<$output> as a distinct entry.
834
25f0751f 835=head2 Optional Parameters
836
837Unless specified below, the optional parameters for C<zip>,
838C<OPTS>, are the same as those used with the OO interface defined in the
839L</"Constructor Options"> section below.
840
841=over 5
842
e7d45986 843=item C<< AutoClose => 0|1 >>
25f0751f 844
845This option applies to any input or output data streams to
846C<zip> that are filehandles.
847
848If C<AutoClose> is specified, and the value is true, it will result in all
849input and/or output filehandles being closed once C<zip> has
850completed.
851
852This parameter defaults to 0.
853
e7d45986 854=item C<< BinModeIn => 0|1 >>
25f0751f 855
856When reading from a file or filehandle, set C<binmode> before reading.
857
858Defaults to 0.
859
e7d45986 860=item C<< Append => 0|1 >>
25f0751f 861
862TODO
863
25f0751f 864=back
865
25f0751f 866=head2 Examples
867
868To read the contents of the file C<file1.txt> and write the compressed
869data to the file C<file1.txt.zip>.
870
871 use strict ;
872 use warnings ;
873 use IO::Compress::Zip qw(zip $ZipError) ;
874
875 my $input = "file1.txt";
876 zip $input => "$input.zip"
877 or die "zip failed: $ZipError\n";
878
25f0751f 879To read from an existing Perl filehandle, C<$input>, and write the
880compressed data to a buffer, C<$buffer>.
881
882 use strict ;
883 use warnings ;
884 use IO::Compress::Zip qw(zip $ZipError) ;
885 use IO::File ;
886
887 my $input = new IO::File "<file1.txt"
888 or die "Cannot open 'file1.txt': $!\n" ;
889 my $buffer ;
890 zip $input => \$buffer
891 or die "zip failed: $ZipError\n";
892
893To compress all files in the directory "/my/home" that match "*.txt"
894and store the compressed data in the same directory
895
896 use strict ;
897 use warnings ;
898 use IO::Compress::Zip qw(zip $ZipError) ;
899
900 zip '</my/home/*.txt>' => '<*.zip>'
901 or die "zip failed: $ZipError\n";
902
903and if you want to compress each file one at a time, this will do the trick
904
905 use strict ;
906 use warnings ;
907 use IO::Compress::Zip qw(zip $ZipError) ;
908
909 for my $input ( glob "/my/home/*.txt" )
910 {
911 my $output = "$input.zip" ;
912 zip $input => $output
913 or die "Error compressing '$input': $ZipError\n";
914 }
915
25f0751f 916=head1 OO Interface
917
918=head2 Constructor
919
920The format of the constructor for C<IO::Compress::Zip> is shown below
921
922 my $z = new IO::Compress::Zip $output [,OPTS]
923 or die "IO::Compress::Zip failed: $ZipError\n";
924
925It returns an C<IO::Compress::Zip> object on success and undef on failure.
926The variable C<$ZipError> will contain an error message on failure.
927
928If you are running Perl 5.005 or better the object, C<$z>, returned from
929IO::Compress::Zip can be used exactly like an L<IO::File|IO::File> filehandle.
930This means that all normal output file operations can be carried out
931with C<$z>.
932For example, to write to a compressed file/buffer you can use either of
933these forms
934
935 $z->print("hello world\n");
936 print $z "hello world\n";
937
938The mandatory parameter C<$output> is used to control the destination
939of the compressed data. This parameter can take one of these forms.
940
941=over 5
942
943=item A filename
944
945If the C<$output> parameter is a simple scalar, it is assumed to be a
946filename. This file will be opened for writing and the compressed data
947will be written to it.
948
949=item A filehandle
950
951If the C<$output> parameter is a filehandle, the compressed data will be
952written to it.
953The string '-' can be used as an alias for standard output.
954
25f0751f 955=item A scalar reference
956
957If C<$output> is a scalar reference, the compressed data will be stored
958in C<$$output>.
959
960=back
961
962If the C<$output> parameter is any other type, C<IO::Compress::Zip>::new will
963return undef.
964
965=head2 Constructor Options
966
967C<OPTS> is any combination of the following options:
968
969=over 5
970
e7d45986 971=item C<< AutoClose => 0|1 >>
25f0751f 972
973This option is only valid when the C<$output> parameter is a filehandle. If
974specified, and the value is true, it will result in the C<$output> being
975closed once either the C<close> method is called or the C<IO::Compress::Zip>
976object is destroyed.
977
978This parameter defaults to 0.
979
e7d45986 980=item C<< Append => 0|1 >>
25f0751f 981
982Opens C<$output> in append mode.
983
984The behaviour of this option is dependent on the type of C<$output>.
985
986=over 5
987
988=item * A Buffer
989
990If C<$output> is a buffer and C<Append> is enabled, all compressed data
991will be append to the end if C<$output>. Otherwise C<$output> will be
992cleared before any data is written to it.
993
994=item * A Filename
995
996If C<$output> is a filename and C<Append> is enabled, the file will be
997opened in append mode. Otherwise the contents of the file, if any, will be
998truncated before any compressed data is written to it.
999
1000=item * A Filehandle
1001
1002If C<$output> is a filehandle, the file pointer will be positioned to the
1003end of the file via a call to C<seek> before any compressed data is written
1004to it. Otherwise the file pointer will not be moved.
1005
1006=back
1007
1008This parameter defaults to 0.
1009
e7d45986 1010=item C<< Name => $string >>
2b4e0969 1011
1012Stores the contents of C<$string> in the zip filename header field. If
1013C<Name> is not specified, no zip filename field will be created.
1014
e7d45986 1015=item C<< Time => $number >>
2b4e0969 1016
1017Sets the last modified time field in the zip header to $number.
1018
1019This field defaults to the time the C<IO::Compress::Zip> object was created
1020if this option is not specified.
1021
4e7676c7 1022=item C<< ExtAttr => $attr >>
1023
1024This option controls the "external file attributes" field in the central
1025header of the zip file. This is a 4 byte field.
1026
d54256af 1027If you are running a Unix derivative this value defaults to
1028
1029 0666 << 16
1030
1031This should allow read/write access to any files that are extracted from
1032the zip file/buffer.
1033
1034For all other systems it defaults to 0.
4e7676c7 1035
e7d45986 1036=item C<< exTime => [$atime, $mtime, $ctime] >>
c70c1701 1037
1038This option expects an array reference with exactly three elements:
1039C<$atime>, C<mtime> and C<$ctime>. These correspond to the last access
1040time, last modification time and creation time respectively.
1041
d54256af 1042It uses these values to set the extended timestamp field (ID is "UT") in
1043the local zip header using the three values, $atime, $mtime, $ctime. In
1044addition it sets the extended timestamp field in the central zip header
1045using C<$mtime>.
c70c1701 1046
4e7676c7 1047If any of the three values is C<undef> that time value will not be used.
1048So, for example, to set only the C<$mtime> you would use this
1049
1050 exTime => [undef, $mtime, undef]
1051
c70c1701 1052If the C<Minimal> option is set to true, this option will be ignored.
1053
1054By default no extended time field is created.
1055
d54256af 1056=item C<< exUnix2 => [$uid, $gid] >>
1057
1058This option expects an array reference with exactly two elements: C<$uid>
1059and C<$gid>. These values correspond to the numeric user ID and group ID
1060of the owner of the files respectively.
1061
1062When the C<exUnix2> option is present it will trigger the creation of a
1063Unix2 extra field (ID is "Ux") in the local zip. This will be populated
1064with C<$uid> and C<$gid>. In addition an empty Unix2 extra field will also
1065be created in the central zip header
1066
1067If the C<Minimal> option is set to true, this option will be ignored.
1068
1069By default no Unix2 extra field is created.
1070
e7d45986 1071=item C<< Comment => $comment >>
c70c1701 1072
1073Stores the contents of C<$comment> in the Central File Header of
1074the zip file.
1075
1076By default, no comment field is written to the zip file.
1077
e7d45986 1078=item C<< ZipComment => $comment >>
c70c1701 1079
1080Stores the contents of C<$comment> in the End of Central Directory record
1081of the zip file.
1082
1083By default, no comment field is written to the zip file.
1084
e7d45986 1085=item C<< Method => $method >>
2b4e0969 1086
1087Controls which compression method is used. At present three compression
1088methods are supported, namely Store (no compression at all), Deflate and
1089Bzip2.
1090
1091The symbols, ZIP_CM_STORE, ZIP_CM_DEFLATE and ZIP_CM_BZIP2 are used to
1092select the compression method.
1093
1094These constants are not imported by C<IO::Compress::Zip> by default.
1095
1096 use IO::Compress::Zip qw(:zip_method);
1097 use IO::Compress::Zip qw(:constants);
1098 use IO::Compress::Zip qw(:all);
1099
1100Note that to create Bzip2 content, the module C<IO::Compress::Bzip2> must
1101be installed. A fatal error will be thrown if you attempt to create Bzip2
1102content when C<IO::Compress::Bzip2> is not available.
1103
1104The default method is ZIP_CM_DEFLATE.
1105
e7d45986 1106=item C<< Stream => 0|1 >>
2b4e0969 1107
1108This option controls whether the zip file/buffer output is created in
1109streaming mode.
1110
c70c1701 1111Note that when outputting to a file with streaming mode disabled (C<Stream>
1112is 0), the output file must be seekable.
1113
2b4e0969 1114The default is 1.
1115
4e7676c7 1116=item C<< Zip64 => 0|1 >>
258133d1 1117
4e7676c7 1118Create a Zip64 zip file/buffer. This option should only be used if you want
1119to store files larger than 4 Gig.
1120
1121If you intend to manipulate the Zip64 zip files created with this module
10c2b2bb 1122using an external zip/unzip make sure that it supports Zip64.
4e7676c7 1123
1124In particular, if you are using Info-Zip you need to have zip version 3.x
1125or better to update a Zip64 archive and unzip version 6.x to read a zip64
10c2b2bb 1126archive.
4e7676c7 1127
1128The default is 0.
258133d1 1129
e7d45986 1130=item C<< TextFlag => 0|1 >>
c70c1701 1131
1132This parameter controls the setting of a bit in the zip central header. It
1133is used to signal that the data stored in the zip file/buffer is probably
1134text.
1135
1136The default is 0.
1137
e7d45986 1138=item C<< ExtraFieldLocal => $data >>
1139=item C<< ExtraFieldCentral => $data >>
c70c1701 1140
d54256af 1141The C<ExtraFieldLocal> option is used to store additional metadata in the
1142local header for the zip file/buffer. The C<ExtraFieldCentral> does the
1143same for the matching central header.
c70c1701 1144
1145An extra field consists of zero or more subfields. Each subfield consists
1146of a two byte header followed by the subfield data.
1147
1148The list of subfields can be supplied in any of the following formats
1149
1150 ExtraFieldLocal => [$id1, $data1,
1151 $id2, $data2,
1152 ...
1153 ]
1154
1155 ExtraFieldLocal => [ [$id1 => $data1],
1156 [$id2 => $data2],
1157 ...
1158 ]
1159
1160 ExtraFieldLocal => { $id1 => $data1,
1161 $id2 => $data2,
1162 ...
1163 }
1164
1165Where C<$id1>, C<$id2> are two byte subfield ID's.
1166
1167If you use the hash syntax, you have no control over the order in which
1168the ExtraSubFields are stored, plus you cannot have SubFields with
1169duplicate ID.
1170
1171Alternatively the list of subfields can by supplied as a scalar, thus
1172
1173 ExtraField => $rawdata
1174
d54256af 1175The Extended Time field (ID "UT"), set using the C<exTime> option, and the
1176Unix2 extra field (ID "Ux), set using the C<exUnix2> option, are examples
1177of extra fields.
c70c1701 1178
1179If the C<Minimal> option is set to true, this option will be ignored.
1180
1181The maximum size of an extra field 65535 bytes.
1182
e7d45986 1183=item C<< Minimal => 1|0 >>
c70c1701 1184
d54256af 1185If specified, this option will disable the creation of all extra fields
1186in the zip local and central headers. So the C<exTime>, C<exUnix2>,
1187C<ExtraFieldLocal> and C<ExtraFieldCentral> options will be ignored.
c70c1701 1188
1189This parameter defaults to 0.
1190
e7d45986 1191=item C<< BlockSize100K => number >>
2b4e0969 1192
1193Specify the number of 100K blocks bzip2 uses during compression.
1194
1195Valid values are from 1 to 9, where 9 is best compression.
1196
1197This option is only valid if the C<Method> is ZIP_CM_BZIP2. It is ignored
1198otherwise.
1199
1200The default is 1.
1201
e7d45986 1202=item C<< WorkFactor => number >>
2b4e0969 1203
1204Specifies how much effort bzip2 should take before resorting to a slower
1205fallback compression algorithm.
1206
1207Valid values range from 0 to 250, where 0 means use the default value 30.
1208
1209This option is only valid if the C<Method> is ZIP_CM_BZIP2. It is ignored
1210otherwise.
1211
1212The default is 0.
1213
25f0751f 1214=item -Level
1215
1216Defines the compression level used by zlib. The value should either be
1217a number between 0 and 9 (0 means no compression and 9 is maximum
1218compression), or one of the symbolic constants defined below.
1219
1220 Z_NO_COMPRESSION
1221 Z_BEST_SPEED
1222 Z_BEST_COMPRESSION
1223 Z_DEFAULT_COMPRESSION
1224
1225The default is Z_DEFAULT_COMPRESSION.
1226
1227Note, these constants are not imported by C<IO::Compress::Zip> by default.
1228
1229 use IO::Compress::Zip qw(:strategy);
1230 use IO::Compress::Zip qw(:constants);
1231 use IO::Compress::Zip qw(:all);
1232
1233=item -Strategy
1234
1235Defines the strategy used to tune the compression. Use one of the symbolic
1236constants defined below.
1237
1238 Z_FILTERED
1239 Z_HUFFMAN_ONLY
1240 Z_RLE
1241 Z_FIXED
1242 Z_DEFAULT_STRATEGY
1243
1244The default is Z_DEFAULT_STRATEGY.
1245
e7d45986 1246=item C<< Strict => 0|1 >>
25f0751f 1247
25f0751f 1248This is a placeholder option.
1249
25f0751f 1250=back
1251
1252=head2 Examples
1253
1254TODO
1255
1256=head1 Methods
1257
1258=head2 print
1259
1260Usage is
1261
1262 $z->print($data)
1263 print $z $data
1264
1265Compresses and outputs the contents of the C<$data> parameter. This
1266has the same behaviour as the C<print> built-in.
1267
1268Returns true if successful.
1269
1270=head2 printf
1271
1272Usage is
1273
1274 $z->printf($format, $data)
1275 printf $z $format, $data
1276
1277Compresses and outputs the contents of the C<$data> parameter.
1278
1279Returns true if successful.
1280
1281=head2 syswrite
1282
1283Usage is
1284
1285 $z->syswrite $data
1286 $z->syswrite $data, $length
1287 $z->syswrite $data, $length, $offset
1288
1289Compresses and outputs the contents of the C<$data> parameter.
1290
1291Returns the number of uncompressed bytes written, or C<undef> if
1292unsuccessful.
1293
1294=head2 write
1295
1296Usage is
1297
1298 $z->write $data
1299 $z->write $data, $length
1300 $z->write $data, $length, $offset
1301
1302Compresses and outputs the contents of the C<$data> parameter.
1303
1304Returns the number of uncompressed bytes written, or C<undef> if
1305unsuccessful.
1306
1307=head2 flush
1308
1309Usage is
1310
25f0751f 1311 $z->flush;
1312 $z->flush($flush_type);
1313
25f0751f 1314Flushes any pending compressed data to the output file/buffer.
1315
25f0751f 1316This method takes an optional parameter, C<$flush_type>, that controls
1317how the flushing will be carried out. By default the C<$flush_type>
1318used is C<Z_FINISH>. Other valid values for C<$flush_type> are
1319C<Z_NO_FLUSH>, C<Z_SYNC_FLUSH>, C<Z_FULL_FLUSH> and C<Z_BLOCK>. It is
1320strongly recommended that you only set the C<flush_type> parameter if
1321you fully understand the implications of what it does - overuse of C<flush>
1322can seriously degrade the level of compression achieved. See the C<zlib>
1323documentation for details.
1324
25f0751f 1325Returns true on success.
1326
25f0751f 1327=head2 tell
1328
1329Usage is
1330
1331 $z->tell()
1332 tell $z
1333
1334Returns the uncompressed file offset.
1335
1336=head2 eof
1337
1338Usage is
1339
1340 $z->eof();
1341 eof($z);
1342
25f0751f 1343Returns true if the C<close> method has been called.
1344
25f0751f 1345=head2 seek
1346
1347 $z->seek($position, $whence);
1348 seek($z, $position, $whence);
1349
25f0751f 1350Provides a sub-set of the C<seek> functionality, with the restriction
1351that it is only legal to seek forward in the output file/buffer.
1352It is a fatal error to attempt to seek backward.
1353
1354Empty parts of the file/buffer will have NULL (0x00) bytes written to them.
1355
25f0751f 1356The C<$whence> parameter takes one the usual values, namely SEEK_SET,
1357SEEK_CUR or SEEK_END.
1358
1359Returns 1 on success, 0 on failure.
1360
1361=head2 binmode
1362
1363Usage is
1364
1365 $z->binmode
1366 binmode $z ;
1367
1368This is a noop provided for completeness.
1369
1370=head2 opened
1371
1372 $z->opened()
1373
1374Returns true if the object currently refers to a opened file/buffer.
1375
1376=head2 autoflush
1377
1378 my $prev = $z->autoflush()
1379 my $prev = $z->autoflush(EXPR)
1380
1381If the C<$z> object is associated with a file or a filehandle, this method
1382returns the current autoflush setting for the underlying filehandle. If
1383C<EXPR> is present, and is non-zero, it will enable flushing after every
1384write/print operation.
1385
1386If C<$z> is associated with a buffer, this method has no effect and always
1387returns C<undef>.
1388
1389B<Note> that the special variable C<$|> B<cannot> be used to set or
1390retrieve the autoflush setting.
1391
1392=head2 input_line_number
1393
1394 $z->input_line_number()
1395 $z->input_line_number(EXPR)
1396
25f0751f 1397This method always returns C<undef> when compressing.
1398
25f0751f 1399=head2 fileno
1400
1401 $z->fileno()
1402 fileno($z)
1403
d54256af 1404If the C<$z> object is associated with a file or a filehandle, C<fileno>
1405will return the underlying file descriptor. Once the C<close> method is
1406called C<fileno> will return C<undef>.
25f0751f 1407
d54256af 1408If the C<$z> object is is associated with a buffer, this method will return
1409C<undef>.
25f0751f 1410
1411=head2 close
1412
1413 $z->close() ;
1414 close $z ;
1415
25f0751f 1416Flushes any pending compressed data and then closes the output file/buffer.
1417
25f0751f 1418For most versions of Perl this method will be automatically invoked if
1419the IO::Compress::Zip object is destroyed (either explicitly or by the
1420variable with the reference to the object going out of scope). The
1421exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
1422these cases, the C<close> method will be called automatically, but
1423not until global destruction of all live objects when the program is
1424terminating.
1425
1426Therefore, if you want your scripts to be able to run on all versions
1427of Perl, you should call C<close> explicitly and not rely on automatic
1428closing.
1429
1430Returns true on success, otherwise 0.
1431
1432If the C<AutoClose> option has been enabled when the IO::Compress::Zip
1433object was created, and the object is associated with a file, the
1434underlying file will also be closed.
1435
25f0751f 1436=head2 newStream([OPTS])
1437
1438Usage is
1439
1440 $z->newStream( [OPTS] )
1441
1442Closes the current compressed data stream and starts a new one.
1443
e7d45986 1444OPTS consists of any of the the options that are available when creating
1445the C<$z> object.
25f0751f 1446
e7d45986 1447See the L</"Constructor Options"> section for more details.
25f0751f 1448
25f0751f 1449=head2 deflateParams
1450
1451Usage is
1452
1453 $z->deflateParams
1454
1455TODO
1456
25f0751f 1457=head1 Importing
1458
25f0751f 1459A number of symbolic constants are required by some methods in
1460C<IO::Compress::Zip>. None are imported by default.
1461
25f0751f 1462=over 5
1463
1464=item :all
1465
25f0751f 1466Imports C<zip>, C<$ZipError> and all symbolic
1467constants that can be used by C<IO::Compress::Zip>. Same as doing this
1468
1469 use IO::Compress::Zip qw(zip $ZipError :constants) ;
1470
1471=item :constants
1472
1473Import all symbolic constants. Same as doing this
1474
2b4e0969 1475 use IO::Compress::Zip qw(:flush :level :strategy :zip_method) ;
1476
25f0751f 1477=item :flush
1478
1479These symbolic constants are used by the C<flush> method.
1480
1481 Z_NO_FLUSH
1482 Z_PARTIAL_FLUSH
1483 Z_SYNC_FLUSH
1484 Z_FULL_FLUSH
1485 Z_FINISH
1486 Z_BLOCK
1487
1488=item :level
1489
1490These symbolic constants are used by the C<Level> option in the constructor.
1491
1492 Z_NO_COMPRESSION
1493 Z_BEST_SPEED
1494 Z_BEST_COMPRESSION
1495 Z_DEFAULT_COMPRESSION
1496
25f0751f 1497=item :strategy
1498
1499These symbolic constants are used by the C<Strategy> option in the constructor.
1500
1501 Z_FILTERED
1502 Z_HUFFMAN_ONLY
1503 Z_RLE
1504 Z_FIXED
1505 Z_DEFAULT_STRATEGY
2b4e0969 1506
2b4e0969 1507=item :zip_method
1508
1509These symbolic constants are used by the C<Method> option in the
1510constructor.
1511
1512 ZIP_CM_STORE
1513 ZIP_CM_DEFLATE
1514 ZIP_CM_BZIP2
1515
1516
25f0751f 1517
1518
1519=back
1520
25f0751f 1521=head1 EXAMPLES
1522
d54256af 1523=head2 Apache::GZip Revisited
e7d45986 1524
319fab50 1525See L<IO::Compress::FAQ|IO::Compress::FAQ/"Apache::GZip Revisited">
e7d45986 1526
d54256af 1527
e7d45986 1528
d54256af 1529=head2 Working with Net::FTP
e7d45986 1530
319fab50 1531See L<IO::Compress::FAQ|IO::Compress::FAQ/"Compressed files and Net::FTP">
e7d45986 1532
25f0751f 1533=head1 SEE ALSO
1534
258133d1 1535L<Compress::Zlib>, L<IO::Compress::Gzip>, L<IO::Uncompress::Gunzip>, L<IO::Compress::Deflate>, L<IO::Uncompress::Inflate>, L<IO::Compress::RawDeflate>, L<IO::Uncompress::RawInflate>, L<IO::Compress::Bzip2>, L<IO::Uncompress::Bunzip2>, L<IO::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress>
25f0751f 1536
1537L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
1538
1539L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
1540L<Archive::Tar|Archive::Tar>,
1541L<IO::Zlib|IO::Zlib>
1542
25f0751f 1543For RFC 1950, 1951 and 1952 see
1544F<http://www.faqs.org/rfcs/rfc1950.html>,
1545F<http://www.faqs.org/rfcs/rfc1951.html> and
1546F<http://www.faqs.org/rfcs/rfc1952.html>
1547
1548The I<zlib> compression library was written by Jean-loup Gailly
1549F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
1550
1551The primary site for the I<zlib> compression library is
1552F<http://www.zlib.org>.
1553
1554The primary site for gzip is F<http://www.gzip.org>.
1555
25f0751f 1556=head1 AUTHOR
1557
cb7abd7f 1558This module was written by Paul Marquess, F<pmqs@cpan.org>.
25f0751f 1559
25f0751f 1560=head1 MODIFICATION HISTORY
1561
1562See the Changes file.
1563
1564=head1 COPYRIGHT AND LICENSE
25f0751f 1565
319fab50 1566Copyright (c) 2005-2009 Paul Marquess. All rights reserved.
25f0751f 1567
1568This program is free software; you can redistribute it and/or
1569modify it under the same terms as Perl itself.
1570