Commit | Line | Data |
a1787f24 |
1 | BEGIN { |
2 | if ($ENV{PERL_CORE}) { |
3 | chdir 't' if -d 't'; |
4 | @INC = ("../lib", "lib/compress"); |
5 | } |
6 | } |
7 | |
8 | use lib qw(t t/compress); |
9 | use strict; |
10 | use warnings; |
11 | use bytes; |
12 | |
13 | use Test::More ; |
14 | use CompTestUtils; |
15 | |
16 | BEGIN { |
17 | # use Test::NoWarnings, if available |
18 | my $extra = 0 ; |
19 | $extra = 1 |
20 | if eval { require Test::NoWarnings ; import Test::NoWarnings; 1 }; |
21 | |
22 | plan tests => 62 + $extra ; |
23 | |
24 | use_ok('IO::Compress::Zip', qw(:all)) ; |
25 | use_ok('IO::Uncompress::Unzip', qw(unzip $UnzipError)) ; |
26 | |
27 | eval { |
28 | require IO::Compress::Bzip2 ; |
29 | import IO::Compress::Bzip2 2.010 ; |
30 | require IO::Uncompress::Bunzip2 ; |
31 | import IO::Uncompress::Bunzip2 2.010 ; |
32 | } ; |
33 | |
34 | } |
35 | |
36 | |
37 | sub getContent |
38 | { |
39 | my $filename = shift; |
40 | |
41 | my $u = new IO::Uncompress::Unzip $filename, Append => 1 |
42 | or die "Cannot open $filename: $UnzipError"; |
43 | |
44 | isa_ok $u, "IO::Uncompress::Unzip"; |
45 | |
46 | my @content; |
47 | my $status ; |
48 | |
49 | for ($status = 1; ! $u->eof(); $status = $u->nextStream()) |
50 | { |
51 | |
52 | my $name = $u->getHeaderInfo()->{Name}; |
53 | #warn "Processing member $name\n" ; |
54 | |
55 | my $buff = ''; |
56 | 1 while ($status = $u->read($buff)) > 0; |
57 | |
58 | push @content, $buff; |
59 | last unless $status == 0; |
60 | } |
61 | |
62 | die "Error processing $filename: $status $!\n" |
63 | if $status < 0 ; |
64 | |
65 | return @content; |
66 | } |
67 | |
68 | |
69 | { |
70 | title "Create a simple zip - All Deflate"; |
71 | |
72 | my $lex = new LexFile my $file1; |
73 | |
74 | my @content = ( |
75 | 'hello', |
76 | '', |
77 | 'goodbye ', |
78 | ); |
79 | |
80 | my $zip = new IO::Compress::Zip $file1, |
81 | Name => "one", Method => ZIP_CM_DEFLATE, Stream => 0; |
82 | isa_ok $zip, "IO::Compress::Zip"; |
83 | |
84 | is $zip->write($content[0]), length($content[0]), "write"; |
85 | $zip->newStream(Name=> "two", Method => ZIP_CM_DEFLATE); |
86 | is $zip->write($content[1]), length($content[1]), "write"; |
87 | $zip->newStream(Name=> "three", Method => ZIP_CM_DEFLATE); |
88 | is $zip->write($content[2]), length($content[2]), "write"; |
89 | ok $zip->close(), "closed"; |
90 | |
91 | my @got = getContent($file1); |
92 | |
93 | is $got[0], $content[0], "Got 1st entry"; |
94 | is $got[1], $content[1], "Got 2nd entry"; |
95 | is $got[2], $content[2], "Got 3nd entry"; |
96 | } |
97 | |
98 | SKIP: |
99 | { |
100 | title "Create a simple zip - All Bzip2"; |
101 | |
102 | skip "IO::Compress::Bzip2 not available", 9 |
103 | unless defined $IO::Compress::Bzip2::VERSION; |
104 | |
105 | my $lex = new LexFile my $file1; |
106 | |
107 | my @content = ( |
108 | 'hello', |
109 | '', |
110 | 'goodbye ', |
111 | ); |
112 | |
113 | my $zip = new IO::Compress::Zip $file1, |
114 | Name => "one", Method => ZIP_CM_BZIP2, Stream => 0; |
115 | isa_ok $zip, "IO::Compress::Zip"; |
116 | |
117 | is $zip->write($content[0]), length($content[0]), "write"; |
118 | $zip->newStream(Name=> "two", Method => ZIP_CM_BZIP2); |
119 | is $zip->write($content[1]), length($content[1]), "write"; |
120 | $zip->newStream(Name=> "three", Method => ZIP_CM_BZIP2); |
121 | is $zip->write($content[2]), length($content[2]), "write"; |
122 | ok $zip->close(), "closed"; |
123 | |
124 | my @got = getContent($file1); |
125 | |
126 | is $got[0], $content[0], "Got 1st entry"; |
127 | is $got[1], $content[1], "Got 2nd entry"; |
128 | is $got[2], $content[2], "Got 3nd entry"; |
129 | } |
130 | |
131 | SKIP: |
132 | { |
133 | title "Create a simple zip - Deflate + Bzip2"; |
134 | |
135 | skip "IO::Compress::Bzip2 not available", 9 |
136 | unless $IO::Compress::Bzip2::VERSION; |
137 | |
138 | my $lex = new LexFile my $file1; |
139 | |
140 | my @content = ( |
141 | 'hello', |
142 | 'and', |
143 | 'goodbye ', |
144 | ); |
145 | |
146 | my $zip = new IO::Compress::Zip $file1, |
147 | Name => "one", Method => ZIP_CM_DEFLATE, Stream => 0; |
148 | isa_ok $zip, "IO::Compress::Zip"; |
149 | |
150 | is $zip->write($content[0]), length($content[0]), "write"; |
151 | $zip->newStream(Name=> "two", Method => ZIP_CM_BZIP2); |
152 | is $zip->write($content[1]), length($content[1]), "write"; |
153 | $zip->newStream(Name=> "three", Method => ZIP_CM_DEFLATE); |
154 | is $zip->write($content[2]), length($content[2]), "write"; |
155 | ok $zip->close(), "closed"; |
156 | |
157 | my @got = getContent($file1); |
158 | |
159 | is $got[0], $content[0], "Got 1st entry"; |
160 | is $got[1], $content[1], "Got 2nd entry"; |
161 | is $got[2], $content[2], "Got 3nd entry"; |
162 | } |
163 | |
164 | { |
165 | title "Create a simple zip - All STORE"; |
166 | |
167 | my $lex = new LexFile my $file1; |
168 | |
169 | my @content = ( |
170 | 'hello', |
171 | '', |
172 | 'goodbye ', |
173 | ); |
174 | |
175 | my $zip = new IO::Compress::Zip $file1, |
176 | Name => "one", Method => ZIP_CM_STORE, Stream => 0; |
177 | isa_ok $zip, "IO::Compress::Zip"; |
178 | |
179 | is $zip->write($content[0]), length($content[0]), "write"; |
180 | $zip->newStream(Name=> "two", Method => ZIP_CM_STORE); |
181 | is $zip->write($content[1]), length($content[1]), "write"; |
182 | $zip->newStream(Name=> "three", Method => ZIP_CM_STORE); |
183 | is $zip->write($content[2]), length($content[2]), "write"; |
184 | ok $zip->close(), "closed"; |
185 | |
186 | my @got = getContent($file1); |
187 | |
188 | is $got[0], $content[0], "Got 1st entry"; |
189 | is $got[1], $content[1], "Got 2nd entry"; |
190 | is $got[2], $content[2], "Got 3nd entry"; |
191 | } |
192 | |
193 | { |
194 | title "Create a simple zip - Deflate + STORE"; |
195 | |
196 | #my $lex = new LexFile my $file1; |
197 | my $file1 = "tryout.zip"; |
198 | |
199 | my @content = qw( |
200 | hello |
201 | and |
202 | goodbye |
203 | ); |
204 | |
205 | my $zip = new IO::Compress::Zip $file1, |
206 | Name => "one", Method => ZIP_CM_DEFLATE, Stream => 0; |
207 | isa_ok $zip, "IO::Compress::Zip"; |
208 | |
209 | is $zip->write($content[0]), length($content[0]), "write"; |
210 | $zip->newStream(Name=> "two", Method => ZIP_CM_STORE); |
211 | is $zip->write($content[1]), length($content[1]), "write"; |
212 | $zip->newStream(Name=> "three", Method => ZIP_CM_DEFLATE); |
213 | is $zip->write($content[2]), length($content[2]), "write"; |
214 | ok $zip->close(), "closed"; |
215 | |
216 | my @got = getContent($file1); |
217 | |
218 | is $got[0], $content[0], "Got 1st entry"; |
219 | is $got[1], $content[1], "Got 2nd entry"; |
220 | is $got[2], $content[2], "Got 3nd entry"; |
221 | } |
222 | |
223 | { |
224 | title "Create a simple zip - Deflate + zero length STORE"; |
225 | |
226 | my $lex = new LexFile my $file1; |
227 | |
228 | my @content = ( |
229 | 'hello ', |
230 | '', |
231 | 'goodbye ', |
232 | ); |
233 | |
234 | my $zip = new IO::Compress::Zip $file1, |
235 | Name => "one", Method => ZIP_CM_DEFLATE, Stream => 0; |
236 | isa_ok $zip, "IO::Compress::Zip"; |
237 | |
238 | is $zip->write($content[0]), length($content[0]), "write"; |
239 | $zip->newStream(Name=> "two", Method => ZIP_CM_STORE); |
240 | is $zip->write($content[1]), length($content[1]), "write"; |
241 | $zip->newStream(Name=> "three", Method => ZIP_CM_DEFLATE); |
242 | is $zip->write($content[2]), length($content[2]), "write"; |
243 | ok $zip->close(), "closed"; |
244 | |
245 | my @got = getContent($file1); |
246 | |
247 | is $got[0], $content[0], "Got 1st entry"; |
248 | ok $got[1] eq $content[1], "Got 2nd entry"; |
249 | is $got[2], $content[2], "Got 3nd entry"; |
250 | } |
251 | |