This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Compress::Zlib
[perl5.git] / ext / Compress / IO / Base / lib / IO / Uncompress / AnyUncompress.pm
CommitLineData
25f0751f
PM
1package IO::Uncompress::AnyUncompress ;
2
3use strict;
4use warnings;
5use bytes;
6
7use IO::Compress::Base::Common qw(createSelfTiedObject);
8
9#use IO::Uncompress::Base ;
10use IO::Uncompress::Gunzip ;
11use IO::Uncompress::Inflate ;
12use IO::Uncompress::RawInflate ;
13use IO::Uncompress::Unzip ;
14
15BEGIN
16{
17 eval { require IO::Uncompress::Adapter::Bunzip2; import IO::Uncompress::Adapter::Bunzip2 };
18 eval { require IO::Uncompress::Adapter::LZO; import IO::Uncompress::Adapter::LZO };
19 eval { require IO::Uncompress::Bunzip2; import IO::Uncompress::Bunzip2 };
20 eval { require IO::Uncompress::UnLzop; import IO::Uncompress::UnLzop };
21}
22
23require Exporter ;
24
25our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $AnyUncompressError);
26
27$VERSION = '2.000_08';
28$AnyUncompressError = '';
29
30@ISA = qw( Exporter IO::Uncompress::Base );
31@EXPORT_OK = qw( $AnyUncompressError anyuncompress ) ;
32%EXPORT_TAGS = %IO::Uncompress::Base::DEFLATE_CONSTANTS ;
33push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
34Exporter::export_ok_tags('all');
35
36# TODO - allow the user to pick a set of the three formats to allow
37# or just assume want to auto-detect any of the three formats.
38
39sub new
40{
41 my $class = shift ;
42 my $obj = createSelfTiedObject($class, \$AnyUncompressError);
43 $obj->_create(undef, 0, @_);
44}
45
46sub anyuncompress
47{
48 my $obj = createSelfTiedObject(undef, \$AnyUncompressError);
49 return $obj->_inf(@_) ;
50}
51
52sub getExtraParams
53{
54 return ();
55}
56
57sub ckParams
58{
59 my $self = shift ;
60 my $got = shift ;
61
62 # any always needs both crc32 and adler32
63 $got->value('CRC32' => 1);
64 $got->value('ADLER32' => 1);
65
66 return 1;
67}
68
69sub mkUncomp
70{
71 my $self = shift ;
72 my $class = shift ;
73 my $got = shift ;
74
75 # try zlib first
76 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Inflate::mkUncompObject();
77
78 return $self->saveErrorString(undef, $errstr, $errno)
79 if ! defined $obj;
80
81 *$self->{Uncomp} = $obj;
82
83 my $magic = $self->ckMagic( qw( RawInflate Inflate Gunzip Unzip ) );
84
85 if ($magic) {
86 *$self->{Info} = $self->readHeader($magic)
87 or return undef ;
88
89 return 1;
90 }
91
92 #foreach my $type ( qw( Bunzip2 UnLzop ) ) {
93 if (defined $IO::Uncompress::Bunzip2::VERSION and
94 $magic = $self->ckMagic('Bunzip2')) {
95 *$self->{Info} = $self->readHeader($magic)
96 or return undef ;
97
98 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Bunzip2::mkUncompObject();
99
100 return $self->saveErrorString(undef, $errstr, $errno)
101 if ! defined $obj;
102
103 *$self->{Uncomp} = $obj;
104
105 return 1;
106 }
107 elsif (defined $IO::Uncompress::UnLzop::VERSION and
108 $magic = $self->ckMagic('UnLzop')) {
109
110 *$self->{Info} = $self->readHeader($magic)
111 or return undef ;
112
113 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::LZO::mkUncompObject();
114
115 return $self->saveErrorString(undef, $errstr, $errno)
116 if ! defined $obj;
117
118 *$self->{Uncomp} = $obj;
119
120 return 1;
121 }
122
123 return 0 ;
124}
125
126
127
128sub ckMagic
129{
130 my $self = shift;
131 my @names = @_ ;
132
133 my $keep = ref $self ;
134 for my $class ( map { "IO::Uncompress::$_" } @names)
135 {
136 bless $self => $class;
137 my $magic = $self->ckMagic();
138
139 if ($magic)
140 {
141 #bless $self => $class;
142 return $magic ;
143 }
144
145 $self->pushBack(*$self->{HeaderPending}) ;
146 *$self->{HeaderPending} = '' ;
147 }
148
149 bless $self => $keep;
150 return undef;
151}
152
1531 ;
154
155__END__
156
157
158=head1 NAME
159
160
161IO::Uncompress::AnyUncompress - Perl interface to read 1950, 1951 & 1952 files/buffers
162
163
164=head1 SYNOPSIS
165
166 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
167
168 my $status = anyuncompress $input => $output [,OPTS]
169 or die "anyuncompress failed: $AnyUncompressError\n";
170
171 my $z = new IO::Uncompress::AnyUncompress $input [OPTS]
172 or die "anyuncompress failed: $AnyUncompressError\n";
173
174 $status = $z->read($buffer)
175 $status = $z->read($buffer, $length)
176 $status = $z->read($buffer, $length, $offset)
177 $line = $z->getline()
178 $char = $z->getc()
179 $char = $z->ungetc()
180 $char = $z->opened()
181
182 $z->trailingData()
183 $data = $z->getHeaderInfo()
184 $z->tell()
185 $z->seek($position, $whence)
186 $z->binmode()
187 $z->fileno()
188 $z->eof()
189 $z->close()
190
191 $AnyUncompressError ;
192
193 # IO::File mode
194
195 <$z>
196 read($z, $buffer);
197 read($z, $buffer, $length);
198 read($z, $buffer, $length, $offset);
199 tell($z)
200 seek($z, $position, $whence)
201 binmode($z)
202 fileno($z)
203 eof($z)
204 close($z)
205
206
207=head1 DESCRIPTION
208
209
210
211B<WARNING -- This is a Beta release>.
212
213=over 5
214
215=item * DO NOT use in production code.
216
217=item * The documentation is incomplete in places.
218
219=item * Parts of the interface defined here are tentative.
220
221=item * Please report any problems you find.
222
223=back
224
225
226
227
228
229This module provides a Perl interface that allows the reading of
230any files/buffers.
231
232For writing 1950, 1951 & 1952 files/buffers, see the companion module IO::Compress::RawDeflate.
233
234
235
236=head1 Functional Interface
237
238A top-level function, C<anyuncompress>, is provided to carry out
239"one-shot" uncompression between buffers and/or files. For finer
240control over the uncompression process, see the L</"OO Interface">
241section.
242
243 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
244
245 anyuncompress $input => $output [,OPTS]
246 or die "anyuncompress failed: $AnyUncompressError\n";
247
248
249
250The functional interface needs Perl5.005 or better.
251
252
253=head2 anyuncompress $input => $output [, OPTS]
254
255
256C<anyuncompress> expects at least two parameters, C<$input> and C<$output>.
257
258=head3 The C<$input> parameter
259
260The parameter, C<$input>, is used to define the source of
261the compressed data.
262
263It can take one of the following forms:
264
265=over 5
266
267=item A filename
268
269If the C<$input> parameter is a simple scalar, it is assumed to be a
270filename. This file will be opened for reading and the input data
271will be read from it.
272
273=item A filehandle
274
275If the C<$input> parameter is a filehandle, the input data will be
276read from it.
277The string '-' can be used as an alias for standard input.
278
279=item A scalar reference
280
281If C<$input> is a scalar reference, the input data will be read
282from C<$$input>.
283
284=item An array reference
285
286If C<$input> is an array reference, each element in the array must be a
287filename.
288
289The input data will be read from each file in turn.
290
291The complete array will be walked to ensure that it only
292contains valid filenames before any data is uncompressed.
293
294
295
296=item An Input FileGlob string
297
298If C<$input> is a string that is delimited by the characters "<" and ">"
299C<anyuncompress> will assume that it is an I<input fileglob string>. The
300input is the list of files that match the fileglob.
301
302If the fileglob does not match any files ...
303
304See L<File::GlobMapper|File::GlobMapper> for more details.
305
306
307=back
308
309If the C<$input> parameter is any other type, C<undef> will be returned.
310
311
312
313=head3 The C<$output> parameter
314
315The parameter C<$output> is used to control the destination of the
316uncompressed data. This parameter can take one of these forms.
317
318=over 5
319
320=item A filename
321
322If the C<$output> parameter is a simple scalar, it is assumed to be a
323filename. This file will be opened for writing and the uncompressed
324data will be written to it.
325
326=item A filehandle
327
328If the C<$output> parameter is a filehandle, the uncompressed data
329will be written to it.
330The string '-' can be used as an alias for standard output.
331
332
333=item A scalar reference
334
335If C<$output> is a scalar reference, the uncompressed data will be
336stored in C<$$output>.
337
338
339
340=item An Array Reference
341
342If C<$output> is an array reference, the uncompressed data will be
343pushed onto the array.
344
345=item An Output FileGlob
346
347If C<$output> is a string that is delimited by the characters "<" and ">"
348C<anyuncompress> will assume that it is an I<output fileglob string>. The
349output is the list of files that match the fileglob.
350
351When C<$output> is an fileglob string, C<$input> must also be a fileglob
352string. Anything else is an error.
353
354=back
355
356If the C<$output> parameter is any other type, C<undef> will be returned.
357
358
359
360=head2 Notes
361
362When C<$input> maps to multiple files/buffers and C<$output> is a single
363file/buffer the uncompressed input files/buffers will all be stored
364in C<$output> as a single uncompressed stream.
365
366
367
368=head2 Optional Parameters
369
370Unless specified below, the optional parameters for C<anyuncompress>,
371C<OPTS>, are the same as those used with the OO interface defined in the
372L</"Constructor Options"> section below.
373
374=over 5
375
376=item AutoClose =E<gt> 0|1
377
378This option applies to any input or output data streams to
379C<anyuncompress> that are filehandles.
380
381If C<AutoClose> is specified, and the value is true, it will result in all
382input and/or output filehandles being closed once C<anyuncompress> has
383completed.
384
385This parameter defaults to 0.
386
387
388
389=item BinModeOut =E<gt> 0|1
390
391When writing to a file or filehandle, set C<binmode> before writing to the
392file.
393
394Defaults to 0.
395
396
397
398
399
400=item -Append =E<gt> 0|1
401
402TODO
403
404=item -MultiStream =E<gt> 0|1
405
406Creates a new stream after each file.
407
408Defaults to 1.
409
410
411
412=back
413
414
415
416
417=head2 Examples
418
419To read the contents of the file C<file1.txt.Compressed> and write the
420compressed data to the file C<file1.txt>.
421
422 use strict ;
423 use warnings ;
424 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
425
426 my $input = "file1.txt.Compressed";
427 my $output = "file1.txt";
428 anyuncompress $input => $output
429 or die "anyuncompress failed: $AnyUncompressError\n";
430
431
432To read from an existing Perl filehandle, C<$input>, and write the
433uncompressed data to a buffer, C<$buffer>.
434
435 use strict ;
436 use warnings ;
437 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
438 use IO::File ;
439
440 my $input = new IO::File "<file1.txt.Compressed"
441 or die "Cannot open 'file1.txt.Compressed': $!\n" ;
442 my $buffer ;
443 anyuncompress $input => \$buffer
444 or die "anyuncompress failed: $AnyUncompressError\n";
445
446To uncompress all files in the directory "/my/home" that match "*.txt.Compressed" and store the compressed data in the same directory
447
448 use strict ;
449 use warnings ;
450 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
451
452 anyuncompress '</my/home/*.txt.Compressed>' => '</my/home/#1.txt>'
453 or die "anyuncompress failed: $AnyUncompressError\n";
454
455and if you want to compress each file one at a time, this will do the trick
456
457 use strict ;
458 use warnings ;
459 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
460
461 for my $input ( glob "/my/home/*.txt.Compressed" )
462 {
463 my $output = $input;
464 $output =~ s/.Compressed// ;
465 anyuncompress $input => $output
466 or die "Error compressing '$input': $AnyUncompressError\n";
467 }
468
469=head1 OO Interface
470
471=head2 Constructor
472
473The format of the constructor for IO::Uncompress::AnyUncompress is shown below
474
475
476 my $z = new IO::Uncompress::AnyUncompress $input [OPTS]
477 or die "IO::Uncompress::AnyUncompress failed: $AnyUncompressError\n";
478
479Returns an C<IO::Uncompress::AnyUncompress> object on success and undef on failure.
480The variable C<$AnyUncompressError> will contain an error message on failure.
481
482If you are running Perl 5.005 or better the object, C<$z>, returned from
483IO::Uncompress::AnyUncompress can be used exactly like an L<IO::File|IO::File> filehandle.
484This means that all normal input file operations can be carried out with
485C<$z>. For example, to read a line from a compressed file/buffer you can
486use either of these forms
487
488 $line = $z->getline();
489 $line = <$z>;
490
491The mandatory parameter C<$input> is used to determine the source of the
492compressed data. This parameter can take one of three forms.
493
494=over 5
495
496=item A filename
497
498If the C<$input> parameter is a scalar, it is assumed to be a filename. This
499file will be opened for reading and the compressed data will be read from it.
500
501=item A filehandle
502
503If the C<$input> parameter is a filehandle, the compressed data will be
504read from it.
505The string '-' can be used as an alias for standard input.
506
507
508=item A scalar reference
509
510If C<$input> is a scalar reference, the compressed data will be read from
511C<$$output>.
512
513=back
514
515=head2 Constructor Options
516
517
518The option names defined below are case insensitive and can be optionally
519prefixed by a '-'. So all of the following are valid
520
521 -AutoClose
522 -autoclose
523 AUTOCLOSE
524 autoclose
525
526OPTS is a combination of the following options:
527
528=over 5
529
530=item -AutoClose =E<gt> 0|1
531
532This option is only valid when the C<$input> parameter is a filehandle. If
533specified, and the value is true, it will result in the file being closed once
534either the C<close> method is called or the IO::Uncompress::AnyUncompress object is
535destroyed.
536
537This parameter defaults to 0.
538
539=item -MultiStream =E<gt> 0|1
540
541
542
543Allows multiple concatenated compressed streams to be treated as a single
544compressed stream. Decompression will stop once either the end of the
545file/buffer is reached, an error is encountered (premature eof, corrupt
546compressed data) or the end of a stream is not immediately followed by the
547start of another stream.
548
549This parameter defaults to 0.
550
551
552
553=item -Prime =E<gt> $string
554
555This option will uncompress the contents of C<$string> before processing the
556input file/buffer.
557
558This option can be useful when the compressed data is embedded in another
559file/data structure and it is not possible to work out where the compressed
560data begins without having to read the first few bytes. If this is the
561case, the uncompression can be I<primed> with these bytes using this
562option.
563
564=item -Transparent =E<gt> 0|1
565
566If this option is set and the input file or buffer is not compressed data,
567the module will allow reading of it anyway.
568
569This option defaults to 1.
570
571=item -BlockSize =E<gt> $num
572
573When reading the compressed input data, IO::Uncompress::AnyUncompress will read it in
574blocks of C<$num> bytes.
575
576This option defaults to 4096.
577
578=item -InputLength =E<gt> $size
579
580When present this option will limit the number of compressed bytes read
581from the input file/buffer to C<$size>. This option can be used in the
582situation where there is useful data directly after the compressed data
583stream and you know beforehand the exact length of the compressed data
584stream.
585
586This option is mostly used when reading from a filehandle, in which case
587the file pointer will be left pointing to the first byte directly after the
588compressed data stream.
589
590
591
592This option defaults to off.
593
594=item -Append =E<gt> 0|1
595
596This option controls what the C<read> method does with uncompressed data.
597
598If set to 1, all uncompressed data will be appended to the output parameter
599of the C<read> method.
600
601If set to 0, the contents of the output parameter of the C<read> method
602will be overwritten by the uncompressed data.
603
604Defaults to 0.
605
606=item -Strict =E<gt> 0|1
607
608
609
610This option controls whether the extra checks defined below are used when
611carrying out the decompression. When Strict is on, the extra tests are
612carried out, when Strict is off they are not.
613
614The default for this option is off.
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629=back
630
631=head2 Examples
632
633TODO
634
635=head1 Methods
636
637=head2 read
638
639Usage is
640
641 $status = $z->read($buffer)
642
643Reads a block of compressed data (the size the the compressed block is
644determined by the C<Buffer> option in the constructor), uncompresses it and
645writes any uncompressed data into C<$buffer>. If the C<Append> parameter is
646set in the constructor, the uncompressed data will be appended to the
647C<$buffer> parameter. Otherwise C<$buffer> will be overwritten.
648
649Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
650or a negative number on error.
651
652=head2 read
653
654Usage is
655
656 $status = $z->read($buffer, $length)
657 $status = $z->read($buffer, $length, $offset)
658
659 $status = read($z, $buffer, $length)
660 $status = read($z, $buffer, $length, $offset)
661
662Attempt to read C<$length> bytes of uncompressed data into C<$buffer>.
663
664The main difference between this form of the C<read> method and the
665previous one, is that this one will attempt to return I<exactly> C<$length>
666bytes. The only circumstances that this function will not is if end-of-file
667or an IO error is encountered.
668
669Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
670or a negative number on error.
671
672
673=head2 getline
674
675Usage is
676
677 $line = $z->getline()
678 $line = <$z>
679
680Reads a single line.
681
682This method fully supports the use of of the variable C<$/>
683(or C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to
684determine what constitutes an end of line. Both paragraph mode and file
685slurp mode are supported.
686
687
688=head2 getc
689
690Usage is
691
692 $char = $z->getc()
693
694Read a single character.
695
696=head2 ungetc
697
698Usage is
699
700 $char = $z->ungetc($string)
701
702
703
704
705=head2 getHeaderInfo
706
707Usage is
708
709 $hdr = $z->getHeaderInfo();
710 @hdrs = $z->getHeaderInfo();
711
712This method returns either a hash reference (in scalar context) or a list
713or hash references (in array context) that contains information about each
714of the header fields in the compressed data stream(s).
715
716
717
718
719=head2 tell
720
721Usage is
722
723 $z->tell()
724 tell $z
725
726Returns the uncompressed file offset.
727
728=head2 eof
729
730Usage is
731
732 $z->eof();
733 eof($z);
734
735
736
737Returns true if the end of the compressed input stream has been reached.
738
739
740
741=head2 seek
742
743 $z->seek($position, $whence);
744 seek($z, $position, $whence);
745
746
747
748
749Provides a sub-set of the C<seek> functionality, with the restriction
750that it is only legal to seek forward in the input file/buffer.
751It is a fatal error to attempt to seek backward.
752
753
754
755The C<$whence> parameter takes one the usual values, namely SEEK_SET,
756SEEK_CUR or SEEK_END.
757
758Returns 1 on success, 0 on failure.
759
760=head2 binmode
761
762Usage is
763
764 $z->binmode
765 binmode $z ;
766
767This is a noop provided for completeness.
768
769=head2 opened
770
771 $z->opened()
772
773Returns true if the object currently refers to a opened file/buffer.
774
775=head2 autoflush
776
777 my $prev = $z->autoflush()
778 my $prev = $z->autoflush(EXPR)
779
780If the C<$z> object is associated with a file or a filehandle, this method
781returns the current autoflush setting for the underlying filehandle. If
782C<EXPR> is present, and is non-zero, it will enable flushing after every
783write/print operation.
784
785If C<$z> is associated with a buffer, this method has no effect and always
786returns C<undef>.
787
788B<Note> that the special variable C<$|> B<cannot> be used to set or
789retrieve the autoflush setting.
790
791=head2 input_line_number
792
793 $z->input_line_number()
794 $z->input_line_number(EXPR)
795
796
797
798Returns the current uncompressed line number. If C<EXPR> is present it has
799the effect of setting the line number. Note that setting the line number
800does not change the current position within the file/buffer being read.
801
802The contents of C<$/> are used to to determine what constitutes a line
803terminator.
804
805
806
807=head2 fileno
808
809 $z->fileno()
810 fileno($z)
811
812If the C<$z> object is associated with a file or a filehandle, this method
813will return the underlying file descriptor.
814
815If the C<$z> object is is associated with a buffer, this method will
816return undef.
817
818=head2 close
819
820 $z->close() ;
821 close $z ;
822
823
824
825Closes the output file/buffer.
826
827
828
829For most versions of Perl this method will be automatically invoked if
830the IO::Uncompress::AnyUncompress object is destroyed (either explicitly or by the
831variable with the reference to the object going out of scope). The
832exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
833these cases, the C<close> method will be called automatically, but
834not until global destruction of all live objects when the program is
835terminating.
836
837Therefore, if you want your scripts to be able to run on all versions
838of Perl, you should call C<close> explicitly and not rely on automatic
839closing.
840
841Returns true on success, otherwise 0.
842
843If the C<AutoClose> option has been enabled when the IO::Uncompress::AnyUncompress
844object was created, and the object is associated with a file, the
845underlying file will also be closed.
846
847
848
849
850=head1 Importing
851
852No symbolic constants are required by this IO::Uncompress::AnyUncompress at present.
853
854=over 5
855
856=item :all
857
858Imports C<anyuncompress> and C<$AnyUncompressError>.
859Same as doing this
860
861 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
862
863=back
864
865=head1 EXAMPLES
866
867
868
869
870=head1 SEE ALSO
871
872L<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>
873
874L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
875
876L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
877L<Archive::Tar|Archive::Tar>,
878L<IO::Zlib|IO::Zlib>
879
880
881
882
883
884
885
886
887=head1 AUTHOR
888
889The I<IO::Uncompress::AnyUncompress> module was written by Paul Marquess,
890F<pmqs@cpan.org>.
891
892
893
894=head1 MODIFICATION HISTORY
895
896See the Changes file.
897
898=head1 COPYRIGHT AND LICENSE
899
900
901Copyright (c) 2005-2006 Paul Marquess. All rights reserved.
902
903This program is free software; you can redistribute it and/or
904modify it under the same terms as Perl itself.
905