IO-Compress-2.212/0000755000175000017500000000000014613172627012315 5ustar paulpaulIO-Compress-2.212/MANIFEST0000644000175000017500000000747314224372656013463 0ustar paulpaulChanges bin/zipdetails perl bin/streamzip perl examples/io/anycat perl examples/io/bzip2/bzcat perl examples/io/bzip2/bzgrep perl examples/io/bzip2/bzstream perl examples/io/gzip/gzappend perl examples/io/gzip/gzcat perl examples/io/gzip/gzgrep perl examples/io/gzip/gzstream perl examples/compress-zlib/filtinf perl examples/compress-zlib/filtdef perl examples/compress-zlib/gzcat perl examples/compress-zlib/gzgrep perl examples/compress-zlib/gzstream perl lib/Compress/Zlib.pm lib/File/GlobMapper.pm lib/IO/Compress/FAQ.pod lib/IO/Compress/Adapter/Bzip2.pm lib/IO/Compress/Adapter/Deflate.pm lib/IO/Compress/Adapter/Identity.pm lib/IO/Compress/Base/Common.pm lib/IO/Compress/Base.pm lib/IO/Compress/Bzip2.pm lib/IO/Compress/Deflate.pm lib/IO/Compress/Gzip/Constants.pm lib/IO/Compress/Gzip.pm lib/IO/Compress/RawDeflate.pm lib/IO/Compress/Zip/Constants.pm lib/IO/Compress/Zip.pm lib/IO/Compress/Zlib/Constants.pm lib/IO/Compress/Zlib/Extra.pm lib/IO/Uncompress/Adapter/Bunzip2.pm lib/IO/Uncompress/Adapter/Identity.pm lib/IO/Uncompress/Adapter/Inflate.pm lib/IO/Uncompress/AnyInflate.pm lib/IO/Uncompress/AnyUncompress.pm lib/IO/Uncompress/Base.pm lib/IO/Uncompress/Bunzip2.pm lib/IO/Uncompress/Gunzip.pm lib/IO/Uncompress/Inflate.pm lib/IO/Uncompress/RawInflate.pm lib/IO/Uncompress/Unzip.pm Makefile.PL MANIFEST private/MakeUtil.pm README t/000prereq.t t/001bzip2.t t/001zlib-generic-deflate.t t/001zlib-generic-gzip.t t/001zlib-generic-rawdeflate.t t/001zlib-generic-zip.t t/002any-deflate.t t/002any-gzip.t t/002any-rawdeflate.t t/002any-transparent.t t/002any-zip.t t/004gziphdr.t t/005defhdr.t t/006zip.t t/010examples-bzip2.t t/010examples-zlib.t t/011-streamzip.t t/01misc.t t/020isize.t t/050interop-gzip.t t/100generic-bzip2.t t/100generic-deflate.t t/100generic-gzip.t t/100generic-rawdeflate.t t/100generic-zip.t t/101truncate-bzip2.t t/101truncate-deflate.t t/101truncate-gzip.t t/101truncate-rawdeflate.t t/101truncate-zip.t t/102tied-bzip2.t t/102tied-deflate.t t/102tied-gzip.t t/102tied-rawdeflate.t t/102tied-zip.t t/103newtied-bzip2.t t/103newtied-deflate.t t/103newtied-gzip.t t/103newtied-rawdeflate.t t/103newtied-zip.t t/104destroy-bzip2.t t/104destroy-deflate.t t/104destroy-gzip.t t/104destroy-rawdeflate.t t/104destroy-zip.t t/105oneshot-bzip2.t t/105oneshot-deflate.t t/105oneshot-gzip-only.t t/105oneshot-gzip.t t/105oneshot-rawdeflate.t t/105oneshot-zip-bzip2-only.t t/105oneshot-zip-only.t t/105oneshot-zip-store-only.t t/105oneshot-zip.t t/106prime-bzip2.t t/106prime-deflate.t t/106prime-gzip.t t/106prime-rawdeflate.t t/106prime-zip.t t/107multi-bzip2.t t/107multi-deflate.t t/107multi-gzip.t t/107multi-rawdeflate.t t/107multi-zip.t t/107multi-zip-only.t t/108anyunc-bzip2.t t/108anyunc-deflate.t t/108anyunc-gzip.t t/108anyunc-rawdeflate.t t/108anyunc-transparent.t t/108anyunc-zip.t t/109merge-deflate.t t/109merge-gzip.t t/109merge-rawdeflate.t t/109merge-zip.t t/110encode-bzip2.t t/110encode-deflate.t t/110encode-gzip.t t/110encode-rawdeflate.t t/110encode-zip.t t/111const-deflate.t t/112utf8-zip.t t/113issues.t t/999meta-json.t t/999meta-yml.t t/999pod.t t/cz-01version.t t/cz-03zlib-v1.t t/cz-05examples.t t/cz-06gzsetp.t t/cz-08encoding.t t/cz-14gzopen.t t/compress/any.pl t/compress/anyunc.pl t/compress/CompTestUtils.pm t/compress/destroy.pl t/compress/encode.pl t/compress/generic.pl t/compress/merge.pl t/compress/multi.pl t/compress/newtied.pl t/compress/oneshot.pl t/compress/prime.pl t/compress/tied.pl t/compress/truncate.pl t/compress/zlib-generic.pl t/files/bad-efs.zip t/files/meta.xml t/files/test.ods t/files/testfile1.odt t/files/encrypt-aes.zip t/files/encrypt-standard.zip t/files/jar.zip t/globmapper.t t/Test/Builder.pm t/Test/More.pm META.yml Module meta-data (added by MakeMaker) t/Test/Simple.pm META.json Module JSON meta-data (added by MakeMaker) IO-Compress-2.212/examples/0000755000175000017500000000000014613172626014132 5ustar paulpaulIO-Compress-2.212/examples/io/0000755000175000017500000000000014613172626014541 5ustar paulpaulIO-Compress-2.212/examples/io/anycat0000755000175000017500000000055013445525324015745 0ustar paulpaul#!/usr/local/bin/perl use strict ; use warnings ; use IO::Uncompress::AnyUncompress qw( anyuncompress $AnyUncompressError ); @ARGV = '-' unless @ARGV ; foreach my $file (@ARGV) { anyuncompress $file => '-', Transparent => 1, Strict => 0, or die "Cannot uncompress '$file': $AnyUncompressError\n" ; } IO-Compress-2.212/examples/io/gzip/0000755000175000017500000000000014613172626015512 5ustar paulpaulIO-Compress-2.212/examples/io/gzip/gzgrep0000755000175000017500000000142413747240551016737 0ustar paulpaul#!/usr/bin/perl use strict ; use warnings ; use IO::Uncompress::Gunzip qw($GunzipError); die "Usage: gzgrep pattern [file...]\n" unless @ARGV >= 1; my $pattern = shift ; my $file ; @ARGV = '-' unless @ARGV ; foreach $file (@ARGV) { my $gz = new IO::Uncompress::Gunzip $file or die "Cannot uncompress $file: $GunzipError\n" ; while (<$gz>) { print if /$pattern/ ; } die "Error reading from $file: $GunzipError\n" if $GunzipError ; } __END__ foreach $file (@ARGV) { my $gz = gzopen($file, "rb") or die "Cannot open $file: $gzerrno\n" ; while ($gz->gzreadline($_) > 0) { print if /$pattern/ ; } die "Error reading from $file: $gzerrno\n" if $gzerrno != Z_STREAM_END ; $gz->gzclose() ; } IO-Compress-2.212/examples/io/gzip/gzappend0000644000175000017500000000061613747240534017251 0ustar paulpaul#!/usr/local/bin/perl use IO::Compress::Gzip qw( $GzipError ); use strict ; use warnings ; die "Usage: gzappend gz-file file...\n" unless @ARGV ; my $output = shift @ARGV ; @ARGV = '-' unless @ARGV ; my $gz = new IO::Compress::Gzip $output, Merge => 1 or die "Cannot open $output: $GzipError\n" ; $gz->write( [@ARGV] ) or die "Cannot open $output: $GzipError\n" ; $gz->close; IO-Compress-2.212/examples/io/gzip/gzcat0000755000175000017500000000075613747240537016564 0ustar paulpaul#!/usr/local/bin/perl use IO::Uncompress::Gunzip qw( $GunzipError ); use strict ; use warnings ; #die "Usage: gzcat file...\n" # unless @ARGV ; my $file ; my $buffer ; my $s; @ARGV = '-' unless @ARGV ; foreach $file (@ARGV) { my $gz = new IO::Uncompress::Gunzip $file or die "Cannot open $file: $GunzipError\n" ; print $buffer while ($s = $gz->read($buffer)) > 0 ; die "Error reading from $file: $GunzipError\n" if $s < 0 ; $gz->close() ; } IO-Compress-2.212/examples/io/gzip/gzstream0000755000175000017500000000100713747240556017277 0ustar paulpaul#!/usr/local/bin/perl use strict ; use warnings ; use IO::Compress::Gzip qw(gzip $GzipError); gzip '-' => '-', Minimal => 1 or die "gzstream: $GzipError\n" ; #exit 0; __END__ #my $gz = new IO::Compress::Gzip *STDOUT my $gz = new IO::Compress::Gzip '-' or die "gzstream: Cannot open stdout as gzip stream: $GzipError\n" ; while (<>) { $gz->write($_) or die "gzstream: Error writing gzip output stream: $GzipError\n" ; } $gz->close or die "gzstream: Error closing gzip output stream: $GzipError\n" ; IO-Compress-2.212/examples/io/bzip2/0000755000175000017500000000000014613172626015567 5ustar paulpaulIO-Compress-2.212/examples/io/bzip2/bzstream0000755000175000017500000000021513445525324017341 0ustar paulpaul#!/usr/local/bin/perl use strict ; use warnings ; use IO::Compress::Bzip2 qw(:all); bzip2 '-' => '-' or die "bzstream: $Bzip2Error\n" ; IO-Compress-2.212/examples/io/bzip2/bzcat0000755000175000017500000000076313747240514016625 0ustar paulpaul#!/usr/local/bin/perl use IO::Uncompress::Bunzip2 qw( $Bunzip2Error ); use strict ; use warnings ; #die "Usage: gzcat file...\n" # unless @ARGV ; my $file ; my $buffer ; my $s; @ARGV = '-' unless @ARGV ; foreach $file (@ARGV) { my $gz = new IO::Uncompress::Bunzip2 $file or die "Cannot open $file: $Bunzip2Error\n" ; print $buffer while ($s = $gz->read($buffer)) > 0 ; die "Error reading from $file: $Bunzip2Error\n" if $s < 0 ; $gz->close() ; } IO-Compress-2.212/examples/io/bzip2/bzgrep0000755000175000017500000000075513747240526017017 0ustar paulpaul#!/usr/bin/perl use strict ; use warnings ; use IO::Uncompress::Bunzip2 qw($Bunzip2Error); die "Usage: gzgrep pattern [file...]\n" unless @ARGV >= 1; my $pattern = shift ; my $file ; @ARGV = '-' unless @ARGV ; foreach $file (@ARGV) { my $gz = new IO::Uncompress::Bunzip2 $file or die "Cannot uncompress $file: $Bunzip2Error\n" ; while (<$gz>) { print if /$pattern/ ; } die "Error reading from $file: $Bunzip2Error\n" if $Bunzip2Error ; } IO-Compress-2.212/examples/compress-zlib/0000755000175000017500000000000014613172626016723 5ustar paulpaulIO-Compress-2.212/examples/compress-zlib/gzgrep0000755000175000017500000000071713747240477020163 0ustar paulpaul#!/usr/local/bin/perl use strict ; use warnings ; use Compress::Zlib ; die "Usage: gzgrep pattern file...\n" unless @ARGV >= 2; my $pattern = shift ; my $file ; foreach $file (@ARGV) { my $gz = gzopen($file, "rb") or die "Cannot open $file: $gzerrno\n" ; while ($gz->gzreadline($_) > 0) { print if /$pattern/ ; } die "Error reading from $file: $gzerrno\n" if $gzerrno != Z_STREAM_END ; $gz->gzclose() ; } IO-Compress-2.212/examples/compress-zlib/filtdef0000755000175000017500000000067313445525324020273 0ustar paulpaul#!/usr/local/bin/perl use strict ; use warnings ; use Compress::Zlib ; binmode STDIN; binmode STDOUT; my $x = deflateInit() or die "Cannot create a deflation stream\n" ; my ($output, $status) ; while (<>) { ($output, $status) = $x->deflate($_) ; $status == Z_OK or die "deflation failed\n" ; print $output ; } ($output, $status) = $x->flush() ; $status == Z_OK or die "deflation failed\n" ; print $output ; IO-Compress-2.212/examples/compress-zlib/gzcat0000755000175000017500000000075713747240473017775 0ustar paulpaul#!/usr/local/bin/perl use strict ; use warnings ; use Compress::Zlib ; #die "Usage: gzcat file...\n" # unless @ARGV ; my $filename ; @ARGV = '-' unless @ARGV ; foreach my $filename (@ARGV) { my $buffer ; my $gz = gzopen($filename, "rb") or die "Cannot open $filename: $gzerrno\n" ; print $buffer while $gz->gzread($buffer) > 0 ; die "Error reading from $filename: $gzerrno" . ($gzerrno+0) . "\n" if $gzerrno != Z_STREAM_END ; $gz->gzclose() ; } IO-Compress-2.212/examples/compress-zlib/filtinf0000755000175000017500000000073213747240467020314 0ustar paulpaul#!/usr/local/bin/perl use strict ; use warnings ; use Compress::Zlib ; my $x = inflateInit() or die "Cannot create a inflation stream\n" ; my $input = '' ; binmode STDIN; binmode STDOUT; my ($output, $status) ; while (read(STDIN, $input, 4096)) { ($output, $status) = $x->inflate(\$input) ; print $output if $status == Z_OK or $status == Z_STREAM_END ; last if $status != Z_OK ; } die "inflation failed\n" unless $status == Z_STREAM_END ; IO-Compress-2.212/examples/compress-zlib/gzstream0000755000175000017500000000047713747240503020512 0ustar paulpaul#!/usr/local/bin/perl use strict ; use warnings ; use Compress::Zlib ; binmode STDOUT; # gzopen only sets it on the fd #my $gz = gzopen(\*STDOUT, "wb") my $gz = gzopen('-', "wb") or die "Cannot open stdout: $gzerrno\n" ; while (<>) { $gz->gzwrite($_) or die "error writing: $gzerrno\n" ; } $gz->gzclose ; IO-Compress-2.212/private/0000755000175000017500000000000014613172626013766 5ustar paulpaulIO-Compress-2.212/private/MakeUtil.pm0000644000175000017500000001760613747240720016047 0ustar paulpaulpackage MakeUtil ; package main ; use strict ; use Config qw(%Config); use File::Copy; my $VERSION = '1.0'; BEGIN { eval { require File::Spec::Functions ; File::Spec::Functions->import() } ; if ($@) { *catfile = sub { return "$_[0]/$_[1]" } } } require VMS::Filespec if $^O eq 'VMS'; unless($ENV{PERL_CORE}) { $ENV{PERL_CORE} = 1 if grep { $_ eq 'PERL_CORE=1' } @ARGV; } $ENV{SKIP_FOR_CORE} = 1 if $ENV{PERL_CORE} || $ENV{MY_PERL_CORE} ; sub MY::libscan { my $self = shift; my $path = shift; return undef if $path =~ /(~|\.bak|_bak)$/ || $path =~ /\..*\.sw(o|p)$/ || $path =~ /\B\.svn\b/; return $path; } sub MY::postamble { return '' if $ENV{PERL_CORE} ; my @files = getPerlFiles('MANIFEST'); # Note: Once you remove all the layers of shell/makefile escaping # the regular expression below reads # # /^\s*local\s*\(\s*\$^W\s*\)/ # my $postamble = ' MyTrebleCheck: @echo Checking for $$^W in files: '. "@files" . ' perl -ne \' \ exit 1 if /^\s*local\s*\(\s*\$$\^W\s*\)/; \' \ ' . " @files || " . ' \ (echo found unexpected $$^W ; exit 1) @echo All is ok. '; return $postamble; } sub getPerlFiles { my @manifests = @_ ; my @files = (); for my $manifest (@manifests) { my $prefix = './'; $prefix = $1 if $manifest =~ m#^(.*/)#; open M, "<$manifest" or die "Cannot open '$manifest': $!\n"; while () { chomp ; next if /^\s*#/ || /^\s*$/ ; s/^\s+//; s/\s+$//; #next if m#t/Test/More\.pm$# or m#t/Test/Builder\.pm$#; /^(\S+)\s*(.*)$/; my ($file, $rest) = ($1, $2); if ($file =~ /\.(pm|pl|t)$/ and $file !~ /MakeUtil.pm/) { push @files, "$prefix$file"; } elsif ($rest =~ /perl/i) { push @files, "$prefix$file"; } } close M; } return @files; } sub UpDowngrade { return if defined $ENV{TipTop}; my @files = @_ ; # our and use bytes/utf8 is stable from 5.6.0 onward # warnings is stable from 5.6.1 onward # Note: this code assumes that each statement it modifies is not # split across multiple lines. my $warn_sub = ''; my $our_sub = '' ; my $upgrade ; my $downgrade ; my $do_downgrade ; my $caller = (caller(1))[3] || ''; if ($caller =~ /downgrade/) { $downgrade = 1; } elsif ($caller =~ /upgrade/) { $upgrade = 1; } else { $do_downgrade = 1 if $] < 5.006001 ; } # else # { # my $opt = shift @ARGV || '' ; # $upgrade = ($opt =~ /^-upgrade/i); # $downgrade = ($opt =~ /^-downgrade/i); # push @ARGV, $opt unless $downgrade || $upgrade; # } if ($downgrade || $do_downgrade) { # From: use|no warnings "blah" # To: local ($^W) = 1; # use|no warnings "blah" $warn_sub = sub { s/^(\s*)(no\s+warnings)/${1}local (\$^W) = 0; #$2/ ; s/^(\s*)(use\s+warnings)/${1}local (\$^W) = 1; #$2/ ; }; } #elsif ($] >= 5.006001 || $upgrade) { elsif ($upgrade) { # From: local ($^W) = 1; # use|no warnings "blah" # To: use|no warnings "blah" $warn_sub = sub { s/^(\s*)local\s*\(\$\^W\)\s*=\s*\d+\s*;\s*#\s*((no|use)\s+warnings.*)/$1$2/ ; }; } if ($downgrade || $do_downgrade) { $our_sub = sub { if ( /^(\s*)our\s+\(\s*([^)]+\s*)\)/ ) { my $indent = $1; my $vars = join ' ', split /\s*,\s*/, $2; $_ = "${indent}use vars qw($vars);\n"; } elsif ( /^(\s*)((use|no)\s+(bytes|utf8)\s*;.*)$/) { $_ = "$1# $2\n"; } }; } #elsif ($] >= 5.006000 || $upgrade) { elsif ($upgrade) { $our_sub = sub { if ( /^(\s*)use\s+vars\s+qw\((.*?)\)/ ) { my $indent = $1; my $vars = join ', ', split ' ', $2; $_ = "${indent}our ($vars);\n"; } elsif ( /^(\s*)#\s*((use|no)\s+(bytes|utf8)\s*;.*)$/) { $_ = "$1$2\n"; } }; } if (! $our_sub && ! $warn_sub) { warn "Up/Downgrade not needed.\n"; if ($upgrade || $downgrade) { exit 0 } else { return } } foreach (@files) { #if (-l $_ ) { doUpDown($our_sub, $warn_sub, $_) } #else #{ doUpDownViaCopy($our_sub, $warn_sub, $_) } } warn "Up/Downgrade complete.\n" ; exit 0 if $upgrade || $downgrade; } sub doUpDown { my $our_sub = shift; my $warn_sub = shift; return if -d $_[0]; local ($^I) = ($^O eq 'VMS') ? "_bak" : ".bak"; local (@ARGV) = shift; while (<>) { print, last if /^__(END|DATA)__/ ; &{ $our_sub }() if $our_sub ; &{ $warn_sub }() if $warn_sub ; print ; } return if eof ; while (<>) { print } } sub doUpDownViaCopy { my $our_sub = shift; my $warn_sub = shift; my $file = shift ; use File::Copy ; return if -d $file ; my $backup = $file . ($^O eq 'VMS') ? "_bak" : ".bak"; copy($file, $backup) or die "Cannot copy $file to $backup: $!"; my @keep = (); { open F, "<$file" or die "Cannot open $file: $!\n" ; while () { if (/^__(END|DATA)__/) { push @keep, $_; last ; } &{ $our_sub }() if $our_sub ; &{ $warn_sub }() if $warn_sub ; push @keep, $_; } if (! eof F) { while () { push @keep, $_ } } close F; } { open F, ">$file" or die "Cannot open $file: $!\n"; print F @keep ; close F; } } sub FindBrokenDependencies { my $version = shift ; my %thisModule = map { $_ => 1} @_; my @modules = qw( IO::Compress::Base IO::Compress::Base::Common IO::Uncompress::Base Compress::Raw::Zlib Compress::Raw::Bzip2 IO::Compress::RawDeflate IO::Uncompress::RawInflate IO::Compress::Deflate IO::Uncompress::Inflate IO::Compress::Gzip IO::Compress::Gzip::Constants IO::Uncompress::Gunzip IO::Compress::Zip IO::Uncompress::Unzip IO::Compress::Bzip2 IO::Uncompress::Bunzip2 IO::Compress::Lzf IO::Uncompress::UnLzf IO::Compress::Lzop IO::Uncompress::UnLzop Compress::Zlib ); my @broken = (); foreach my $module ( grep { ! $thisModule{$_} } @modules) { my $hasVersion = getInstalledVersion($module); # No need to upgrade if the module isn't installed at all next if ! defined $hasVersion; # If already have C::Z version 1, then an upgrade to any of the # IO::Compress modules will not break it. next if $module eq 'Compress::Zlib' && $hasVersion < 2; if ($hasVersion < $version) { push @broken, $module } } return @broken; } sub getInstalledVersion { my $module = shift; my $version; eval " require $module; "; if ($@ eq '') { no strict 'refs'; $version = ${ $module . "::VERSION" }; $version = 0 } return $version; } package MakeUtil ; 1; IO-Compress-2.212/lib/0000755000175000017500000000000014613172626013062 5ustar paulpaulIO-Compress-2.212/lib/Compress/0000755000175000017500000000000014613172626014655 5ustar paulpaulIO-Compress-2.212/lib/Compress/Zlib.pm0000644000175000017500000012605714613133740016120 0ustar paulpaul package Compress::Zlib; require 5.006 ; require Exporter; use Carp ; use IO::Handle ; use Scalar::Util qw(dualvar); use IO::Compress::Base::Common 2.212 ; use Compress::Raw::Zlib 2.212 ; use IO::Compress::Gzip 2.212 ; use IO::Uncompress::Gunzip 2.212 ; use strict ; use warnings ; use bytes ; our ($VERSION, $XS_VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); $VERSION = '2.212'; $XS_VERSION = $VERSION; $VERSION = eval $VERSION; @ISA = qw(Exporter); # Items to export into callers namespace by default. Note: do not export # names by default without a very good reason. Use EXPORT_OK instead. # Do not simply export all your public functions/methods/constants. @EXPORT = qw( deflateInit inflateInit compress uncompress gzopen $gzerrno ); push @EXPORT, @Compress::Raw::Zlib::EXPORT ; @EXPORT_OK = qw(memGunzip memGzip zlib_version); %EXPORT_TAGS = ( ALL => \@EXPORT ); BEGIN { *zlib_version = \&Compress::Raw::Zlib::zlib_version; } use constant FLAG_APPEND => 1 ; use constant FLAG_CRC => 2 ; use constant FLAG_ADLER => 4 ; use constant FLAG_CONSUME_INPUT => 8 ; our (@my_z_errmsg); @my_z_errmsg = ( "need dictionary", # Z_NEED_DICT 2 "stream end", # Z_STREAM_END 1 "", # Z_OK 0 "file error", # Z_ERRNO (-1) "stream error", # Z_STREAM_ERROR (-2) "data error", # Z_DATA_ERROR (-3) "insufficient memory", # Z_MEM_ERROR (-4) "buffer error", # Z_BUF_ERROR (-5) "incompatible version",# Z_VERSION_ERROR(-6) ); sub _set_gzerr { my $value = shift ; if ($value == 0) { $Compress::Zlib::gzerrno = 0 ; } elsif ($value == Z_ERRNO() || $value > 2) { $Compress::Zlib::gzerrno = $! ; } else { $Compress::Zlib::gzerrno = dualvar($value+0, $my_z_errmsg[2 - $value]); } return $value ; } sub _set_gzerr_undef { _set_gzerr(@_); return undef; } sub _save_gzerr { my $gz = shift ; my $test_eof = shift ; my $value = $gz->errorNo() || 0 ; my $eof = $gz->eof() ; if ($test_eof) { # gzread uses Z_STREAM_END to denote a successful end $value = Z_STREAM_END() if $gz->eof() && $value == 0 ; } _set_gzerr($value) ; } sub gzopen($$) { my ($file, $mode) = @_ ; my $gz ; my %defOpts = (Level => Z_DEFAULT_COMPRESSION(), Strategy => Z_DEFAULT_STRATEGY(), ); my $writing ; $writing = ! ($mode =~ /r/i) ; $writing = ($mode =~ /[wa]/i) ; $defOpts{Level} = $1 if $mode =~ /(\d)/; $defOpts{Strategy} = Z_FILTERED() if $mode =~ /f/i; $defOpts{Strategy} = Z_HUFFMAN_ONLY() if $mode =~ /h/i; $defOpts{Append} = 1 if $mode =~ /a/i; my $infDef = $writing ? 'deflate' : 'inflate'; my @params = () ; croak "gzopen: file parameter is not a filehandle or filename" unless isaFilehandle $file || isaFilename $file || (ref $file && ref $file eq 'SCALAR'); return undef unless $mode =~ /[rwa]/i ; _set_gzerr(0) ; if ($writing) { $gz = IO::Compress::Gzip->new($file, Minimal => 1, AutoClose => 1, %defOpts) or $Compress::Zlib::gzerrno = $IO::Compress::Gzip::GzipError; } else { $gz = IO::Uncompress::Gunzip->new($file, Transparent => 1, Append => 0, AutoClose => 1, MultiStream => 1, Strict => 0) or $Compress::Zlib::gzerrno = $IO::Uncompress::Gunzip::GunzipError; } return undef if ! defined $gz ; bless [$gz, $infDef], 'Compress::Zlib::gzFile'; } sub Compress::Zlib::gzFile::gzread { my $self = shift ; return _set_gzerr(Z_STREAM_ERROR()) if $self->[1] ne 'inflate'; my $len = defined $_[1] ? $_[1] : 4096 ; my $gz = $self->[0] ; if ($self->gzeof() || $len == 0) { # Zap the output buffer to match ver 1 behaviour. $_[0] = "" ; _save_gzerr($gz, 1); return 0 ; } my $status = $gz->read($_[0], $len) ; _save_gzerr($gz, 1); return $status ; } sub Compress::Zlib::gzFile::gzreadline { my $self = shift ; my $gz = $self->[0] ; { # Maintain backward compatibility with 1.x behaviour # It didn't support $/, so this can't either. local $/ = "\n" ; $_[0] = $gz->getline() ; } _save_gzerr($gz, 1); return defined $_[0] ? length $_[0] : 0 ; } sub Compress::Zlib::gzFile::gzwrite { my $self = shift ; my $gz = $self->[0] ; return _set_gzerr(Z_STREAM_ERROR()) if $self->[1] ne 'deflate'; $] >= 5.008 and (utf8::downgrade($_[0], 1) or croak "Wide character in gzwrite"); my $status = $gz->write($_[0]) ; _save_gzerr($gz); return $status ; } sub Compress::Zlib::gzFile::gztell { my $self = shift ; my $gz = $self->[0] ; my $status = $gz->tell() ; _save_gzerr($gz); return $status ; } sub Compress::Zlib::gzFile::gzseek { my $self = shift ; my $offset = shift ; my $whence = shift ; my $gz = $self->[0] ; my $status ; eval { local $SIG{__DIE__}; $status = $gz->seek($offset, $whence) ; }; if ($@) { my $error = $@; $error =~ s/^.*: /gzseek: /; $error =~ s/ at .* line \d+\s*$//; croak $error; } _save_gzerr($gz); return $status ; } sub Compress::Zlib::gzFile::gzflush { my $self = shift ; my $f = shift ; my $gz = $self->[0] ; my $status = $gz->flush($f) ; my $err = _save_gzerr($gz); return $status ? 0 : $err; } sub Compress::Zlib::gzFile::gzclose { my $self = shift ; my $gz = $self->[0] ; my $status = $gz->close() ; my $err = _save_gzerr($gz); return $status ? 0 : $err; } sub Compress::Zlib::gzFile::gzeof { my $self = shift ; my $gz = $self->[0] ; return 0 if $self->[1] ne 'inflate'; my $status = $gz->eof() ; _save_gzerr($gz); return $status ; } sub Compress::Zlib::gzFile::gzsetparams { my $self = shift ; croak "Usage: Compress::Zlib::gzFile::gzsetparams(file, level, strategy)" unless @_ eq 2 ; my $gz = $self->[0] ; my $level = shift ; my $strategy = shift; return _set_gzerr(Z_STREAM_ERROR()) if $self->[1] ne 'deflate'; my $status = *$gz->{Compress}->deflateParams(-Level => $level, -Strategy => $strategy); _save_gzerr($gz); return $status ; } sub Compress::Zlib::gzFile::gzerror { my $self = shift ; my $gz = $self->[0] ; return $Compress::Zlib::gzerrno ; } sub compress($;$) { my ($x, $output, $err, $in) =('', '', '', '') ; if (ref $_[0] ) { $in = $_[0] ; croak "not a scalar reference" unless ref $in eq 'SCALAR' ; } else { $in = \$_[0] ; } $] >= 5.008 and (utf8::downgrade($$in, 1) or croak "Wide character in compress"); my $level = (@_ == 2 ? $_[1] : Z_DEFAULT_COMPRESSION() ); $x = Compress::Raw::Zlib::_deflateInit(FLAG_APPEND, $level, Z_DEFLATED, MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY, 4096, '') or return undef ; $err = $x->deflate($in, $output) ; return undef unless $err == Z_OK() ; $err = $x->flush($output) ; return undef unless $err == Z_OK() ; return $output ; } sub uncompress($) { my ($output, $in) =('', '') ; if (ref $_[0] ) { $in = $_[0] ; croak "not a scalar reference" unless ref $in eq 'SCALAR' ; } else { $in = \$_[0] ; } $] >= 5.008 and (utf8::downgrade($$in, 1) or croak "Wide character in uncompress"); my ($obj, $status) = Compress::Raw::Zlib::_inflateInit(0, MAX_WBITS, 4096, "") ; $status == Z_OK or return undef; $obj->inflate($in, $output) == Z_STREAM_END or return undef; return $output; } sub deflateInit(@) { my ($got) = ParseParameters(0, { 'bufsize' => [IO::Compress::Base::Common::Parse_unsigned, 4096], 'level' => [IO::Compress::Base::Common::Parse_signed, Z_DEFAULT_COMPRESSION()], 'method' => [IO::Compress::Base::Common::Parse_unsigned, Z_DEFLATED()], 'windowbits' => [IO::Compress::Base::Common::Parse_signed, MAX_WBITS()], 'memlevel' => [IO::Compress::Base::Common::Parse_unsigned, MAX_MEM_LEVEL()], 'strategy' => [IO::Compress::Base::Common::Parse_unsigned, Z_DEFAULT_STRATEGY()], 'dictionary' => [IO::Compress::Base::Common::Parse_any, ""], }, @_ ) ; croak "Compress::Zlib::deflateInit: Bufsize must be >= 1, you specified " . $got->getValue('bufsize') unless $got->getValue('bufsize') >= 1; my $obj ; my $status = 0 ; ($obj, $status) = Compress::Raw::Zlib::_deflateInit(0, $got->getValue('level'), $got->getValue('method'), $got->getValue('windowbits'), $got->getValue('memlevel'), $got->getValue('strategy'), $got->getValue('bufsize'), $got->getValue('dictionary')) ; my $x = ($status == Z_OK() ? bless $obj, "Zlib::OldDeflate" : undef) ; return wantarray ? ($x, $status) : $x ; } sub inflateInit(@) { my ($got) = ParseParameters(0, { 'bufsize' => [IO::Compress::Base::Common::Parse_unsigned, 4096], 'windowbits' => [IO::Compress::Base::Common::Parse_signed, MAX_WBITS()], 'dictionary' => [IO::Compress::Base::Common::Parse_any, ""], }, @_) ; croak "Compress::Zlib::inflateInit: Bufsize must be >= 1, you specified " . $got->getValue('bufsize') unless $got->getValue('bufsize') >= 1; my $status = 0 ; my $obj ; ($obj, $status) = Compress::Raw::Zlib::_inflateInit(FLAG_CONSUME_INPUT, $got->getValue('windowbits'), $got->getValue('bufsize'), $got->getValue('dictionary')) ; my $x = ($status == Z_OK() ? bless $obj, "Zlib::OldInflate" : undef) ; wantarray ? ($x, $status) : $x ; } package Zlib::OldDeflate ; our (@ISA); @ISA = qw(Compress::Raw::Zlib::deflateStream); sub deflate { my $self = shift ; my $output ; my $status = $self->SUPER::deflate($_[0], $output) ; wantarray ? ($output, $status) : $output ; } sub flush { my $self = shift ; my $output ; my $flag = shift || Compress::Zlib::Z_FINISH(); my $status = $self->SUPER::flush($output, $flag) ; wantarray ? ($output, $status) : $output ; } package Zlib::OldInflate ; our (@ISA); @ISA = qw(Compress::Raw::Zlib::inflateStream); sub inflate { my $self = shift ; my $output ; my $status = $self->SUPER::inflate($_[0], $output) ; wantarray ? ($output, $status) : $output ; } package Compress::Zlib ; use IO::Compress::Gzip::Constants 2.212 ; sub memGzip($) { _set_gzerr(0); my $x = Compress::Raw::Zlib::_deflateInit(FLAG_APPEND|FLAG_CRC, Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS(), MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY, 4096, '') or return undef ; # if the deflation buffer isn't a reference, make it one my $string = (ref $_[0] ? $_[0] : \$_[0]) ; $] >= 5.008 and (utf8::downgrade($$string, 1) or croak "Wide character in memGzip"); my $out; my $status ; $x->deflate($string, $out) == Z_OK or return undef ; $x->flush($out) == Z_OK or return undef ; return IO::Compress::Gzip::Constants::GZIP_MINIMUM_HEADER . $out . pack("V V", $x->crc32(), $x->total_in()); } sub _removeGzipHeader($) { my $string = shift ; return Z_DATA_ERROR() if length($$string) < GZIP_MIN_HEADER_SIZE ; my ($magic1, $magic2, $method, $flags, $time, $xflags, $oscode) = unpack ('CCCCVCC', $$string); return Z_DATA_ERROR() unless $magic1 == GZIP_ID1 and $magic2 == GZIP_ID2 and $method == Z_DEFLATED() and !($flags & GZIP_FLG_RESERVED) ; substr($$string, 0, GZIP_MIN_HEADER_SIZE) = '' ; # skip extra field if ($flags & GZIP_FLG_FEXTRA) { return Z_DATA_ERROR() if length($$string) < GZIP_FEXTRA_HEADER_SIZE ; my ($extra_len) = unpack ('v', $$string); $extra_len += GZIP_FEXTRA_HEADER_SIZE; return Z_DATA_ERROR() if length($$string) < $extra_len ; substr($$string, 0, $extra_len) = ''; } # skip orig name if ($flags & GZIP_FLG_FNAME) { my $name_end = index ($$string, GZIP_NULL_BYTE); return Z_DATA_ERROR() if $name_end == -1 ; substr($$string, 0, $name_end + 1) = ''; } # skip comment if ($flags & GZIP_FLG_FCOMMENT) { my $comment_end = index ($$string, GZIP_NULL_BYTE); return Z_DATA_ERROR() if $comment_end == -1 ; substr($$string, 0, $comment_end + 1) = ''; } # skip header crc if ($flags & GZIP_FLG_FHCRC) { return Z_DATA_ERROR() if length ($$string) < GZIP_FHCRC_SIZE ; substr($$string, 0, GZIP_FHCRC_SIZE) = ''; } return Z_OK(); } sub _ret_gun_error { $Compress::Zlib::gzerrno = $IO::Uncompress::Gunzip::GunzipError; return undef; } sub memGunzip($) { # if the buffer isn't a reference, make it one my $string = (ref $_[0] ? $_[0] : \$_[0]); $] >= 5.008 and (utf8::downgrade($$string, 1) or croak "Wide character in memGunzip"); _set_gzerr(0); my $status = _removeGzipHeader($string) ; $status == Z_OK() or return _set_gzerr_undef($status); my $bufsize = length $$string > 4096 ? length $$string : 4096 ; my $x = Compress::Raw::Zlib::_inflateInit(FLAG_CRC | FLAG_CONSUME_INPUT, -MAX_WBITS(), $bufsize, '') or return _ret_gun_error(); my $output = '' ; $status = $x->inflate($string, $output); if ( $status == Z_OK() ) { _set_gzerr(Z_DATA_ERROR()); return undef; } return _ret_gun_error() if ($status != Z_STREAM_END()); if (length $$string >= 8) { my ($crc, $len) = unpack ("VV", substr($$string, 0, 8)); substr($$string, 0, 8) = ''; return _set_gzerr_undef(Z_DATA_ERROR()) unless $len == length($output) and $crc == Compress::Raw::Zlib::crc32($output); } else { $$string = ''; } return $output; } # Autoload methods go after __END__, and are processed by the autosplit program. 1; __END__ =head1 NAME Compress::Zlib - Interface to zlib compression library =head1 SYNOPSIS use Compress::Zlib ; ($d, $status) = deflateInit( [OPT] ) ; $status = $d->deflate($input, $output) ; $status = $d->flush([$flush_type]) ; $d->deflateParams(OPTS) ; $d->deflateTune(OPTS) ; $d->dict_adler() ; $d->crc32() ; $d->adler32() ; $d->total_in() ; $d->total_out() ; $d->msg() ; $d->get_Strategy(); $d->get_Level(); $d->get_BufSize(); ($i, $status) = inflateInit( [OPT] ) ; $status = $i->inflate($input, $output [, $eof]) ; $status = $i->inflateSync($input) ; $i->dict_adler() ; $d->crc32() ; $d->adler32() ; $i->total_in() ; $i->total_out() ; $i->msg() ; $d->get_BufSize(); $dest = compress($source) ; $dest = uncompress($source) ; $gz = gzopen($filename or filehandle, $mode) ; $bytesread = $gz->gzread($buffer [,$size]) ; $bytesread = $gz->gzreadline($line) ; $byteswritten = $gz->gzwrite($buffer) ; $status = $gz->gzflush($flush) ; $offset = $gz->gztell() ; $status = $gz->gzseek($offset, $whence) ; $status = $gz->gzclose() ; $status = $gz->gzeof() ; $status = $gz->gzsetparams($level, $strategy) ; $errstring = $gz->gzerror() ; $gzerrno $dest = Compress::Zlib::memGzip($buffer) ; $dest = Compress::Zlib::memGunzip($buffer) ; $crc = adler32($buffer [,$crc]) ; $crc = crc32($buffer [,$crc]) ; $crc = crc32_combine($crc1, $crc2, $len2); $adler = adler32_combine($adler1, $adler2, $len2); my $version = Compress::Raw::Zlib::zlib_version(); =head1 DESCRIPTION The I module provides a Perl interface to the I compression library (see L for details about where to get I). The C module can be split into two general areas of functionality, namely a simple read/write interface to I files and a low-level in-memory compression/decompression interface. Each of these areas will be discussed in the following sections. =head2 Notes for users of Compress::Zlib version 1 The main change in C version 2.x is that it does not now interface directly to the zlib library. Instead it uses the C and C modules for reading/writing gzip files, and the C module for some low-level zlib access. The interface provided by version 2 of this module should be 100% backward compatible with version 1. If you find a difference in the expected behaviour please contact the author (See L). See L With the creation of the C and C modules no new features are planned for C - the new modules do everything that C does and then some. Development on C will be limited to bug fixes only. If you are writing new code, your first port of call should be one of the new C or C modules. =head1 GZIP INTERFACE A number of functions are supplied in I for reading and writing I files that conform to RFC 1952. This module provides an interface to most of them. If you have previously used C 1.x, the following enhancements/changes have been made to the C interface: =over 5 =item 1 If you want to open either STDIN or STDOUT with C, you can now optionally use the special filename "C<->" as a synonym for C<\*STDIN> and C<\*STDOUT>. =item 2 In C version 1.x, C used the zlib library to open the underlying file. This made things especially tricky when a Perl filehandle was passed to C. Behind the scenes the numeric C file descriptor had to be extracted from the Perl filehandle and this passed to the zlib library. Apart from being non-portable to some operating systems, this made it difficult to use C in situations where you wanted to extract/create a gzip data stream that is embedded in a larger file, without having to resort to opening and closing the file multiple times. It also made it impossible to pass a perl filehandle that wasn't associated with a real filesystem file, like, say, an C. In C version 2.x, the C interface has been completely rewritten to use the L for writing gzip files and L for reading gzip files. None of the limitations mentioned above apply. =item 3 Addition of C to provide a restricted C interface. =item 4. Added C. =back A more complete and flexible interface for reading/writing gzip files/buffers is included with the module C. See L and L for more details. =over 5 =item B<$gz = gzopen($filename, $mode)> =item B<$gz = gzopen($filehandle, $mode)> This function opens either the I file C<$filename> for reading or writing or attaches to the opened filehandle, C<$filehandle>. It returns an object on success and C on failure. When writing a gzip file this interface will I create the smallest possible gzip header (exactly 10 bytes). If you want greater control over what gets stored in the gzip header (like the original filename or a comment) use L instead. Similarly if you want to read the contents of the gzip header use L. The second parameter, C<$mode>, is used to specify whether the file is opened for reading or writing and to optionally specify a compression level and compression strategy when writing. The format of the C<$mode> parameter is similar to the mode parameter to the 'C' function C, so "rb" is used to open for reading, "wb" for writing and "ab" for appending (writing at the end of the file). To specify a compression level when writing, append a digit between 0 and 9 to the mode string -- 0 means no compression and 9 means maximum compression. If no compression level is specified Z_DEFAULT_COMPRESSION is used. To specify the compression strategy when writing, append 'f' for filtered data, 'h' for Huffman only compression, or 'R' for run-length encoding. If no strategy is specified Z_DEFAULT_STRATEGY is used. So, for example, "wb9" means open for writing with the maximum compression using the default strategy and "wb4R" means open for writing with compression level 4 and run-length encoding. Refer to the I documentation for the exact format of the C<$mode> parameter. =item B<$bytesread = $gz-Egzread($buffer [, $size]) ;> Reads C<$size> bytes from the compressed file into C<$buffer>. If C<$size> is not specified, it will default to 4096. If the scalar C<$buffer> is not large enough, it will be extended automatically. Returns the number of bytes actually read. On EOF it returns 0 and in the case of an error, -1. =item B<$bytesread = $gz-Egzreadline($line) ;> Reads the next line from the compressed file into C<$line>. Returns the number of bytes actually read. On EOF it returns 0 and in the case of an error, -1. It is legal to intermix calls to C and C. To maintain backward compatibility with version 1.x of this module C ignores the C<$/> variable - it I uses the string C<"\n"> as the line delimiter. If you want to read a gzip file a line at a time and have it respect the C<$/> variable (or C<$INPUT_RECORD_SEPARATOR>, or C<$RS> when C is in use) see L. =item B<$byteswritten = $gz-Egzwrite($buffer) ;> Writes the contents of C<$buffer> to the compressed file. Returns the number of bytes actually written, or 0 on error. =item B<$status = $gz-Egzflush($flush_type) ;> Flushes all pending output into the compressed file. This method takes an optional parameter, C<$flush_type>, that controls how the flushing will be carried out. By default the C<$flush_type> used is C. Other valid values for C<$flush_type> are C, C, C and C. It is strongly recommended that you only set the C parameter if you fully understand the implications of what it does - overuse of C can seriously degrade the level of compression achieved. See the C documentation for details. Returns 0 on success. =item B<$offset = $gz-Egztell() ;> Returns the uncompressed file offset. =item B<$status = $gz-Egzseek($offset, $whence) ;> Provides a sub-set of the C functionality, with the restriction that it is only legal to seek forward in the compressed file. It is a fatal error to attempt to seek backward. When opened for writing, empty parts of the file will have NULL (0x00) bytes written to them. The C<$whence> parameter should be one of SEEK_SET, SEEK_CUR or SEEK_END. Returns 1 on success, 0 on failure. =item B<$gz-Egzclose> Closes the compressed file. Any pending data is flushed to the file before it is closed. Returns 0 on success. =item B<$gz-Egzsetparams($level, $strategy> Change settings for the deflate stream C<$gz>. The list of the valid options is shown below. Options not specified will remain unchanged. Note: This method is only available if you are running zlib 1.0.6 or better. =over 5 =item B<$level> Defines the compression level. Valid values are 0 through 9, C, C, C, and C. =item B<$strategy> Defines the strategy used to tune the compression. The valid values are C, C and C. =back =item B<$gz-Egzerror> Returns the I error message or number for the last operation associated with C<$gz>. The return value will be the I error number when used in a numeric context and the I error message when used in a string context. The I error number constants, shown below, are available for use. Z_OK Z_STREAM_END Z_ERRNO Z_STREAM_ERROR Z_DATA_ERROR Z_MEM_ERROR Z_BUF_ERROR =item B<$gzerrno> The C<$gzerrno> scalar holds the error code associated with the most recent I routine. Note that unlike C, the error is I associated with a particular file. As with C it returns an error number in numeric context and an error message in string context. Unlike C though, the error message will correspond to the I message when the error is associated with I itself, or the UNIX error message when it is not (i.e. I returned C). As there is an overlap between the error numbers used by I and UNIX, C<$gzerrno> should only be used to check for the presence of I error in numeric context. Use C to check for specific I errors. The I example below shows how the variable can be used safely. =back =head2 Examples Here is an example script which uses the interface. It implements a I function. use strict ; use warnings ; use Compress::Zlib ; # use stdin if no files supplied @ARGV = '-' unless @ARGV ; foreach my $file (@ARGV) { my $buffer ; my $gz = gzopen($file, "rb") or die "Cannot open $file: $gzerrno\n" ; print $buffer while $gz->gzread($buffer) > 0 ; die "Error reading from $file: $gzerrno" . ($gzerrno+0) . "\n" if $gzerrno != Z_STREAM_END ; $gz->gzclose() ; } Below is a script which makes use of C. It implements a very simple I like script. use strict ; use warnings ; use Compress::Zlib ; die "Usage: gzgrep pattern [file...]\n" unless @ARGV >= 1; my $pattern = shift ; # use stdin if no files supplied @ARGV = '-' unless @ARGV ; foreach my $file (@ARGV) { my $gz = gzopen($file, "rb") or die "Cannot open $file: $gzerrno\n" ; while ($gz->gzreadline($_) > 0) { print if /$pattern/ ; } die "Error reading from $file: $gzerrno\n" if $gzerrno != Z_STREAM_END ; $gz->gzclose() ; } This script, I, does the opposite of the I script above. It reads from standard input and writes a gzip data stream to standard output. use strict ; use warnings ; use Compress::Zlib ; binmode STDOUT; # gzopen only sets it on the fd my $gz = gzopen(\*STDOUT, "wb") or die "Cannot open stdout: $gzerrno\n" ; while (<>) { $gz->gzwrite($_) or die "error writing: $gzerrno\n" ; } $gz->gzclose ; =head2 Compress::Zlib::memGzip This function is used to create an in-memory gzip file with the minimum possible gzip header (exactly 10 bytes). $dest = Compress::Zlib::memGzip($buffer) or die "Cannot compress: $gzerrno\n"; If successful, it returns the in-memory gzip file. Otherwise it returns C and the C<$gzerrno> variable will store the zlib error code. The C<$buffer> parameter can either be a scalar or a scalar reference. See L for an alternative way to carry out in-memory gzip compression. =head2 Compress::Zlib::memGunzip This function is used to uncompress an in-memory gzip file. $dest = Compress::Zlib::memGunzip($buffer) or die "Cannot uncompress: $gzerrno\n"; If successful, it returns the uncompressed gzip file. Otherwise it returns C and the C<$gzerrno> variable will store the zlib error code. The C<$buffer> parameter can either be a scalar or a scalar reference. The contents of the C<$buffer> parameter are destroyed after calling this function. If C<$buffer> consists of multiple concatenated gzip data streams only the first will be uncompressed. Use C with the C option in the C module if you need to deal with concatenated data streams. See L for an alternative way to carry out in-memory gzip uncompression. =head1 COMPRESS/UNCOMPRESS Two functions are provided to perform in-memory compression/uncompression of RFC 1950 data streams. They are called C and C. =over 5 =item B<$dest = compress($source [, $level] ) ;> Compresses C<$source>. If successful it returns the compressed data. Otherwise it returns I. The source buffer, C<$source>, can either be a scalar or a scalar reference. The C<$level> parameter defines the compression level. Valid values are 0 through 9, C, C, C, and C. If C<$level> is not specified C will be used. =item B<$dest = uncompress($source) ;> Uncompresses C<$source>. If successful it returns the uncompressed data. Otherwise it returns I. The source buffer can either be a scalar or a scalar reference. =back Please note: the two functions defined above are I compatible with the Unix commands of the same name. See L and L included with this distribution for an alternative interface for reading/writing RFC 1950 files/buffers. =head1 Deflate Interface This section defines an interface that allows in-memory compression using the I interface provided by zlib. Here is a definition of the interface available: =head2 B<($d, $status) = deflateInit( [OPT] )> Initialises a deflation stream. It combines the features of the I functions C, C and C. If successful, it will return the initialised deflation stream, C<$d> and C<$status> of C in a list context. In scalar context it returns the deflation stream, C<$d>, only. If not successful, the returned deflation stream (C<$d>) will be I and C<$status> will hold the exact I error code. The function optionally takes a number of named options specified as C<< -Name=>value >> pairs. This allows individual options to be tailored without having to specify them all in the parameter list. For backward compatibility, it is also possible to pass the parameters as a reference to a hash containing the name=>value pairs. The function takes one optional parameter, a reference to a hash. The contents of the hash allow the deflation interface to be tailored. Here is a list of the valid options: =over 5 =item B<-Level> Defines the compression level. Valid values are 0 through 9, C, C, C, and C. The default is Z_DEFAULT_COMPRESSION. =item B<-Method> Defines the compression method. The only valid value at present (and the default) is Z_DEFLATED. =item B<-WindowBits> To create an RFC 1950 data stream, set C to a positive number. To create an RFC 1951 data stream, set C to C<-MAX_WBITS>. For a full definition of the meaning and valid values for C refer to the I documentation for I. Defaults to MAX_WBITS. =item B<-MemLevel> For a definition of the meaning and valid values for C refer to the I documentation for I. Defaults to MAX_MEM_LEVEL. =item B<-Strategy> Defines the strategy used to tune the compression. The valid values are C, C and C. The default is Z_DEFAULT_STRATEGY. =item B<-Dictionary> When a dictionary is specified I will automatically call C directly after calling C. The Adler32 value for the dictionary can be obtained by calling the method C<< $d->dict_adler() >>. The default is no dictionary. =item B<-Bufsize> Sets the initial size for the deflation buffer. If the buffer has to be reallocated to increase the size, it will grow in increments of C. The default is 4096. =back Here is an example of using the C optional parameter list to override the default buffer size and compression level. All other options will take their default values. deflateInit( -Bufsize => 300, -Level => Z_BEST_SPEED ) ; =head2 B<($out, $status) = $d-Edeflate($buffer)> Deflates the contents of C<$buffer>. The buffer can either be a scalar or a scalar reference. When finished, C<$buffer> will be completely processed (assuming there were no errors). If the deflation was successful it returns the deflated output, C<$out>, and a status value, C<$status>, of C. On error, C<$out> will be I and C<$status> will contain the I error code. In a scalar context C will return C<$out> only. As with the I function in I, it is not necessarily the case that any output will be produced by this method. So don't rely on the fact that C<$out> is empty for an error test. =head2 B<($out, $status) = $d-Eflush()> =head2 B<($out, $status) = $d-Eflush($flush_type)> Typically used to finish the deflation. Any pending output will be returned via C<$out>. C<$status> will have a value C if successful. In a scalar context C will return C<$out> only. Note that flushing can seriously degrade the compression ratio, so it should only be used to terminate a decompression (using C) or when you want to create a I (using C). By default the C used is C. Other valid values for C are C, C, C and C. It is strongly recommended that you only set the C parameter if you fully understand the implications of what it does. See the C documentation for details. =head2 B<$status = $d-EdeflateParams([OPT])> Change settings for the deflate stream C<$d>. The list of the valid options is shown below. Options not specified will remain unchanged. =over 5 =item B<-Level> Defines the compression level. Valid values are 0 through 9, C, C, C, and C. =item B<-Strategy> Defines the strategy used to tune the compression. The valid values are C, C and C. =back =head2 B<$d-Edict_adler()> Returns the adler32 value for the dictionary. =head2 B<$d-Emsg()> Returns the last error message generated by zlib. =head2 B<$d-Etotal_in()> Returns the total number of bytes uncompressed bytes input to deflate. =head2 B<$d-Etotal_out()> Returns the total number of compressed bytes output from deflate. =head2 Example Here is a trivial example of using C. It simply reads standard input, deflates it and writes it to standard output. use strict ; use warnings ; use Compress::Zlib ; binmode STDIN; binmode STDOUT; my $x = deflateInit() or die "Cannot create a deflation stream\n" ; my ($output, $status) ; while (<>) { ($output, $status) = $x->deflate($_) ; $status == Z_OK or die "deflation failed\n" ; print $output ; } ($output, $status) = $x->flush() ; $status == Z_OK or die "deflation failed\n" ; print $output ; =head1 Inflate Interface This section defines the interface available that allows in-memory uncompression using the I interface provided by zlib. Here is a definition of the interface: =head2 B<($i, $status) = inflateInit()> Initialises an inflation stream. In a list context it returns the inflation stream, C<$i>, and the I status code in C<$status>. In a scalar context it returns the inflation stream only. If successful, C<$i> will hold the inflation stream and C<$status> will be C. If not successful, C<$i> will be I and C<$status> will hold the I error code. The function optionally takes a number of named options specified as C<< -Name=>value >> pairs. This allows individual options to be tailored without having to specify them all in the parameter list. For backward compatibility, it is also possible to pass the parameters as a reference to a hash containing the name=>value pairs. The function takes one optional parameter, a reference to a hash. The contents of the hash allow the deflation interface to be tailored. Here is a list of the valid options: =over 5 =item B<-WindowBits> To uncompress an RFC 1950 data stream, set C to a positive number. To uncompress an RFC 1951 data stream, set C to C<-MAX_WBITS>. For a full definition of the meaning and valid values for C refer to the I documentation for I. Defaults to MAX_WBITS. =item B<-Bufsize> Sets the initial size for the inflation buffer. If the buffer has to be reallocated to increase the size, it will grow in increments of C. Default is 4096. =item B<-Dictionary> The default is no dictionary. =back Here is an example of using the C optional parameter to override the default buffer size. inflateInit( -Bufsize => 300 ) ; =head2 B<($out, $status) = $i-Einflate($buffer)> Inflates the complete contents of C<$buffer>. The buffer can either be a scalar or a scalar reference. Returns C if successful and C if the end of the compressed data has been successfully reached. If not successful, C<$out> will be I and C<$status> will hold the I error code. The C<$buffer> parameter is modified by C. On completion it will contain what remains of the input buffer after inflation. This means that C<$buffer> will be an empty string when the return status is C. When the return status is C the C<$buffer> parameter will contains what (if anything) was stored in the input buffer after the deflated data stream. This feature is useful when processing a file format that encapsulates a compressed data stream (e.g. gzip, zip). =head2 B<$status = $i-EinflateSync($buffer)> Scans C<$buffer> until it reaches either a I or the end of the buffer. If a I is found, C is returned and C<$buffer> will be have all data up to the flush point removed. This can then be passed to the C method. Any other return code means that a flush point was not found. If more data is available, C can be called repeatedly with more compressed data until the flush point is found. =head2 B<$i-Edict_adler()> Returns the adler32 value for the dictionary. =head2 B<$i-Emsg()> Returns the last error message generated by zlib. =head2 B<$i-Etotal_in()> Returns the total number of bytes compressed bytes input to inflate. =head2 B<$i-Etotal_out()> Returns the total number of uncompressed bytes output from inflate. =head2 Example Here is an example of using C. use strict ; use warnings ; use Compress::Zlib ; my $x = inflateInit() or die "Cannot create a inflation stream\n" ; my $input = '' ; binmode STDIN; binmode STDOUT; my ($output, $status) ; while (read(STDIN, $input, 4096)) { ($output, $status) = $x->inflate(\$input) ; print $output if $status == Z_OK or $status == Z_STREAM_END ; last if $status != Z_OK ; } die "inflation failed\n" unless $status == Z_STREAM_END ; =head1 CHECKSUM FUNCTIONS Two functions are provided by I to calculate checksums. For the Perl interface, the order of the two parameters in both functions has been reversed. This allows both running checksums and one off calculations to be done. $crc = adler32($buffer [,$crc]) ; $crc = crc32($buffer [,$crc]) ; The buffer parameters can either be a scalar or a scalar reference. If the $crc parameters is C, the crc value will be reset. If you have built this module with zlib 1.2.3 or better, two more CRC-related functions are available. $crc = crc32_combine($crc1, $crc2, $len2); $adler = adler32_combine($adler1, $adler2, $len2); These functions allow checksums to be merged. Refer to the I documentation for more details. =head1 Misc =head2 my $version = Compress::Zlib::zlib_version(); Returns the version of the zlib library. =head1 CONSTANTS All the I constants are automatically imported when you make use of I. =head1 SUPPORT General feedback/questions/bug reports should be sent to L (preferred) or L. =head1 SEE ALSO L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L L L, L, L, L For RFC 1950, 1951 and 1952 see L, L and L The I compression library was written by Jean-loup Gailly C and Mark Adler C. The primary site for the I compression library is L. The primary site for the I compression library is L. The primary site for gzip is L. =head1 AUTHOR This module was written by Paul Marquess, C. =head1 MODIFICATION HISTORY See the Changes file. =head1 COPYRIGHT AND LICENSE Copyright (c) 1995-2024 Paul Marquess. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. IO-Compress-2.212/lib/IO/0000755000175000017500000000000014613172626013371 5ustar paulpaulIO-Compress-2.212/lib/IO/Compress/0000755000175000017500000000000014613172626015164 5ustar paulpaulIO-Compress-2.212/lib/IO/Compress/Zip.pm0000644000175000017500000020625614613134205016266 0ustar paulpaulpackage IO::Compress::Zip ; use strict ; use warnings; use bytes; use IO::Compress::Base::Common 2.212 qw(:Status ); use IO::Compress::RawDeflate 2.212 (); use IO::Compress::Adapter::Deflate 2.212 ; use IO::Compress::Adapter::Identity 2.212 ; use IO::Compress::Zlib::Extra 2.212 ; use IO::Compress::Zip::Constants 2.212 ; use File::Spec(); use Config; use Compress::Raw::Zlib 2.212 (); BEGIN { eval { require IO::Compress::Adapter::Bzip2 ; IO::Compress::Adapter::Bzip2->VERSION( 2.212 ); require IO::Compress::Bzip2 ; IO::Compress::Bzip2->VERSION( 2.212 ); } ; eval { require IO::Compress::Adapter::Lzma ; IO::Compress::Adapter::Lzma->VERSION( 2.212 ); require IO::Compress::Lzma ; IO::Compress::Lzma->VERSION( 2.212 ); } ; eval { require IO::Compress::Adapter::Xz ; IO::Compress::Adapter::Xz->VERSION( 2.212 ); require IO::Compress::Xz ; IO::Compress::Xz->VERSION( 2.212 ); } ; eval { require IO::Compress::Adapter::Zstd ; IO::Compress::Adapter::Zstd->VERSION( 2.212 ); require IO::Compress::Zstd ; IO::Compress::Zstd->VERSION( 2.212 ); } ; } require Exporter ; our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, %DEFLATE_CONSTANTS, $ZipError); $VERSION = '2.212'; $ZipError = ''; @ISA = qw(IO::Compress::RawDeflate Exporter); @EXPORT_OK = qw( $ZipError zip ) ; %EXPORT_TAGS = %IO::Compress::RawDeflate::DEFLATE_CONSTANTS ; push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ; $EXPORT_TAGS{zip_method} = [qw( ZIP_CM_STORE ZIP_CM_DEFLATE ZIP_CM_BZIP2 ZIP_CM_LZMA ZIP_CM_XZ ZIP_CM_ZSTD)]; push @{ $EXPORT_TAGS{all} }, @{ $EXPORT_TAGS{zip_method} }; Exporter::export_ok_tags('all'); sub new { my $class = shift ; my $obj = IO::Compress::Base::Common::createSelfTiedObject($class, \$ZipError); $obj->_create(undef, @_); } sub zip { my $obj = IO::Compress::Base::Common::createSelfTiedObject(undef, \$ZipError); return $obj->_def(@_); } sub isMethodAvailable { my $method = shift; # Store & Deflate are always available return 1 if $method == ZIP_CM_STORE || $method == ZIP_CM_DEFLATE ; return 1 if $method == ZIP_CM_BZIP2 && defined $IO::Compress::Adapter::Bzip2::VERSION && defined &{ "IO::Compress::Adapter::Bzip2::mkRawZipCompObject" }; return 1 if $method == ZIP_CM_LZMA && defined $IO::Compress::Adapter::Lzma::VERSION && defined &{ "IO::Compress::Adapter::Lzma::mkRawZipCompObject" }; return 1 if $method == ZIP_CM_XZ && defined $IO::Compress::Adapter::Xz::VERSION && defined &{ "IO::Compress::Adapter::Xz::mkRawZipCompObject" }; return 1 if $method == ZIP_CM_ZSTD && defined $IO::Compress::Adapter::ZSTD::VERSION && defined &{ "IO::Compress::Adapter::ZSTD::mkRawZipCompObject" }; return 0; } sub beforePayload { my $self = shift ; if (*$self->{ZipData}{Sparse} ) { my $inc = 1024 * 100 ; my $NULLS = ("\x00" x $inc) ; my $sparse = *$self->{ZipData}{Sparse} ; *$self->{CompSize}->add( $sparse ); *$self->{UnCompSize}->add( $sparse ); *$self->{FH}->seek($sparse, IO::Handle::SEEK_CUR); *$self->{ZipData}{CRC32} = Compress::Raw::Zlib::crc32($NULLS, *$self->{ZipData}{CRC32}) for 1 .. int $sparse / $inc; *$self->{ZipData}{CRC32} = Compress::Raw::Zlib::crc32(substr($NULLS, 0, $sparse % $inc), *$self->{ZipData}{CRC32}) if $sparse % $inc; } } sub mkComp { my $self = shift ; my $got = shift ; my ($obj, $errstr, $errno) ; if (*$self->{ZipData}{Method} == ZIP_CM_STORE) { ($obj, $errstr, $errno) = IO::Compress::Adapter::Identity::mkCompObject( $got->getValue('level'), $got->getValue('strategy') ); *$self->{ZipData}{CRC32} = Compress::Raw::Zlib::crc32(undef); } elsif (*$self->{ZipData}{Method} == ZIP_CM_DEFLATE) { ($obj, $errstr, $errno) = IO::Compress::Adapter::Deflate::mkCompObject( $got->getValue('crc32'), $got->getValue('adler32'), $got->getValue('level'), $got->getValue('strategy') ); } elsif (*$self->{ZipData}{Method} == ZIP_CM_BZIP2) { ($obj, $errstr, $errno) = IO::Compress::Adapter::Bzip2::mkCompObject( $got->getValue('blocksize100k'), $got->getValue('workfactor'), $got->getValue('verbosity') ); *$self->{ZipData}{CRC32} = Compress::Raw::Zlib::crc32(undef); } elsif (*$self->{ZipData}{Method} == ZIP_CM_LZMA) { ($obj, $errstr, $errno) = IO::Compress::Adapter::Lzma::mkRawZipCompObject($got->getValue('preset'), $got->getValue('extreme'), ); *$self->{ZipData}{CRC32} = Compress::Raw::Zlib::crc32(undef); } elsif (*$self->{ZipData}{Method} == ZIP_CM_XZ) { ($obj, $errstr, $errno) = IO::Compress::Adapter::Xz::mkCompObject($got->getValue('preset'), $got->getValue('extreme'), 0 ); *$self->{ZipData}{CRC32} = Compress::Raw::Zlib::crc32(undef); } elsif (*$self->{ZipData}{Method} == ZIP_CM_ZSTD) { ($obj, $errstr, $errno) = IO::Compress::Adapter::Zstd::mkCompObject(defined $got->getValue('level') ? $got->getValue('level') : 3, ); *$self->{ZipData}{CRC32} = Compress::Raw::Zlib::crc32(undef); } return $self->saveErrorString(undef, $errstr, $errno) if ! defined $obj; if (! defined *$self->{ZipData}{SizesOffset}) { *$self->{ZipData}{SizesOffset} = 0; *$self->{ZipData}{Offset} = U64->new(); } *$self->{ZipData}{AnyZip64} = 0 if ! defined *$self->{ZipData}{AnyZip64} ; return $obj; } sub reset { my $self = shift ; *$self->{Compress}->reset(); *$self->{ZipData}{CRC32} = Compress::Raw::Zlib::crc32(''); return STATUS_OK; } sub filterUncompressed { my $self = shift ; if (*$self->{ZipData}{Method} == ZIP_CM_DEFLATE) { *$self->{ZipData}{CRC32} = *$self->{Compress}->crc32(); } else { *$self->{ZipData}{CRC32} = Compress::Raw::Zlib::crc32(${$_[0]}, *$self->{ZipData}{CRC32}); } } sub canonicalName { # This sub is derived from Archive::Zip::_asZipDirName # Return the normalized name as used in a zip file (path # separators become slashes, etc.). # Will translate internal slashes in path components (i.e. on Macs) to # underscores. Discards volume names. # When $forceDir is set, returns paths with trailing slashes # # input output # . '.' # ./a a # ./a/b a/b # ./a/b/ a/b # a/b/ a/b # /a/b/ a/b # c:\a\b\c.doc a/b/c.doc # on Windows # "i/o maps:whatever" i_o maps/whatever # on Macs my $name = shift; my $forceDir = shift ; my ( $volume, $directories, $file ) = File::Spec->splitpath( File::Spec->canonpath($name), $forceDir ); my @dirs = map { $_ =~ s{/}{_}g; $_ } File::Spec->splitdir($directories); if ( @dirs > 0 ) { pop (@dirs) if $dirs[-1] eq '' } # remove empty component push @dirs, defined($file) ? $file : '' ; my $normalised_path = join '/', @dirs; # Leading directory separators should not be stored in zip archives. # Example: # C:\a\b\c\ a/b/c # C:\a\b\c.txt a/b/c.txt # /a/b/c/ a/b/c # /a/b/c.txt a/b/c.txt $normalised_path =~ s{^/}{}; # remove leading separator return $normalised_path; } sub mkHeader { my $self = shift; my $param = shift ; *$self->{ZipData}{LocalHdrOffset} = U64::clone(*$self->{ZipData}{Offset}); my $comment = ''; $comment = $param->valueOrDefault('comment') ; my $filename = ''; $filename = $param->valueOrDefault('name') ; $filename = canonicalName($filename) if length $filename && $param->getValue('canonicalname') ; if (defined *$self->{ZipData}{FilterName} ) { local *_ = \$filename ; &{ *$self->{ZipData}{FilterName} }() ; } if ( $param->getValue('efs') && $] >= 5.008004) { if (length $filename) { utf8::downgrade($filename, 1) or Carp::croak "Wide character in zip filename"; } if (length $comment) { utf8::downgrade($comment, 1) or Carp::croak "Wide character in zip comment"; } } my $hdr = ''; my $time = _unixToDosTime($param->getValue('time')); my $extra = ''; my $ctlExtra = ''; my $empty = 0; my $osCode = $param->getValue('os_code') ; my $extFileAttr = 0 ; # This code assumes Unix. # TODO - revisit this $extFileAttr = 0100644 << 16 if $osCode == ZIP_OS_CODE_UNIX ; if (*$self->{ZipData}{Zip64}) { $empty = IO::Compress::Base::Common::MAX32; my $x = ''; $x .= pack "V V", 0, 0 ; # uncompressedLength $x .= pack "V V", 0, 0 ; # compressedLength # Zip64 needs to be first in extra field to workaround a Windows Explorer Bug # See http://www.info-zip.org/phpBB3/viewtopic.php?f=3&t=440 for details $extra .= IO::Compress::Zlib::Extra::mkSubField(ZIP_EXTRA_ID_ZIP64, $x); } if (! $param->getValue('minimal')) { if ($param->parsed('mtime')) { $extra .= mkExtendedTime($param->getValue('mtime'), $param->getValue('atime'), $param->getValue('ctime')); $ctlExtra .= mkExtendedTime($param->getValue('mtime')); } if ( $osCode == ZIP_OS_CODE_UNIX ) { if ( $param->getValue('want_exunixn') ) { my $ux3 = mkUnixNExtra( @{ $param->getValue('want_exunixn') }); $extra .= $ux3; $ctlExtra .= $ux3; } if ( $param->getValue('exunix2') ) { $extra .= mkUnix2Extra( @{ $param->getValue('exunix2') }); $ctlExtra .= mkUnix2Extra(); } } $extFileAttr = $param->getValue('extattr') if defined $param->getValue('extattr') ; $extra .= $param->getValue('extrafieldlocal') if defined $param->getValue('extrafieldlocal'); $ctlExtra .= $param->getValue('extrafieldcentral') if defined $param->getValue('extrafieldcentral'); } my $method = *$self->{ZipData}{Method} ; my $gpFlag = 0 ; $gpFlag |= ZIP_GP_FLAG_STREAMING_MASK if *$self->{ZipData}{Stream} ; $gpFlag |= ZIP_GP_FLAG_LZMA_EOS_PRESENT if $method == ZIP_CM_LZMA ; $gpFlag |= ZIP_GP_FLAG_LANGUAGE_ENCODING if $param->getValue('efs') && (length($filename) || length($comment)); my $version = $ZIP_CM_MIN_VERSIONS{$method}; $version = ZIP64_MIN_VERSION if ZIP64_MIN_VERSION > $version && *$self->{ZipData}{Zip64}; my $madeBy = ($param->getValue('os_code') << 8) + $version; my $extract = $version; *$self->{ZipData}{Version} = $version; *$self->{ZipData}{MadeBy} = $madeBy; my $ifa = 0; $ifa |= ZIP_IFA_TEXT_MASK if $param->getValue('textflag'); $hdr .= pack "V", ZIP_LOCAL_HDR_SIG ; # signature $hdr .= pack 'v', $extract ; # extract Version & OS $hdr .= pack 'v', $gpFlag ; # general purpose flag (set streaming mode) $hdr .= pack 'v', $method ; # compression method (deflate) $hdr .= pack 'V', $time ; # last mod date/time $hdr .= pack 'V', 0 ; # crc32 - 0 when streaming $hdr .= pack 'V', $empty ; # compressed length - 0 when streaming $hdr .= pack 'V', $empty ; # uncompressed length - 0 when streaming $hdr .= pack 'v', length $filename ; # filename length $hdr .= pack 'v', length $extra ; # extra length $hdr .= $filename ; # Remember the offset for the compressed & uncompressed lengths in the # local header. if (*$self->{ZipData}{Zip64}) { *$self->{ZipData}{SizesOffset} = *$self->{ZipData}{Offset}->get64bit() + length($hdr) + 4 ; } else { *$self->{ZipData}{SizesOffset} = *$self->{ZipData}{Offset}->get64bit() + 18; } $hdr .= $extra ; my $ctl = ''; $ctl .= pack "V", ZIP_CENTRAL_HDR_SIG ; # signature $ctl .= pack 'v', $madeBy ; # version made by $ctl .= pack 'v', $extract ; # extract Version $ctl .= pack 'v', $gpFlag ; # general purpose flag (streaming mode) $ctl .= pack 'v', $method ; # compression method (deflate) $ctl .= pack 'V', $time ; # last mod date/time $ctl .= pack 'V', 0 ; # crc32 $ctl .= pack 'V', $empty ; # compressed length $ctl .= pack 'V', $empty ; # uncompressed length $ctl .= pack 'v', length $filename ; # filename length *$self->{ZipData}{ExtraOffset} = length $ctl; *$self->{ZipData}{ExtraSize} = length $ctlExtra ; $ctl .= pack 'v', length $ctlExtra ; # extra length $ctl .= pack 'v', length $comment ; # file comment length $ctl .= pack 'v', 0 ; # disk number start $ctl .= pack 'v', $ifa ; # internal file attributes $ctl .= pack 'V', $extFileAttr ; # external file attributes # offset to local hdr if (*$self->{ZipData}{LocalHdrOffset}->is64bit() ) { $ctl .= pack 'V', IO::Compress::Base::Common::MAX32 ; } else { $ctl .= *$self->{ZipData}{LocalHdrOffset}->getPacked_V32() ; } $ctl .= $filename ; *$self->{ZipData}{Offset}->add32(length $hdr) ; *$self->{ZipData}{CentralHeader} = [ $ctl, $ctlExtra, $comment]; return $hdr; } sub mkTrailer { my $self = shift ; my $crc32 ; if (*$self->{ZipData}{Method} == ZIP_CM_DEFLATE) { $crc32 = pack "V", *$self->{Compress}->crc32(); } else { $crc32 = pack "V", *$self->{ZipData}{CRC32}; } my ($ctl, $ctlExtra, $comment) = @{ *$self->{ZipData}{CentralHeader} }; my $sizes ; if (! *$self->{ZipData}{Zip64}) { $sizes .= *$self->{CompSize}->getPacked_V32() ; # Compressed size $sizes .= *$self->{UnCompSize}->getPacked_V32() ; # Uncompressed size } else { $sizes .= *$self->{CompSize}->getPacked_V64() ; # Compressed size $sizes .= *$self->{UnCompSize}->getPacked_V64() ; # Uncompressed size } my $data = $crc32 . $sizes ; my $xtrasize = *$self->{UnCompSize}->getPacked_V64() ; # Uncompressed size $xtrasize .= *$self->{CompSize}->getPacked_V64() ; # Compressed size my $hdr = ''; if (*$self->{ZipData}{Stream}) { $hdr = pack "V", ZIP_DATA_HDR_SIG ; # signature $hdr .= $data ; } else { $self->writeAt(*$self->{ZipData}{LocalHdrOffset}->get64bit() + 14, $crc32) or return undef; $self->writeAt(*$self->{ZipData}{SizesOffset}, *$self->{ZipData}{Zip64} ? $xtrasize : $sizes) or return undef; } # Central Header Record/Zip64 extended field substr($ctl, 16, length $crc32) = $crc32 ; my $zip64Payload = ''; # uncompressed length - only set zip64 if needed if (*$self->{UnCompSize}->isAlmost64bit()) { # || *$self->{ZipData}{Zip64}) { $zip64Payload .= *$self->{UnCompSize}->getPacked_V64() ; } else { substr($ctl, 24, 4) = *$self->{UnCompSize}->getPacked_V32() ; } # compressed length - only set zip64 if needed if (*$self->{CompSize}->isAlmost64bit()) { # || *$self->{ZipData}{Zip64}) { $zip64Payload .= *$self->{CompSize}->getPacked_V64() ; } else { substr($ctl, 20, 4) = *$self->{CompSize}->getPacked_V32() ; } # Local Header offset $zip64Payload .= *$self->{ZipData}{LocalHdrOffset}->getPacked_V64() if *$self->{ZipData}{LocalHdrOffset}->is64bit() ; # disk no - always zero, so don't need to include it. #$zip64Payload .= pack "V", 0 ; my $zip64Xtra = ''; if (length $zip64Payload) { $zip64Xtra = IO::Compress::Zlib::Extra::mkSubField(ZIP_EXTRA_ID_ZIP64, $zip64Payload); substr($ctl, *$self->{ZipData}{ExtraOffset}, 2) = pack 'v', *$self->{ZipData}{ExtraSize} + length $zip64Xtra; *$self->{ZipData}{AnyZip64} = 1; } # Zip64 needs to be first in extra field to workaround a Windows Explorer Bug # See http://www.info-zip.org/phpBB3/viewtopic.php?f=3&t=440 for details $ctl .= $zip64Xtra . $ctlExtra . $comment; *$self->{ZipData}{Offset}->add32(length($hdr)); *$self->{ZipData}{Offset}->add( *$self->{CompSize} ); push @{ *$self->{ZipData}{CentralDir} }, $ctl ; return $hdr; } sub mkFinalTrailer { my $self = shift ; my $comment = ''; $comment = *$self->{ZipData}{ZipComment} ; my $cd_offset = *$self->{ZipData}{Offset}->get32bit() ; # offset to start central dir my $entries = @{ *$self->{ZipData}{CentralDir} }; *$self->{ZipData}{AnyZip64} = 1 if *$self->{ZipData}{Offset}->is64bit || $entries >= 0xFFFF ; my $cd = join '', @{ *$self->{ZipData}{CentralDir} }; my $cd_len = length $cd ; my $z64e = ''; if ( *$self->{ZipData}{AnyZip64} ) { my $v = *$self->{ZipData}{Version} ; my $mb = *$self->{ZipData}{MadeBy} ; $z64e .= pack 'v', $mb ; # Version made by $z64e .= pack 'v', $v ; # Version to extract $z64e .= pack 'V', 0 ; # number of disk $z64e .= pack 'V', 0 ; # number of disk with central dir $z64e .= U64::pack_V64 $entries ; # entries in central dir on this disk $z64e .= U64::pack_V64 $entries ; # entries in central dir $z64e .= U64::pack_V64 $cd_len ; # size of central dir $z64e .= *$self->{ZipData}{Offset}->getPacked_V64() ; # offset to start central dir $z64e .= *$self->{ZipData}{extrafieldzip64} # otional extra field if defined *$self->{ZipData}{extrafieldzip64} ; $z64e = pack("V", ZIP64_END_CENTRAL_REC_HDR_SIG) # signature . U64::pack_V64(length $z64e) . $z64e ; *$self->{ZipData}{Offset}->add32(length $cd) ; $z64e .= pack "V", ZIP64_END_CENTRAL_LOC_HDR_SIG; # signature $z64e .= pack 'V', 0 ; # number of disk with central dir $z64e .= *$self->{ZipData}{Offset}->getPacked_V64() ; # offset to end zip64 central dir $z64e .= pack 'V', 1 ; # Total number of disks $cd_offset = IO::Compress::Base::Common::MAX32 ; $cd_len = IO::Compress::Base::Common::MAX32 if IO::Compress::Base::Common::isGeMax32 $cd_len ; $entries = 0xFFFF if $entries >= 0xFFFF ; } my $ecd = ''; $ecd .= pack "V", ZIP_END_CENTRAL_HDR_SIG ; # signature $ecd .= pack 'v', 0 ; # number of disk $ecd .= pack 'v', 0 ; # number of disk with central dir $ecd .= pack 'v', $entries ; # entries in central dir on this disk $ecd .= pack 'v', $entries ; # entries in central dir $ecd .= pack 'V', $cd_len ; # size of central dir $ecd .= pack 'V', $cd_offset ; # offset to start central dir $ecd .= pack 'v', length $comment ; # zipfile comment length $ecd .= $comment; return $cd . $z64e . $ecd ; } sub ckParams { my $self = shift ; my $got = shift; $got->setValue('crc32' => 1); if (! $got->parsed('time') ) { # Modification time defaults to now. $got->setValue('time' => time) ; } if ($got->parsed('extime') ) { my $timeRef = $got->getValue('extime'); if ( defined $timeRef) { return $self->saveErrorString(undef, "exTime not a 3-element array ref") if ref $timeRef ne 'ARRAY' || @$timeRef != 3; } $got->setValue("mtime", $timeRef->[1]); $got->setValue("atime", $timeRef->[0]); $got->setValue("ctime", $timeRef->[2]); } # Unix2/3 Extended Attribute for my $name (qw(exunix2 exunixn)) { if ($got->parsed($name) ) { my $idRef = $got->getValue($name); if ( defined $idRef) { return $self->saveErrorString(undef, "$name not a 2-element array ref") if ref $idRef ne 'ARRAY' || @$idRef != 2; } $got->setValue("uid", $idRef->[0]); $got->setValue("gid", $idRef->[1]); $got->setValue("want_$name", $idRef); } } *$self->{ZipData}{AnyZip64} = 1 if $got->getValue('zip64') || $got->getValue('extrafieldzip64') ; *$self->{ZipData}{Zip64} = $got->getValue('zip64'); *$self->{ZipData}{Stream} = $got->getValue('stream'); my $method = $got->getValue('method'); return $self->saveErrorString(undef, "Unknown Method '$method'") if ! defined $ZIP_CM_MIN_VERSIONS{$method}; return $self->saveErrorString(undef, "Bzip2 not available") if $method == ZIP_CM_BZIP2 and ! defined $IO::Compress::Adapter::Bzip2::VERSION; return $self->saveErrorString(undef, "Lzma not available") if $method == ZIP_CM_LZMA and ! defined $IO::Compress::Adapter::Lzma::VERSION; *$self->{ZipData}{Method} = $method; *$self->{ZipData}{ZipComment} = $got->getValue('zipcomment') ; for my $name (qw( extrafieldlocal extrafieldcentral extrafieldzip64)) { my $data = $got->getValue($name) ; if (defined $data) { my $bad = IO::Compress::Zlib::Extra::parseExtraField($data, 1, 0) ; return $self->saveErrorString(undef, "Error with $name Parameter: $bad") if $bad ; $got->setValue($name, $data) ; *$self->{ZipData}{$name} = $data; } } return undef if defined $IO::Compress::Bzip2::VERSION and ! IO::Compress::Bzip2::ckParams($self, $got); if ($got->parsed('sparse') ) { *$self->{ZipData}{Sparse} = $got->getValue('sparse') ; *$self->{ZipData}{Method} = ZIP_CM_STORE; } if ($got->parsed('filtername')) { my $v = $got->getValue('filtername') ; *$self->{ZipData}{FilterName} = $v if ref $v eq 'CODE' ; } return 1 ; } sub outputPayload { my $self = shift ; return 1 if *$self->{ZipData}{Sparse} ; return $self->output(@_); } #sub newHeader #{ # my $self = shift ; # # return $self->mkHeader(*$self->{Got}); #} our %PARAMS = ( 'stream' => [IO::Compress::Base::Common::Parse_boolean, 1], #'store' => [IO::Compress::Base::Common::Parse_boolean, 0], 'method' => [IO::Compress::Base::Common::Parse_unsigned, ZIP_CM_DEFLATE], # # Zip header fields 'minimal' => [IO::Compress::Base::Common::Parse_boolean, 0], 'zip64' => [IO::Compress::Base::Common::Parse_boolean, 0], 'comment' => [IO::Compress::Base::Common::Parse_any, ''], 'zipcomment'=> [IO::Compress::Base::Common::Parse_any, ''], 'name' => [IO::Compress::Base::Common::Parse_any, ''], 'filtername'=> [IO::Compress::Base::Common::Parse_code, undef], 'canonicalname'=> [IO::Compress::Base::Common::Parse_boolean, 0], 'efs' => [IO::Compress::Base::Common::Parse_boolean, 0], 'time' => [IO::Compress::Base::Common::Parse_any, undef], 'extime' => [IO::Compress::Base::Common::Parse_any, undef], 'exunix2' => [IO::Compress::Base::Common::Parse_any, undef], 'exunixn' => [IO::Compress::Base::Common::Parse_any, undef], 'extattr' => [IO::Compress::Base::Common::Parse_any, $Compress::Raw::Zlib::gzip_os_code == 3 ? 0100644 << 16 : 0], 'os_code' => [IO::Compress::Base::Common::Parse_unsigned, $Compress::Raw::Zlib::gzip_os_code], 'textflag' => [IO::Compress::Base::Common::Parse_boolean, 0], 'extrafieldlocal' => [IO::Compress::Base::Common::Parse_any, undef], 'extrafieldcentral'=> [IO::Compress::Base::Common::Parse_any, undef], 'extrafieldzip64' => [IO::Compress::Base::Common::Parse_any, undef], # Lzma 'preset' => [IO::Compress::Base::Common::Parse_unsigned, 6], 'extreme' => [IO::Compress::Base::Common::Parse_boolean, 0], # For internal use only 'sparse' => [IO::Compress::Base::Common::Parse_unsigned, 0], IO::Compress::RawDeflate::getZlibParams(), defined $IO::Compress::Bzip2::VERSION ? IO::Compress::Bzip2::getExtraParams() : () ); sub getExtraParams { return %PARAMS ; } sub getInverseClass { no warnings 'once'; return ('IO::Uncompress::Unzip', \$IO::Uncompress::Unzip::UnzipError); } sub getFileInfo { my $self = shift ; my $params = shift; my $filename = shift ; if (IO::Compress::Base::Common::isaScalar($filename)) { $params->setValue(zip64 => 1) if IO::Compress::Base::Common::isGeMax32 length (${ $filename }) ; return ; } my ($mode, $uid, $gid, $size, $atime, $mtime, $ctime) ; if ( $params->parsed('storelinks') ) { ($mode, $uid, $gid, $size, $atime, $mtime, $ctime) = (lstat($filename))[2, 4,5,7, 8,9,10] ; } else { ($mode, $uid, $gid, $size, $atime, $mtime, $ctime) = (stat($filename))[2, 4,5,7, 8,9,10] ; } $params->setValue(textflag => -T $filename ) if ! $params->parsed('textflag'); $params->setValue(zip64 => 1) if IO::Compress::Base::Common::isGeMax32 $size ; $params->setValue('name' => $filename) if ! $params->parsed('name') ; $params->setValue('time' => $mtime) if ! $params->parsed('time') ; if ( ! $params->parsed('extime')) { $params->setValue('mtime' => $mtime) ; $params->setValue('atime' => $atime) ; $params->setValue('ctime' => undef) ; # No Creation time # TODO - see if can fillout creation time on non-Unix } # NOTE - Unix specific code alert if (! $params->parsed('extattr')) { use Fcntl qw(:mode) ; my $attr = $mode << 16; $attr |= ZIP_A_RONLY if ($mode & S_IWRITE) == 0 ; $attr |= ZIP_A_DIR if ($mode & S_IFMT ) == S_IFDIR ; $params->setValue('extattr' => $attr); } $params->setValue('want_exunixn', [$uid, $gid]); $params->setValue('uid' => $uid) ; $params->setValue('gid' => $gid) ; } sub mkExtendedTime { # order expected is m, a, c my $times = ''; my $bit = 1 ; my $flags = 0; for my $time (@_) { if (defined $time) { $flags |= $bit; $times .= pack("V", $time); } $bit <<= 1 ; } return IO::Compress::Zlib::Extra::mkSubField(ZIP_EXTRA_ID_EXT_TIMESTAMP, pack("C", $flags) . $times); } sub mkUnix2Extra { my $ids = ''; for my $id (@_) { $ids .= pack("v", $id); } return IO::Compress::Zlib::Extra::mkSubField(ZIP_EXTRA_ID_INFO_ZIP_UNIX2, $ids); } sub mkUnixNExtra { my $uid = shift; my $gid = shift; # Assumes UID/GID are 32-bit my $ids ; $ids .= pack "C", 1; # version $ids .= pack "C", $Config{uidsize}; $ids .= pack "V", $uid; $ids .= pack "C", $Config{gidsize}; $ids .= pack "V", $gid; return IO::Compress::Zlib::Extra::mkSubField(ZIP_EXTRA_ID_INFO_ZIP_UNIXN, $ids); } # from Archive::Zip sub _unixToDosTime # Archive::Zip::Member { my $time_t = shift; # TODO - add something to cope with unix time < 1980 my ( $sec, $min, $hour, $mday, $mon, $year ) = localtime($time_t); my $dt = 0; $dt += ( $sec >> 1 ); $dt += ( $min << 5 ); $dt += ( $hour << 11 ); $dt += ( $mday << 16 ); $dt += ( ( $mon + 1 ) << 21 ); $dt += ( ( $year - 80 ) << 25 ); return $dt; } 1; __END__ =head1 NAME IO::Compress::Zip - Write zip files/buffers =head1 SYNOPSIS use IO::Compress::Zip qw(zip $ZipError) ; my $status = zip $input => $output [,OPTS] or die "zip failed: $ZipError\n"; my $z = IO::Compress::Zip->new( $output [,OPTS] ) or die "zip failed: $ZipError\n"; $z->print($string); $z->printf($format, $string); $z->write($string); $z->syswrite($string [, $length, $offset]); $z->flush(); $z->tell(); $z->eof(); $z->seek($position, $whence); $z->binmode(); $z->fileno(); $z->opened(); $z->autoflush(); $z->input_line_number(); $z->newStream( [OPTS] ); $z->deflateParams(); $z->close() ; $ZipError ; # IO::File mode print $z $string; printf $z $format, $string; tell $z eof $z seek $z, $position, $whence binmode $z fileno $z close $z ; =head1 DESCRIPTION This module provides a Perl interface that allows writing zip compressed data to files or buffer. The primary purpose of this module is to provide streaming write access to zip files and buffers. At present the following compression methods are supported by IO::Compress::Zip =over 5 =item Store (0) =item Deflate (8) =item Bzip2 (12) To write Bzip2 content, the module C must be installed. =item Lzma (14) To write LZMA content, the module C must be installed. =item Zstandard (93) To write Zstandard content, the module C must be installed. =item Xz (95) To write Xz content, the module C must be installed. =back For reading zip files/buffers, see the companion module L. =head1 Functional Interface A top-level function, C, is provided to carry out "one-shot" compression between buffers and/or files. For finer control over the compression process, see the L section. use IO::Compress::Zip qw(zip $ZipError) ; zip $input_filename_or_reference => $output_filename_or_reference [,OPTS] or die "zip failed: $ZipError\n"; The functional interface needs Perl5.005 or better. =head2 zip $input_filename_or_reference => $output_filename_or_reference [, OPTS] C expects at least two parameters, C<$input_filename_or_reference> and C<$output_filename_or_reference> and zero or more optional parameters (see L) =head3 The C<$input_filename_or_reference> parameter The parameter, C<$input_filename_or_reference>, is used to define the source of the uncompressed data. It can take one of the following forms: =over 5 =item A filename If the C<$input_filename_or_reference> parameter is a simple scalar, it is assumed to be a filename. This file will be opened for reading and the input data will be read from it. =item A filehandle If the C<$input_filename_or_reference> parameter is a filehandle, the input data will be read from it. The string '-' can be used as an alias for standard input. =item A scalar reference If C<$input_filename_or_reference> is a scalar reference, the input data will be read from C<$$input_filename_or_reference>. =item An array reference If C<$input_filename_or_reference> is an array reference, each element in the array must be a filename. The input data will be read from each file in turn. The complete array will be walked to ensure that it only contains valid filenames before any data is compressed. =item An Input FileGlob string If C<$input_filename_or_reference> is a string that is delimited by the characters "<" and ">" C will assume that it is an I. The input is the list of files that match the fileglob. See L for more details. =back If the C<$input_filename_or_reference> parameter is any other type, C will be returned. In addition, if C<$input_filename_or_reference> corresponds to a filename from the filesystem, a number of zip file header fields will be populated by default using the following attributes from the input file =over 5 =item * the full filename contained in C<$input_filename_or_reference> =item * the file protection attributes =item * the UID/GID for the file =item * the file timestamps =back If you do not want to use these defaults they can be overridden by explicitly setting one, or more, of the C, C