File Coverage

blib/lib/Archive/Zip.pm
Criterion Covered Total %
statement 318 354 89.8
branch 22 58 37.9
condition 13 29 44.8
subroutine 96 102 94.1
pod 6 6 100.0
total 455 549 82.9


line stmt bran cond sub pod time code
1             package Archive::Zip;
2              
3             # Copyright 2000 - 2002 Ned Konz. All rights reserved. This program is free
4             # software; you can redistribute it and/or modify it under the same terms as
5             # Perl itself.
6              
7             # ----------------------------------------------------------------------
8             # class Archive::Zip
9             # Note that the package Archive::Zip exists only for exporting and
10             # sharing constants. Everything else is in another package
11             # in this file.
12             # Creation of a new Archive::Zip object actually creates a new object
13             # of class Archive::Zip::Archive.
14             # ----------------------------------------------------------------------
15              
16             BEGIN {
17 6     6   74 require 5.003_96;
18             }
19 6     6   374 use strict;
  6         55  
  6         164  
20 6     6   3191 use UNIVERSAL ();
  6         73  
  6         102  
21 6     6   93 use Carp ();
  6         54  
  6         55  
22 6     6   181 use IO::File ();
  6         62  
  6         60  
23 6     6   163 use IO::Seekable ();
  6         202  
  6         223  
24 6     6   177 use Compress::Zlib ();
  6         65  
  6         62  
25 6     6   289 use File::Spec ();
  6         65  
  6         58  
26 6     6   267 use File::Temp ();
  6         118  
  6         59  
27              
28 6     6   104 use vars qw( $VERSION @ISA );
  6         55  
  6         377  
29             BEGIN {
30 6     6   80 $VERSION = '1.18';
31 6         217 $VERSION = eval $VERSION;
32              
33 6         169 require Exporter;
34 6         85 @ISA = qw( Exporter );
35             }
36              
37 6     6   121 use vars qw( $ChunkSize $ErrorHandler );
  6         56  
  6         81  
38              
39             # This is the size we'll try to read, write, and (de)compress.
40             # You could set it to something different if you had lots of memory
41             # and needed more speed.
42             $ChunkSize = 32768;
43              
44             $ErrorHandler = \&Carp::carp;
45              
46             # BEGIN block is necessary here so that other modules can use the constants.
47 6     6   95 use vars qw( @EXPORT_OK %EXPORT_TAGS );
  6         54  
  6         75  
48             BEGIN {
49 6     6   103 @EXPORT_OK   = ('computeCRC32');
50 6         689 %EXPORT_TAGS = (
51             CONSTANTS => [ qw(
52             FA_MSDOS
53             FA_UNIX
54             GPBF_ENCRYPTED_MASK
55             GPBF_DEFLATING_COMPRESSION_MASK
56             GPBF_HAS_DATA_DESCRIPTOR_MASK
57             COMPRESSION_STORED
58             COMPRESSION_DEFLATED
59             COMPRESSION_LEVEL_NONE
60             COMPRESSION_LEVEL_DEFAULT
61             COMPRESSION_LEVEL_FASTEST
62             COMPRESSION_LEVEL_BEST_COMPRESSION
63             IFA_TEXT_FILE_MASK
64             IFA_TEXT_FILE
65             IFA_BINARY_FILE
66             ) ],
67              
68             MISC_CONSTANTS => [ qw(
69             FA_AMIGA
70             FA_VAX_VMS
71             FA_VM_CMS
72             FA_ATARI_ST
73             FA_OS2_HPFS
74             FA_MACINTOSH
75             FA_Z_SYSTEM
76             FA_CPM
77             FA_TOPS20
78             FA_WINDOWS_NTFS
79             FA_QDOS
80             FA_ACORN
81             FA_VFAT
82             FA_MVS
83             FA_BEOS
84             FA_TANDEM
85             FA_THEOS
86             GPBF_IMPLODING_8K_SLIDING_DICTIONARY_MASK
87             GPBF_IMPLODING_3_SHANNON_FANO_TREES_MASK
88             GPBF_IS_COMPRESSED_PATCHED_DATA_MASK
89             COMPRESSION_SHRUNK
90             DEFLATING_COMPRESSION_NORMAL
91             DEFLATING_COMPRESSION_MAXIMUM
92             DEFLATING_COMPRESSION_FAST
93             DEFLATING_COMPRESSION_SUPER_FAST
94             COMPRESSION_REDUCED_1
95             COMPRESSION_REDUCED_2
96             COMPRESSION_REDUCED_3
97             COMPRESSION_REDUCED_4
98             COMPRESSION_IMPLODED
99             COMPRESSION_TOKENIZED
100             COMPRESSION_DEFLATED_ENHANCED
101             COMPRESSION_PKWARE_DATA_COMPRESSION_LIBRARY_IMPLODED
102             ) ],
103              
104             ERROR_CODES => [ qw(
105             AZ_OK
106             AZ_STREAM_END
107             AZ_ERROR
108             AZ_FORMAT_ERROR
109             AZ_IO_ERROR
110             ) ],
111              
112             # For Internal Use Only
113             PKZIP_CONSTANTS => [ qw(
114             SIGNATURE_FORMAT
115             SIGNATURE_LENGTH
116             LOCAL_FILE_HEADER_SIGNATURE
117             LOCAL_FILE_HEADER_FORMAT
118             LOCAL_FILE_HEADER_LENGTH
119             CENTRAL_DIRECTORY_FILE_HEADER_SIGNATURE
120             DATA_DESCRIPTOR_FORMAT
121             DATA_DESCRIPTOR_LENGTH
122             DATA_DESCRIPTOR_SIGNATURE
123             DATA_DESCRIPTOR_FORMAT_NO_SIG
124             DATA_DESCRIPTOR_LENGTH_NO_SIG
125             CENTRAL_DIRECTORY_FILE_HEADER_FORMAT
126             CENTRAL_DIRECTORY_FILE_HEADER_LENGTH
127             END_OF_CENTRAL_DIRECTORY_SIGNATURE
128             END_OF_CENTRAL_DIRECTORY_SIGNATURE_STRING
129             END_OF_CENTRAL_DIRECTORY_FORMAT
130             END_OF_CENTRAL_DIRECTORY_LENGTH
131             ) ],
132              
133             # For Internal Use Only
134             UTILITY_METHODS => [ qw(
135             _error
136             _printError
137             _ioError
138             _formatError
139             _subclassResponsibility
140             _binmode
141             _isSeekable
142             _newFileHandle
143             _readSignature
144             _asZipDirName
145             ) ],
146             );
147              
148             # Add all the constant names and error code names to @EXPORT_OK
149 6         90 Exporter::export_ok_tags( qw(
150             CONSTANTS
151             ERROR_CODES
152             PKZIP_CONSTANTS
153             UTILITY_METHODS
154             MISC_CONSTANTS
155             ) );
156              
157             }
158              
159             # Error codes
160 6     6   102 use constant AZ_OK => 0;
  6         57  
  6         119  
161 6     6   103 use constant AZ_STREAM_END => 1;
  6         56  
  6         74  
162 6     6   85 use constant AZ_ERROR => 2;
  6         55  
  6         71  
163 6     6   89 use constant AZ_FORMAT_ERROR => 3;
  6         55  
  6         148  
164 6     6   87 use constant AZ_IO_ERROR => 4;
  6         56  
  6         75  
165              
166             # File types
167             # Values of Archive::Zip::Member->fileAttributeFormat()
168              
169 6     6   455 use constant FA_MSDOS => 0;
  6         71  
  6         73  
170 6     6   96 use constant FA_AMIGA => 1;
  6         135  
  6         85  
171 6     6   157 use constant FA_VAX_VMS => 2;
  6         55  
  6         74  
172 6     6   114 use constant FA_UNIX => 3;
  6         55  
  6         72  
173 6     6   89 use constant FA_VM_CMS => 4;
  6         58  
  6         821  
174 6     6   102 use constant FA_ATARI_ST => 5;
  6         239  
  6         78  
175 6     6   102 use constant FA_OS2_HPFS => 6;
  6         55  
  6         73  
176 6     6   95 use constant FA_MACINTOSH => 7;
  6         55  
  6         74  
177 6     6   93 use constant FA_Z_SYSTEM => 8;
  6         83  
  6         78  
178 6     6   132 use constant FA_CPM => 9;
  6         95  
  6         76  
179 6     6   86 use constant FA_TOPS20 => 10;
  6         57  
  6         73  
180 6     6   177 use constant FA_WINDOWS_NTFS => 11;
  6         58  
  6         107  
181 6     6   86 use constant FA_QDOS => 12;
  6         56  
  6         107  
182 6     6   277 use constant FA_ACORN => 13;
  6         57  
  6         80  
183 6     6   87 use constant FA_VFAT => 14;
  6         58  
  6         92  
184 6     6   88 use constant FA_MVS => 15;
  6         55  
  6         126  
185 6     6   91 use constant FA_BEOS => 16;
  6         57  
  6         71  
186 6     6   86 use constant FA_TANDEM => 17;
  6         56  
  6         72  
187 6     6   235 use constant FA_THEOS => 18;
  6         56  
  6         74  
188              
189             # general-purpose bit flag masks
190             # Found in Archive::Zip::Member->bitFlag()
191              
192 6     6   87 use constant GPBF_ENCRYPTED_MASK => 1 << 0;
  6         52  
  6         75  
193 6     6   93 use constant GPBF_DEFLATING_COMPRESSION_MASK => 3 << 1;
  6         56  
  6         72  
194 6     6   89 use constant GPBF_HAS_DATA_DESCRIPTOR_MASK => 1 << 3;
  6         58  
  6         77  
195              
196             # deflating compression types, if compressionMethod == COMPRESSION_DEFLATED
197             # ( Archive::Zip::Member->bitFlag() & GPBF_DEFLATING_COMPRESSION_MASK )
198              
199 6     6   87 use constant DEFLATING_COMPRESSION_NORMAL => 0 << 1;
  6         55  
  6         77  
200 6     6   137 use constant DEFLATING_COMPRESSION_MAXIMUM => 1 << 1;
  6         57  
  6         145  
201 6     6   170 use constant DEFLATING_COMPRESSION_FAST => 2 << 1;
  6         56  
  6         75  
202 6     6   88 use constant DEFLATING_COMPRESSION_SUPER_FAST => 3 << 1;
  6         58  
  6         76  
203              
204             # compression method
205              
206             # these two are the only ones supported in this module
207 6     6   215 use constant COMPRESSION_STORED => 0; # file is stored (no compression)
  6         58  
  6         79  
208 6     6   90 use constant COMPRESSION_DEFLATED => 8; # file is Deflated
  6         84  
  6         72  
209 6     6   88 use constant COMPRESSION_LEVEL_NONE => 0;
  6         58  
  6         75  
210 6     6   88 use constant COMPRESSION_LEVEL_DEFAULT => -1;
  6         53  
  6         523  
211 6     6   89 use constant COMPRESSION_LEVEL_FASTEST => 1;
  6         102  
  6         72  
212 6     6   91 use constant COMPRESSION_LEVEL_BEST_COMPRESSION => 9;
  6         56  
  6         72  
213              
214             # internal file attribute bits
215             # Found in Archive::Zip::Member::internalFileAttributes()
216              
217 6     6   93 use constant IFA_TEXT_FILE_MASK => 1;
  6         55  
  6         76  
218 6     6   88 use constant IFA_TEXT_FILE => 1;
  6         82  
  6         74  
219 6     6   143 use constant IFA_BINARY_FILE => 0;
  6         82  
  6         737  
220              
221             # PKZIP file format miscellaneous constants (for internal use only)
222 6     6   90 use constant SIGNATURE_FORMAT => "V";
  6         53  
  6         214  
223 6     6   92 use constant SIGNATURE_LENGTH => 4;
  6         53  
  6         73  
224              
225             # these lengths are without the signature.
226 6     6   93 use constant LOCAL_FILE_HEADER_SIGNATURE => 0x04034b50;
  6         220  
  6         105  
227 6     6   115 use constant LOCAL_FILE_HEADER_FORMAT => "v3 V4 v2";
  6         54  
  6         71  
228 6     6   90 use constant LOCAL_FILE_HEADER_LENGTH => 26;
  6         56  
  6         74  
229              
230             # PKZIP docs don't mention the signature, but Info-Zip writes it.
231 6     6   85 use constant DATA_DESCRIPTOR_SIGNATURE => 0x08074b50;
  6         293  
  6         83  
232 6     6   92 use constant DATA_DESCRIPTOR_FORMAT => "V3";
  6         52  
  6         100  
233 6     6   89 use constant DATA_DESCRIPTOR_LENGTH => 12;
  6         57  
  6         72  
234              
235             # but the signature is apparently optional.
236 6     6   88 use constant DATA_DESCRIPTOR_FORMAT_NO_SIG => "V2";
  6         54  
  6         73  
237 6     6   128 use constant DATA_DESCRIPTOR_LENGTH_NO_SIG => 8;
  6         57  
  6         71  
238              
239 6     6   89 use constant CENTRAL_DIRECTORY_FILE_HEADER_SIGNATURE => 0x02014b50;
  6         55  
  6         169  
240 6     6   87 use constant CENTRAL_DIRECTORY_FILE_HEADER_FORMAT => "C2 v3 V4 v5 V2";
  6         56  
  6         73  
241 6     6   112 use constant CENTRAL_DIRECTORY_FILE_HEADER_LENGTH => 42;
  6         86  
  6         73  
242              
243 6     6   93 use constant END_OF_CENTRAL_DIRECTORY_SIGNATURE => 0x06054b50;
  6         56  
  6         74  
244 6         92 use constant END_OF_CENTRAL_DIRECTORY_SIGNATURE_STRING =>
245 6     6   94 pack( "V", END_OF_CENTRAL_DIRECTORY_SIGNATURE );
  6         99  
246 6     6   89 use constant END_OF_CENTRAL_DIRECTORY_FORMAT => "v4 V2 v";
  6         55  
  6         135  
247 6     6   2437 use constant END_OF_CENTRAL_DIRECTORY_LENGTH => 18;
  6         81  
  6         83  
248              
249 6     6   102 use constant GPBF_IMPLODING_8K_SLIDING_DICTIONARY_MASK => 1 << 1;
  6         58  
  6         77  
250 6     6   257 use constant GPBF_IMPLODING_3_SHANNON_FANO_TREES_MASK => 1 << 2;
  6         109  
  6         76  
251 6     6   92 use constant GPBF_IS_COMPRESSED_PATCHED_DATA_MASK => 1 << 5;
  6         54  
  6         73  
252              
253             # the rest of these are not supported in this module
254 6     6   113 use constant COMPRESSION_SHRUNK => 1; # file is Shrunk
  6         53  
  6         104  
255 6     6   120 use constant COMPRESSION_REDUCED_1 => 2; # file is Reduced CF=1
  6         56  
  6         100  
256 6     6   90 use constant COMPRESSION_REDUCED_2 => 3; # file is Reduced CF=2
  6         54  
  6         74  
257 6     6   89 use constant COMPRESSION_REDUCED_3 => 4; # file is Reduced CF=3
  6         211  
  6         105  
258 6     6   91 use constant COMPRESSION_REDUCED_4 => 5; # file is Reduced CF=4
  6         56  
  6         75  
259 6     6   121 use constant COMPRESSION_IMPLODED => 6; # file is Imploded
  6         56  
  6         92  
260 6     6   127 use constant COMPRESSION_TOKENIZED => 7; # reserved for Tokenizing compr.
  6         57  
  6         101  
261 6     6   90 use constant COMPRESSION_DEFLATED_ENHANCED => 9; # reserved for enh. Deflating
  6         54  
  6         74  
262 6     6   89 use constant COMPRESSION_PKWARE_DATA_COMPRESSION_LIBRARY_IMPLODED => 10;
  6         56  
  6         75  
263              
264             # Load the various required classes
265             require Archive::Zip::Archive;
266             require Archive::Zip::Member;
267             require Archive::Zip::FileMember;
268             require Archive::Zip::DirectoryMember;
269             require Archive::Zip::ZipFileMember;
270             require Archive::Zip::NewFileMember;
271             require Archive::Zip::StringMember;
272              
273 6     6   95 use constant ZIPARCHIVECLASS => 'Archive::Zip::Archive';
  6         56  
  6         102  
274 6     6   96 use constan