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