IO::Compress::*
[p5sagit/p5-mst-13.2.git] / ext / Compress / IO / Zlib / lib / IO / Compress / Zip.pm
CommitLineData
25f0751f 1package IO::Compress::Zip ;
2
3use strict ;
4use warnings;
5use bytes;
6
2b4e0969 7use IO::Compress::Base::Common qw(:Status createSelfTiedObject);
25f0751f 8use IO::Compress::RawDeflate;
9use IO::Compress::Adapter::Deflate;
10use IO::Compress::Adapter::Identity;
11
2b4e0969 12use Compress::Raw::Zlib qw(crc32) ;
13BEGIN
14{
15 eval { require IO::Compress::Adapter::Bzip2;
16 import IO::Compress::Adapter::Bzip2;
17 require IO::Compress::Bzip2;
18 import IO::Compress::Bzip2;
19 } ;
20}
21
22
25f0751f 23require Exporter ;
24
25our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $ZipError);
26
2b4e0969 27$VERSION = '2.000_11';
25f0751f 28$ZipError = '';
29
30@ISA = qw(Exporter IO::Compress::RawDeflate);
31@EXPORT_OK = qw( $ZipError zip ) ;
32%EXPORT_TAGS = %IO::Compress::RawDeflate::DEFLATE_CONSTANTS ;
33push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
2b4e0969 34
35$EXPORT_TAGS{zip_method} = [qw( ZIP_CM_STORE ZIP_CM_DEFLATE ZIP_CM_BZIP2 )];
36push @{ $EXPORT_TAGS{all} }, @{ $EXPORT_TAGS{zip_method} };
37
25f0751f 38Exporter::export_ok_tags('all');
39
2b4e0969 40use constant ZIP_CM_STORE => 0 ;
41use constant ZIP_CM_DEFLATE => 8 ;
42use constant ZIP_CM_BZIP2 => 12 ;
43
44use constant ZIP_LOCAL_HDR_SIG => 0x04034b50;
45use constant ZIP_DATA_HDR_SIG => 0x08074b50;
46use constant ZIP_CENTRAL_HDR_SIG => 0x02014b50;
47use constant ZIP_END_CENTRAL_HDR_SIG => 0x06054b50;
48
49
50our (%ZIP_CM_MIN_VERSIONS);
51%ZIP_CM_MIN_VERSIONS = (
52 ZIP_CM_STORE() => 20,
53 ZIP_CM_DEFLATE() => 20,
54 ZIP_CM_BZIP2() => 46,
55 );
25f0751f 56
57sub new
58{
59 my $class = shift ;
60
61 my $obj = createSelfTiedObject($class, \$ZipError);
62 $obj->_create(undef, @_);
63}
64
65sub zip
66{
67 my $obj = createSelfTiedObject(undef, \$ZipError);
68 return $obj->_def(@_);
69}
70
71sub mkComp
72{
73 my $self = shift ;
74 my $class = shift ;
75 my $got = shift ;
76
77 my ($obj, $errstr, $errno) ;
78
2b4e0969 79 if (*$self->{ZipData}{Method} == ZIP_CM_STORE) {
25f0751f 80 ($obj, $errstr, $errno) = IO::Compress::Adapter::Identity::mkCompObject(
25f0751f 81 $got->value('Level'),
82 $got->value('Strategy')
83 );
84 }
2b4e0969 85 elsif (*$self->{ZipData}{Method} == ZIP_CM_DEFLATE) {
25f0751f 86 ($obj, $errstr, $errno) = IO::Compress::Adapter::Deflate::mkCompObject(
87 $got->value('CRC32'),
88 $got->value('Adler32'),
89 $got->value('Level'),
90 $got->value('Strategy')
91 );
92 }
2b4e0969 93 elsif (*$self->{ZipData}{Method} == ZIP_CM_BZIP2) {
94 ($obj, $errstr, $errno) = IO::Compress::Adapter::Bzip2::mkCompObject(
95 $got->value('BlockSize100K'),
96 $got->value('WorkFactor'),
97 $got->value('Verbosity')
98 );
99 *$self->{ZipData}{CRC32} = crc32(undef);
100 }
25f0751f 101
102 return $self->saveErrorString(undef, $errstr, $errno)
103 if ! defined $obj;
104
105 if (! defined *$self->{ZipData}{StartOffset}) {
106 *$self->{ZipData}{StartOffset} = *$self->{ZipData}{Offset} = 0;
107 }
108
109 return $obj;
110}
111
2b4e0969 112sub reset
113{
114 my $self = shift ;
115
116 *$self->{Compress}->reset();
117 *$self->{ZipData}{CRC32} = Compress::Raw::Zlib::crc32('');
118
119 return STATUS_OK;
120}
121
122sub filterUncompressed
123{
124 my $self = shift ;
25f0751f 125
2b4e0969 126 if (*$self->{ZipData}{Method} == ZIP_CM_DEFLATE) {
127 *$self->{ZipData}{CRC32} = *$self->{Compress}->crc32();
128 }
129 else {
130 *$self->{ZipData}{CRC32} = crc32(${$_[0]}, *$self->{ZipData}{CRC32});
131
132 }
133}
25f0751f 134
135sub mkHeader
136{
137 my $self = shift;
138 my $param = shift ;
139
140 my $filename = '';
141 $filename = $param->value('Name') || '';
142
143 my $comment = '';
144 $comment = $param->value('Comment') || '';
145
25f0751f 146 my $hdr = '';
147
148 my $time = _unixToDosTime($param->value('Time'));
149 *$self->{ZipData}{StartOffset} = *$self->{ZipData}{Offset} ;
150
151 my $strm = *$self->{ZipData}{Stream} ? 8 : 0 ;
2b4e0969 152 # bzip2 is 12, deflate is 8
153 my $method = *$self->{ZipData}{Method} ;
154
155 # deflate is 20
156 # bzip2 is 46
157 my $extract = $param->value('OS_Code') << 8 +
158 $ZIP_CM_MIN_VERSIONS{$method};
25f0751f 159
2b4e0969 160 $hdr .= pack "V", ZIP_LOCAL_HDR_SIG ; # signature
25f0751f 161 $hdr .= pack 'v', $extract ; # extract Version & OS
162 $hdr .= pack 'v', $strm ; # general purpose flag (set streaming mode)
163 $hdr .= pack 'v', $method ; # compression method (deflate)
164 $hdr .= pack 'V', $time ; # last mod date/time
165 $hdr .= pack 'V', 0 ; # crc32 - 0 when streaming
166 $hdr .= pack 'V', 0 ; # compressed length - 0 when streaming
167 $hdr .= pack 'V', 0 ; # uncompressed length - 0 when streaming
168 $hdr .= pack 'v', length $filename ; # filename length
169 $hdr .= pack 'v', 0 ; # extra length
170
171 $hdr .= $filename ;
172
173
174 my $ctl = '';
175
2b4e0969 176 $ctl .= pack "V", ZIP_CENTRAL_HDR_SIG ; # signature
25f0751f 177 $ctl .= pack 'v', $extract ; # version made by
178 $ctl .= pack 'v', $extract ; # extract Version
179 $ctl .= pack 'v', $strm ; # general purpose flag (streaming mode)
180 $ctl .= pack 'v', $method ; # compression method (deflate)
181 $ctl .= pack 'V', $time ; # last mod date/time
182 $ctl .= pack 'V', 0 ; # crc32
183 $ctl .= pack 'V', 0 ; # compressed length
184 $ctl .= pack 'V', 0 ; # uncompressed length
185 $ctl .= pack 'v', length $filename ; # filename length
186 $ctl .= pack 'v', 0 ; # extra length
187 $ctl .= pack 'v', length $comment ; # file comment length
188 $ctl .= pack 'v', 0 ; # disk number start
189 $ctl .= pack 'v', 0 ; # internal file attributes
190 $ctl .= pack 'V', 0 ; # external file attributes
191 $ctl .= pack 'V', *$self->{ZipData}{Offset} ; # offset to local header
192
193 $ctl .= $filename ;
194 #$ctl .= $extra ;
195 $ctl .= $comment ;
196
197 *$self->{ZipData}{Offset} += length $hdr ;
198
199 *$self->{ZipData}{CentralHeader} = $ctl;
200
201 return $hdr;
202}
203
204sub mkTrailer
205{
206 my $self = shift ;
207
2b4e0969 208 my $crc32 ;
209 if (*$self->{ZipData}{Method} == ZIP_CM_DEFLATE) {
210 $crc32 = *$self->{Compress}->crc32();
211 }
212 else {
213 $crc32 = *$self->{ZipData}{CRC32};
214 }
215
25f0751f 216 my $compressedBytes = *$self->{Compress}->compressedBytes();
217 my $uncompressedBytes = *$self->{Compress}->uncompressedBytes();
218
219 my $data ;
220 $data .= pack "V", $crc32 ; # CRC32
221 $data .= pack "V", $compressedBytes ; # Compressed Size
222 $data .= pack "V", $uncompressedBytes; # Uncompressed Size
223
224 my $hdr = '';
225
226 if (*$self->{ZipData}{Stream}) {
2b4e0969 227 $hdr = pack "V", ZIP_DATA_HDR_SIG ; # signature
25f0751f 228 $hdr .= $data ;
229 }
230 else {
231 $self->writeAt(*$self->{ZipData}{StartOffset} + 14, $data)
232 or return undef;
233 }
234
235 my $ctl = *$self->{ZipData}{CentralHeader} ;
236 substr($ctl, 16, 12) = $data ;
237 #substr($ctl, 16, 4) = pack "V", $crc32 ; # CRC32
238 #substr($ctl, 20, 4) = pack "V", $compressedBytes ; # Compressed Size
239 #substr($ctl, 24, 4) = pack "V", $uncompressedBytes ; # Uncompressed Size
240
241 *$self->{ZipData}{Offset} += length($hdr) + $compressedBytes;
242 push @{ *$self->{ZipData}{CentralDir} }, $ctl ;
243
244 return $hdr;
245}
246
247sub mkFinalTrailer
248{
249 my $self = shift ;
250
251 my $comment = '';
252 $comment = *$self->{ZipData}{ZipComment} ;
253
254 my $entries = @{ *$self->{ZipData}{CentralDir} };
255 my $cd = join '', @{ *$self->{ZipData}{CentralDir} };
256
257 my $ecd = '';
2b4e0969 258 $ecd .= pack "V", ZIP_END_CENTRAL_HDR_SIG ; # signature
25f0751f 259 $ecd .= pack 'v', 0 ; # number of disk
260 $ecd .= pack 'v', 0 ; # number if disk with central dir
261 $ecd .= pack 'v', $entries ; # entries in central dir on this disk
262 $ecd .= pack 'v', $entries ; # entries in central dir
263 $ecd .= pack 'V', length $cd ; # size of central dir
264 $ecd .= pack 'V', *$self->{ZipData}{Offset} ; # offset to start central dir
265 $ecd .= pack 'v', length $comment ; # zipfile comment length
266 $ecd .= $comment;
267
268 return $cd . $ecd ;
269}
270
271sub ckParams
272{
273 my $self = shift ;
274 my $got = shift;
275
276 $got->value('CRC32' => 1);
277
278 if (! $got->parsed('Time') ) {
279 # Modification time defaults to now.
280 $got->value('Time' => time) ;
281 }
282
283 *$self->{ZipData}{Stream} = $got->value('Stream');
2b4e0969 284 #*$self->{ZipData}{Store} = $got->value('Store');
285
286 my $method = $got->value('Method');
287 #if ($method != 0 && $method != 8 && $method != 12) {
288 return $self->saveErrorString(undef, "Unknown Method '$method'")
289 if ! defined $ZIP_CM_MIN_VERSIONS{$method};
290
291 return $self->saveErrorString(undef, "Bzip2 not available")
292 if $method == ZIP_CM_BZIP2 and
293 ! defined $IO::Compress::Adapter::Bzip2::VERSION;
294
295 *$self->{ZipData}{Method} = $method;
296
25f0751f 297 *$self->{ZipData}{ZipComment} = $got->value('ZipComment') ;
298
2b4e0969 299 return undef
300 if defined $IO::Compress::Bzip2::VERSION
301 and ! IO::Compress::Bzip2::ckParams($self, $got);
25f0751f 302
303 return 1 ;
304}
305
306#sub newHeader
307#{
308# my $self = shift ;
309#
310# return $self->mkHeader(*$self->{Got});
311#}
312
313sub getExtraParams
314{
315 my $self = shift ;
316
317 use IO::Compress::Base::Common qw(:Parse);
318 use Compress::Raw::Zlib qw(Z_DEFLATED Z_DEFAULT_COMPRESSION Z_DEFAULT_STRATEGY);
319
2b4e0969 320 my @Bzip2 = ();
321
322 @Bzip2 = IO::Compress::Bzip2::getExtraParams($self)
323 if defined $IO::Compress::Bzip2::VERSION;
25f0751f 324
325 return (
326 # zlib behaviour
327 $self->getZlibParams(),
328
329 'Stream' => [1, 1, Parse_boolean, 1],
2b4e0969 330 #'Store' => [0, 1, Parse_boolean, 0],
331 'Method' => [0, 1, Parse_unsigned, ZIP_CM_DEFLATE],
25f0751f 332
333# # Zip header fields
334# 'Minimal' => [0, 1, Parse_boolean, 0],
335 'Comment' => [0, 1, Parse_any, ''],
336 'ZipComment'=> [0, 1, Parse_any, ''],
337 'Name' => [0, 1, Parse_any, ''],
338 'Time' => [0, 1, Parse_any, undef],
339 'OS_Code' => [0, 1, Parse_unsigned, $Compress::Raw::Zlib::gzip_os_code],
340
341# 'TextFlag' => [0, 1, Parse_boolean, 0],
342# 'ExtraField'=> [0, 1, Parse_string, ''],
2b4e0969 343
344 @Bzip2,
25f0751f 345 );
346}
347
348sub getInverseClass
349{
350 return ('IO::Uncompress::Unzip',
351 \$IO::Uncompress::Unzip::UnzipError);
352}
353
354sub getFileInfo
355{
356 my $self = shift ;
357 my $params = shift;
358 my $filename = shift ;
359
360 my $defaultTime = (stat($filename))[9] ;
361
362 $params->value('Name' => $filename)
363 if ! $params->parsed('Name') ;
364
365 $params->value('Time' => $defaultTime)
366 if ! $params->parsed('Time') ;
367
368
369}
370
371# from Archive::Zip
372sub _unixToDosTime # Archive::Zip::Member
373{
374 my $time_t = shift;
375 # TODO - add something to cope with unix time < 1980
376 my ( $sec, $min, $hour, $mday, $mon, $year ) = localtime($time_t);
377 my $dt = 0;
378 $dt += ( $sec >> 1 );
379 $dt += ( $min << 5 );
380 $dt += ( $hour << 11 );
381 $dt += ( $mday << 16 );
382 $dt += ( ( $mon + 1 ) << 21 );
383 $dt += ( ( $year - 80 ) << 25 );
384 return $dt;
385}
386
3871;
388
389__END__
390
391=head1 NAME
392
393
cb7abd7f 394
395IO::Compress::Zip - Write zip files/buffers
396
25f0751f 397
398
399=head1 SYNOPSIS
400
401 use IO::Compress::Zip qw(zip $ZipError) ;
402
403
404 my $status = zip $input => $output [,OPTS]
405 or die "zip failed: $ZipError\n";
406
407 my $z = new IO::Compress::Zip $output [,OPTS]
408 or die "zip failed: $ZipError\n";
409
410 $z->print($string);
411 $z->printf($format, $string);
412 $z->write($string);
413 $z->syswrite($string [, $length, $offset]);
414 $z->flush();
415 $z->tell();
416 $z->eof();
417 $z->seek($position, $whence);
418 $z->binmode();
419 $z->fileno();
420 $z->opened();
421 $z->autoflush();
422 $z->input_line_number();
423 $z->newStream( [OPTS] );
424
425 $z->deflateParams();
426
427 $z->close() ;
428
429 $ZipError ;
430
431 # IO::File mode
432
433 print $z $string;
434 printf $z $format, $string;
435 tell $z
436 eof $z
437 seek $z, $position, $whence
438 binmode $z
439 fileno $z
440 close $z ;
441
442
443=head1 DESCRIPTION
444
445
446
447B<WARNING -- This is a Beta release>.
448
449=over 5
450
451=item * DO NOT use in production code.
452
453=item * The documentation is incomplete in places.
454
455=item * Parts of the interface defined here are tentative.
456
457=item * Please report any problems you find.
458
459=back
460
461
462
463
464This module provides a Perl interface that allows writing zip
465compressed data to files or buffer.
466
467
468
469
470
471
472
cb7abd7f 473Note that IO::Compress::Zip is not intended to be a replacement for the module
25f0751f 474C<Archive::Zip>.
cb7abd7f 475
25f0751f 476The primary aim of this module is not as an archiver, but to provide
477streaming write access to zip file files and buffers.
478
479
480
481For reading zip files/buffers, see the companion module
482L<IO::Uncompress::Unzip|IO::Uncompress::Unzip>.
483
484
485=head1 Functional Interface
486
487A top-level function, C<zip>, is provided to carry out
488"one-shot" compression between buffers and/or files. For finer
489control over the compression process, see the L</"OO Interface">
490section.
491
492 use IO::Compress::Zip qw(zip $ZipError) ;
493
494 zip $input => $output [,OPTS]
495 or die "zip failed: $ZipError\n";
496
497
498
499The functional interface needs Perl5.005 or better.
500
501
502=head2 zip $input => $output [, OPTS]
503
504
505C<zip> expects at least two parameters, C<$input> and C<$output>.
506
507=head3 The C<$input> parameter
508
509The parameter, C<$input>, is used to define the source of
510the uncompressed data.
511
512It can take one of the following forms:
513
514=over 5
515
516=item A filename
517
518If the C<$input> parameter is a simple scalar, it is assumed to be a
519filename. This file will be opened for reading and the input data
520will be read from it.
521
522=item A filehandle
523
524If the C<$input> parameter is a filehandle, the input data will be
525read from it.
526The string '-' can be used as an alias for standard input.
527
528=item A scalar reference
529
530If C<$input> is a scalar reference, the input data will be read
531from C<$$input>.
532
533=item An array reference
534
535If C<$input> is an array reference, each element in the array must be a
536filename.
537
538The input data will be read from each file in turn.
539
540The complete array will be walked to ensure that it only
541contains valid filenames before any data is compressed.
542
543
544
545=item An Input FileGlob string
546
547If C<$input> is a string that is delimited by the characters "<" and ">"
548C<zip> will assume that it is an I<input fileglob string>. The
549input is the list of files that match the fileglob.
550
551If the fileglob does not match any files ...
552
553See L<File::GlobMapper|File::GlobMapper> for more details.
554
555
556=back
557
558If the C<$input> parameter is any other type, C<undef> will be returned.
559
560
25f0751f 561In addition, if C<$input> is a simple filename, the default values for
cb7abd7f 562the C<Name> and C<Time> options will be sourced from that file.
25f0751f 563
564If you do not want to use these defaults they can be overridden by
565explicitly setting the C<Name> and C<Time> options or by setting the
566C<Minimal> parameter.
567
568
569
570=head3 The C<$output> parameter
571
572The parameter C<$output> is used to control the destination of the
573compressed data. This parameter can take one of these forms.
574
575=over 5
576
577=item A filename
578
579If the C<$output> parameter is a simple scalar, it is assumed to be a
580filename. This file will be opened for writing and the compressed
581data will be written to it.
582
583=item A filehandle
584
585If the C<$output> parameter is a filehandle, the compressed data
586will be written to it.
587The string '-' can be used as an alias for standard output.
588
589
590=item A scalar reference
591
592If C<$output> is a scalar reference, the compressed data will be
593stored in C<$$output>.
594
595
596
597=item An Array Reference
598
599If C<$output> is an array reference, the compressed data will be
600pushed onto the array.
601
602=item An Output FileGlob
603
604If C<$output> is a string that is delimited by the characters "<" and ">"
605C<zip> will assume that it is an I<output fileglob string>. The
606output is the list of files that match the fileglob.
607
608When C<$output> is an fileglob string, C<$input> must also be a fileglob
609string. Anything else is an error.
610
611=back
612
613If the C<$output> parameter is any other type, C<undef> will be returned.
614
615
616
617=head2 Notes
618
619When C<$input> maps to multiple files/buffers and C<$output> is a single
620file/buffer the compressed input files/buffers will all be stored
621in C<$output> as a single compressed stream.
622
623
624
625=head2 Optional Parameters
626
627Unless specified below, the optional parameters for C<zip>,
628C<OPTS>, are the same as those used with the OO interface defined in the
629L</"Constructor Options"> section below.
630
631=over 5
632
633=item AutoClose =E<gt> 0|1
634
635This option applies to any input or output data streams to
636C<zip> that are filehandles.
637
638If C<AutoClose> is specified, and the value is true, it will result in all
639input and/or output filehandles being closed once C<zip> has
640completed.
641
642This parameter defaults to 0.
643
644
645
646=item BinModeIn =E<gt> 0|1
647
648When reading from a file or filehandle, set C<binmode> before reading.
649
650Defaults to 0.
651
652
653
654
655
656=item -Append =E<gt> 0|1
657
658TODO
659
660
661=back
662
663
664
665=head2 Examples
666
667To read the contents of the file C<file1.txt> and write the compressed
668data to the file C<file1.txt.zip>.
669
670 use strict ;
671 use warnings ;
672 use IO::Compress::Zip qw(zip $ZipError) ;
673
674 my $input = "file1.txt";
675 zip $input => "$input.zip"
676 or die "zip failed: $ZipError\n";
677
678
679To read from an existing Perl filehandle, C<$input>, and write the
680compressed data to a buffer, C<$buffer>.
681
682 use strict ;
683 use warnings ;
684 use IO::Compress::Zip qw(zip $ZipError) ;
685 use IO::File ;
686
687 my $input = new IO::File "<file1.txt"
688 or die "Cannot open 'file1.txt': $!\n" ;
689 my $buffer ;
690 zip $input => \$buffer
691 or die "zip failed: $ZipError\n";
692
693To compress all files in the directory "/my/home" that match "*.txt"
694and store the compressed data in the same directory
695
696 use strict ;
697 use warnings ;
698 use IO::Compress::Zip qw(zip $ZipError) ;
699
700 zip '</my/home/*.txt>' => '<*.zip>'
701 or die "zip failed: $ZipError\n";
702
703and if you want to compress each file one at a time, this will do the trick
704
705 use strict ;
706 use warnings ;
707 use IO::Compress::Zip qw(zip $ZipError) ;
708
709 for my $input ( glob "/my/home/*.txt" )
710 {
711 my $output = "$input.zip" ;
712 zip $input => $output
713 or die "Error compressing '$input': $ZipError\n";
714 }
715
716
717=head1 OO Interface
718
719=head2 Constructor
720
721The format of the constructor for C<IO::Compress::Zip> is shown below
722
723 my $z = new IO::Compress::Zip $output [,OPTS]
724 or die "IO::Compress::Zip failed: $ZipError\n";
725
726It returns an C<IO::Compress::Zip> object on success and undef on failure.
727The variable C<$ZipError> will contain an error message on failure.
728
729If you are running Perl 5.005 or better the object, C<$z>, returned from
730IO::Compress::Zip can be used exactly like an L<IO::File|IO::File> filehandle.
731This means that all normal output file operations can be carried out
732with C<$z>.
733For example, to write to a compressed file/buffer you can use either of
734these forms
735
736 $z->print("hello world\n");
737 print $z "hello world\n";
738
739The mandatory parameter C<$output> is used to control the destination
740of the compressed data. This parameter can take one of these forms.
741
742=over 5
743
744=item A filename
745
746If the C<$output> parameter is a simple scalar, it is assumed to be a
747filename. This file will be opened for writing and the compressed data
748will be written to it.
749
750=item A filehandle
751
752If the C<$output> parameter is a filehandle, the compressed data will be
753written to it.
754The string '-' can be used as an alias for standard output.
755
756
757=item A scalar reference
758
759If C<$output> is a scalar reference, the compressed data will be stored
760in C<$$output>.
761
762=back
763
764If the C<$output> parameter is any other type, C<IO::Compress::Zip>::new will
765return undef.
766
767=head2 Constructor Options
768
769C<OPTS> is any combination of the following options:
770
771=over 5
772
773=item AutoClose =E<gt> 0|1
774
775This option is only valid when the C<$output> parameter is a filehandle. If
776specified, and the value is true, it will result in the C<$output> being
777closed once either the C<close> method is called or the C<IO::Compress::Zip>
778object is destroyed.
779
780This parameter defaults to 0.
781
782=item Append =E<gt> 0|1
783
784Opens C<$output> in append mode.
785
786The behaviour of this option is dependent on the type of C<$output>.
787
788=over 5
789
790=item * A Buffer
791
792If C<$output> is a buffer and C<Append> is enabled, all compressed data
793will be append to the end if C<$output>. Otherwise C<$output> will be
794cleared before any data is written to it.
795
796=item * A Filename
797
798If C<$output> is a filename and C<Append> is enabled, the file will be
799opened in append mode. Otherwise the contents of the file, if any, will be
800truncated before any compressed data is written to it.
801
802=item * A Filehandle
803
804If C<$output> is a filehandle, the file pointer will be positioned to the
805end of the file via a call to C<seek> before any compressed data is written
806to it. Otherwise the file pointer will not be moved.
807
808=back
809
810This parameter defaults to 0.
811
812
813
2b4e0969 814=item -Name =E<gt> $string
815
816Stores the contents of C<$string> in the zip filename header field. If
817C<Name> is not specified, no zip filename field will be created.
818
819=item -Time =E<gt> $number
820
821Sets the last modified time field in the zip header to $number.
822
823This field defaults to the time the C<IO::Compress::Zip> object was created
824if this option is not specified.
825
826=item Method =E<gt> $method
827
828Controls which compression method is used. At present three compression
829methods are supported, namely Store (no compression at all), Deflate and
830Bzip2.
831
832The symbols, ZIP_CM_STORE, ZIP_CM_DEFLATE and ZIP_CM_BZIP2 are used to
833select the compression method.
834
835These constants are not imported by C<IO::Compress::Zip> by default.
836
837 use IO::Compress::Zip qw(:zip_method);
838 use IO::Compress::Zip qw(:constants);
839 use IO::Compress::Zip qw(:all);
840
841Note that to create Bzip2 content, the module C<IO::Compress::Bzip2> must
842be installed. A fatal error will be thrown if you attempt to create Bzip2
843content when C<IO::Compress::Bzip2> is not available.
844
845The default method is ZIP_CM_DEFLATE.
846
847=item -Stream =E<gt> 0|1
848
849This option controls whether the zip file/buffer output is created in
850streaming mode.
851
852The default is 1.
853
854=item BlockSize100K =E<gt> number
855
856Specify the number of 100K blocks bzip2 uses during compression.
857
858Valid values are from 1 to 9, where 9 is best compression.
859
860This option is only valid if the C<Method> is ZIP_CM_BZIP2. It is ignored
861otherwise.
862
863The default is 1.
864
865=item WorkFactor =E<gt> number
866
867Specifies how much effort bzip2 should take before resorting to a slower
868fallback compression algorithm.
869
870Valid values range from 0 to 250, where 0 means use the default value 30.
871
872This option is only valid if the C<Method> is ZIP_CM_BZIP2. It is ignored
873otherwise.
874
875The default is 0.
876
25f0751f 877
878
879
880=item -Level
881
882Defines the compression level used by zlib. The value should either be
883a number between 0 and 9 (0 means no compression and 9 is maximum
884compression), or one of the symbolic constants defined below.
885
886 Z_NO_COMPRESSION
887 Z_BEST_SPEED
888 Z_BEST_COMPRESSION
889 Z_DEFAULT_COMPRESSION
890
891The default is Z_DEFAULT_COMPRESSION.
892
893Note, these constants are not imported by C<IO::Compress::Zip> by default.
894
895 use IO::Compress::Zip qw(:strategy);
896 use IO::Compress::Zip qw(:constants);
897 use IO::Compress::Zip qw(:all);
898
899=item -Strategy
900
901Defines the strategy used to tune the compression. Use one of the symbolic
902constants defined below.
903
904 Z_FILTERED
905 Z_HUFFMAN_ONLY
906 Z_RLE
907 Z_FIXED
908 Z_DEFAULT_STRATEGY
909
910The default is Z_DEFAULT_STRATEGY.
911
912
913
914
915
916
917=item -Strict =E<gt> 0|1
918
919
920
921This is a placeholder option.
922
923
924
925=back
926
927=head2 Examples
928
929TODO
930
931=head1 Methods
932
933=head2 print
934
935Usage is
936
937 $z->print($data)
938 print $z $data
939
940Compresses and outputs the contents of the C<$data> parameter. This
941has the same behaviour as the C<print> built-in.
942
943Returns true if successful.
944
945=head2 printf
946
947Usage is
948
949 $z->printf($format, $data)
950 printf $z $format, $data
951
952Compresses and outputs the contents of the C<$data> parameter.
953
954Returns true if successful.
955
956=head2 syswrite
957
958Usage is
959
960 $z->syswrite $data
961 $z->syswrite $data, $length
962 $z->syswrite $data, $length, $offset
963
964Compresses and outputs the contents of the C<$data> parameter.
965
966Returns the number of uncompressed bytes written, or C<undef> if
967unsuccessful.
968
969=head2 write
970
971Usage is
972
973 $z->write $data
974 $z->write $data, $length
975 $z->write $data, $length, $offset
976
977Compresses and outputs the contents of the C<$data> parameter.
978
979Returns the number of uncompressed bytes written, or C<undef> if
980unsuccessful.
981
982=head2 flush
983
984Usage is
985
986
987 $z->flush;
988 $z->flush($flush_type);
989
990
991Flushes any pending compressed data to the output file/buffer.
992
993
994This method takes an optional parameter, C<$flush_type>, that controls
995how the flushing will be carried out. By default the C<$flush_type>
996used is C<Z_FINISH>. Other valid values for C<$flush_type> are
997C<Z_NO_FLUSH>, C<Z_SYNC_FLUSH>, C<Z_FULL_FLUSH> and C<Z_BLOCK>. It is
998strongly recommended that you only set the C<flush_type> parameter if
999you fully understand the implications of what it does - overuse of C<flush>
1000can seriously degrade the level of compression achieved. See the C<zlib>
1001documentation for details.
1002
1003
1004Returns true on success.
1005
1006
1007=head2 tell
1008
1009Usage is
1010
1011 $z->tell()
1012 tell $z
1013
1014Returns the uncompressed file offset.
1015
1016=head2 eof
1017
1018Usage is
1019
1020 $z->eof();
1021 eof($z);
1022
1023
1024
1025Returns true if the C<close> method has been called.
1026
1027
1028
1029=head2 seek
1030
1031 $z->seek($position, $whence);
1032 seek($z, $position, $whence);
1033
1034
1035
1036
1037Provides a sub-set of the C<seek> functionality, with the restriction
1038that it is only legal to seek forward in the output file/buffer.
1039It is a fatal error to attempt to seek backward.
1040
1041Empty parts of the file/buffer will have NULL (0x00) bytes written to them.
1042
1043
1044
1045The C<$whence> parameter takes one the usual values, namely SEEK_SET,
1046SEEK_CUR or SEEK_END.
1047
1048Returns 1 on success, 0 on failure.
1049
1050=head2 binmode
1051
1052Usage is
1053
1054 $z->binmode
1055 binmode $z ;
1056
1057This is a noop provided for completeness.
1058
1059=head2 opened
1060
1061 $z->opened()
1062
1063Returns true if the object currently refers to a opened file/buffer.
1064
1065=head2 autoflush
1066
1067 my $prev = $z->autoflush()
1068 my $prev = $z->autoflush(EXPR)
1069
1070If the C<$z> object is associated with a file or a filehandle, this method
1071returns the current autoflush setting for the underlying filehandle. If
1072C<EXPR> is present, and is non-zero, it will enable flushing after every
1073write/print operation.
1074
1075If C<$z> is associated with a buffer, this method has no effect and always
1076returns C<undef>.
1077
1078B<Note> that the special variable C<$|> B<cannot> be used to set or
1079retrieve the autoflush setting.
1080
1081=head2 input_line_number
1082
1083 $z->input_line_number()
1084 $z->input_line_number(EXPR)
1085
1086
1087This method always returns C<undef> when compressing.
1088
1089
1090
1091=head2 fileno
1092
1093 $z->fileno()
1094 fileno($z)
1095
1096If the C<$z> object is associated with a file or a filehandle, this method
1097will return the underlying file descriptor.
1098
1099If the C<$z> object is is associated with a buffer, this method will
1100return undef.
1101
1102=head2 close
1103
1104 $z->close() ;
1105 close $z ;
1106
1107
1108
1109Flushes any pending compressed data and then closes the output file/buffer.
1110
1111
1112
1113For most versions of Perl this method will be automatically invoked if
1114the IO::Compress::Zip object is destroyed (either explicitly or by the
1115variable with the reference to the object going out of scope). The
1116exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
1117these cases, the C<close> method will be called automatically, but
1118not until global destruction of all live objects when the program is
1119terminating.
1120
1121Therefore, if you want your scripts to be able to run on all versions
1122of Perl, you should call C<close> explicitly and not rely on automatic
1123closing.
1124
1125Returns true on success, otherwise 0.
1126
1127If the C<AutoClose> option has been enabled when the IO::Compress::Zip
1128object was created, and the object is associated with a file, the
1129underlying file will also be closed.
1130
1131
1132
1133
1134=head2 newStream([OPTS])
1135
1136Usage is
1137
1138 $z->newStream( [OPTS] )
1139
1140Closes the current compressed data stream and starts a new one.
1141
1142OPTS consists of the following sub-set of the the options that are
1143available when creating the C<$z> object,
1144
1145=over 5
1146
1147
1148
1149=item * Level
1150
1151
1152
1153=back
1154
1155
1156=head2 deflateParams
1157
1158Usage is
1159
1160 $z->deflateParams
1161
1162TODO
1163
1164
1165=head1 Importing
1166
1167
1168A number of symbolic constants are required by some methods in
1169C<IO::Compress::Zip>. None are imported by default.
1170
1171
1172
1173=over 5
1174
1175=item :all
1176
1177
1178Imports C<zip>, C<$ZipError> and all symbolic
1179constants that can be used by C<IO::Compress::Zip>. Same as doing this
1180
1181 use IO::Compress::Zip qw(zip $ZipError :constants) ;
1182
1183=item :constants
1184
1185Import all symbolic constants. Same as doing this
1186
2b4e0969 1187
1188 use IO::Compress::Zip qw(:flush :level :strategy :zip_method) ;
1189
25f0751f 1190
1191=item :flush
1192
1193These symbolic constants are used by the C<flush> method.
1194
1195 Z_NO_FLUSH
1196 Z_PARTIAL_FLUSH
1197 Z_SYNC_FLUSH
1198 Z_FULL_FLUSH
1199 Z_FINISH
1200 Z_BLOCK
1201
1202=item :level
1203
1204These symbolic constants are used by the C<Level> option in the constructor.
1205
1206 Z_NO_COMPRESSION
1207 Z_BEST_SPEED
1208 Z_BEST_COMPRESSION
1209 Z_DEFAULT_COMPRESSION
1210
1211
1212=item :strategy
1213
1214These symbolic constants are used by the C<Strategy> option in the constructor.
1215
1216 Z_FILTERED
1217 Z_HUFFMAN_ONLY
1218 Z_RLE
1219 Z_FIXED
1220 Z_DEFAULT_STRATEGY
2b4e0969 1221
1222
1223=item :zip_method
1224
1225These symbolic constants are used by the C<Method> option in the
1226constructor.
1227
1228 ZIP_CM_STORE
1229 ZIP_CM_DEFLATE
1230 ZIP_CM_BZIP2
1231
1232
25f0751f 1233
1234
1235=back
1236
1237For
1238
1239=head1 EXAMPLES
1240
1241TODO
1242
1243
1244
1245
1246
1247
1248=head1 SEE ALSO
1249
1250L<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::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress>
1251
1252L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
1253
1254L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
1255L<Archive::Tar|Archive::Tar>,
1256L<IO::Zlib|IO::Zlib>
1257
1258
1259For RFC 1950, 1951 and 1952 see
1260F<http://www.faqs.org/rfcs/rfc1950.html>,
1261F<http://www.faqs.org/rfcs/rfc1951.html> and
1262F<http://www.faqs.org/rfcs/rfc1952.html>
1263
1264The I<zlib> compression library was written by Jean-loup Gailly
1265F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
1266
1267The primary site for the I<zlib> compression library is
1268F<http://www.zlib.org>.
1269
1270The primary site for gzip is F<http://www.gzip.org>.
1271
1272
1273
1274
25f0751f 1275=head1 AUTHOR
1276
cb7abd7f 1277This module was written by Paul Marquess, F<pmqs@cpan.org>.
25f0751f 1278
1279
1280
1281=head1 MODIFICATION HISTORY
1282
1283See the Changes file.
1284
1285=head1 COPYRIGHT AND LICENSE
25f0751f 1286
1287Copyright (c) 2005-2006 Paul Marquess. All rights reserved.
1288
1289This program is free software; you can redistribute it and/or
1290modify it under the same terms as Perl itself.
1291
1292