4 @INC = ("../lib", "lib/compress");
8 use lib qw(t t/compress);
17 plan(skip_all => "oneshot needs Perl 5.005 or better - you have Perl $]" )
21 # use Test::NoWarnings, if available
24 if eval { require Test::NoWarnings ; import Test::NoWarnings; 1 };
26 plan tests => 146 + $extra ;
28 #use_ok('IO::Compress::Zip', qw(zip $ZipError :zip_method)) ;
29 use_ok('IO::Compress::Zip', qw(:all)) ;
30 use_ok('IO::Uncompress::Unzip', qw(unzip $UnzipError)) ;
45 ok zip($in, \$out, %opts), " zip ok" ;
46 ok unzip(\$out, \$got), " unzip ok"
48 is $got, $content, " got expected content" ;
50 my $gunz = new IO::Uncompress::Unzip \$out, Strict => 0
51 or diag "UnzipError is $IO::Uncompress::Unzip::UnzipError" ;
52 ok $gunz, " Created IO::Uncompress::Unzip object";
53 my $hdr = $gunz->getHeaderInfo();
54 ok $hdr, " got Header info";
56 ok $gunz->read($uncomp), " read ok" ;
57 is $uncomp, $content, " got expected content";
58 ok $gunz->close, " closed ok" ;
65 title "Check zip header default NAME & MTIME settings" ;
67 my $lex = new LexFile my $file1;
69 my $content = "hello ";
73 writeFile($file1, $content);
74 $mtime = (stat($file1))[9];
75 # make sure that the zip file isn't created in the same
76 # second as the input file
78 $hdr = zipGetHeader($file1, $content);
80 is $hdr->{Name}, $file1, " Name is '$file1'";
81 is $hdr->{Time}>>1, $mtime>>1, " Time is ok";
83 title "Override Name" ;
85 writeFile($file1, $content);
86 $mtime = (stat($file1))[9];
88 $hdr = zipGetHeader($file1, $content, Name => "abcde");
90 is $hdr->{Name}, "abcde", " Name is 'abcde'" ;
91 is $hdr->{Time} >> 1, $mtime >> 1, " Time is ok";
93 title "Override Time" ;
95 writeFile($file1, $content);
96 my $useTime = time + 2000 ;
97 $hdr = zipGetHeader($file1, $content, Time => $useTime);
99 is $hdr->{Name}, $file1, " Name is '$file1'" ;
100 is $hdr->{Time} >> 1 , $useTime >> 1 , " Time is $useTime";
102 title "Override Name and Time" ;
104 $useTime = time + 5000 ;
105 writeFile($file1, $content);
106 $hdr = zipGetHeader($file1, $content, Time => $useTime, Name => "abcde");
108 is $hdr->{Name}, "abcde", " Name is 'abcde'" ;
109 is $hdr->{Time} >> 1 , $useTime >> 1 , " Time is $useTime";
111 title "Filehandle doesn't have default Name or Time" ;
112 my $fh = new IO::File "< $file1"
113 or diag "Cannot open '$file1': $!\n" ;
116 $hdr = zipGetHeader($fh, $content);
119 ok ! defined $hdr->{Name}, " Name is undef";
120 cmp_ok $hdr->{Time} >> 1, '>=', $before >> 1, " Time is ok";
121 cmp_ok $hdr->{Time} >> 1, '<=', $after >> 1, " Time is ok";
125 title "Buffer doesn't have default Name or Time" ;
126 my $buffer = $content;
128 $hdr = zipGetHeader(\$buffer, $content);
131 ok ! defined $hdr->{Name}, " Name is undef";
132 cmp_ok $hdr->{Time} >> 1, '>=', $before >> 1, " Time is ok";
133 cmp_ok $hdr->{Time} >> 1, '<=', $after >> 1, " Time is ok";
136 for my $stream (0, 1)
140 next if $zip64 && ! $stream;
142 for my $method (ZIP_CM_STORE, ZIP_CM_DEFLATE)
145 title "Stream $stream, Zip64 $zip64, Method $method";
147 my $lex = new LexFile my $file1;
149 my $content = "hello ";
150 #writeFile($file1, $content);
152 my $status = zip(\$content => $file1 ,
157 ok $status, " zip ok"
161 if ($stream && $method == ZIP_CM_STORE ) {
162 #eval ' unzip($file1 => \$got) ';
163 ok ! unzip($file1 => \$got), " unzip fails";
164 like $UnzipError, "/Streamed Stored content not supported/",
165 " Streamed Stored content not supported";
169 ok unzip($file1 => \$got), " unzip ok"
170 or diag $UnzipError ;
172 is $got, $content, " content ok";
174 my $u = new IO::Uncompress::Unzip $file1
177 my $hdr = $u->getHeaderInfo();
178 ok $hdr, " got header";
180 is $hdr->{Stream}, $stream, " stream is $stream" ;
181 is $hdr->{MethodID}, $method, " MethodID is $method" ;
182 is $hdr->{Zip64}, $zip64, " Zip64 is $zip64" ;
187 for my $stream (0, 1)
191 next if $zip64 && ! $stream;
192 for my $method (ZIP_CM_STORE, ZIP_CM_DEFLATE)
194 title "Stream $stream, Zip64 $zip64, Method $method";
199 my $lex = new LexFile $file1, $file2, $zipfile;
201 my $content1 = "hello ";
202 writeFile($file1, $content1);
204 my $content2 = "goodbye ";
205 writeFile($file2, $content2);
207 my %content = ( $file1 => $content1,
211 ok zip([$file1, $file2] => $zipfile , Method => $method,
213 Stream => $stream), " zip ok"
216 for my $file ($file1, $file2)
219 if ($stream && $method == ZIP_CM_STORE ) {
220 #eval ' unzip($zipfile => \$got) ';
221 ok ! unzip($zipfile => \$got, Name => $file), " unzip fails";
222 like $UnzipError, "/Streamed Stored content not supported/",
223 " Streamed Stored content not supported";
227 ok unzip($zipfile => \$got, Name => $file), " unzip $file ok"
228 or diag $UnzipError ;
230 is $got, $content{$file}, " content ok";
236 # TODO add more error cases