Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
6954 dpurdie 1
package Archive::Zip;
2
 
3
use 5.006;
4
use strict;
5
use Carp                ();
6
use Cwd                 ();
7
use IO::File            ();
8
use IO::Seekable        ();
9
use Compress::Raw::Zlib ();
10
use File::Spec          ();
11
use File::Temp          ();
12
use FileHandle          ();
13
 
14
use vars qw( $VERSION @ISA );
15
 
16
BEGIN {
17
    $VERSION = '1.57';
18
 
19
    require Exporter;
20
    @ISA = qw( Exporter );
21
}
22
 
23
use vars qw( $ChunkSize $ErrorHandler );
24
 
25
BEGIN {
26
    # This is the size we'll try to read, write, and (de)compress.
27
    # You could set it to something different if you had lots of memory
28
    # and needed more speed.
29
    $ChunkSize ||= 32768;
30
 
31
    $ErrorHandler = \&Carp::carp;
32
}
33
 
34
# BEGIN block is necessary here so that other modules can use the constants.
35
use vars qw( @EXPORT_OK %EXPORT_TAGS );
36
 
37
BEGIN {
38
    @EXPORT_OK   = ('computeCRC32');
39
    %EXPORT_TAGS = (
40
        CONSTANTS => [
41
            qw(
42
              FA_MSDOS
43
              FA_UNIX
44
              GPBF_ENCRYPTED_MASK
45
              GPBF_DEFLATING_COMPRESSION_MASK
46
              GPBF_HAS_DATA_DESCRIPTOR_MASK
47
              COMPRESSION_STORED
48
              COMPRESSION_DEFLATED
49
              COMPRESSION_LEVEL_NONE
50
              COMPRESSION_LEVEL_DEFAULT
51
              COMPRESSION_LEVEL_FASTEST
52
              COMPRESSION_LEVEL_BEST_COMPRESSION
53
              IFA_TEXT_FILE_MASK
54
              IFA_TEXT_FILE
55
              IFA_BINARY_FILE
56
              )
57
        ],
58
 
59
        MISC_CONSTANTS => [
60
            qw(
61
              FA_AMIGA
62
              FA_VAX_VMS
63
              FA_VM_CMS
64
              FA_ATARI_ST
65
              FA_OS2_HPFS
66
              FA_MACINTOSH
67
              FA_Z_SYSTEM
68
              FA_CPM
69
              FA_TOPS20
70
              FA_WINDOWS_NTFS
71
              FA_QDOS
72
              FA_ACORN
73
              FA_VFAT
74
              FA_MVS
75
              FA_BEOS
76
              FA_TANDEM
77
              FA_THEOS
78
              GPBF_IMPLODING_8K_SLIDING_DICTIONARY_MASK
79
              GPBF_IMPLODING_3_SHANNON_FANO_TREES_MASK
80
              GPBF_IS_COMPRESSED_PATCHED_DATA_MASK
81
              COMPRESSION_SHRUNK
82
              DEFLATING_COMPRESSION_NORMAL
83
              DEFLATING_COMPRESSION_MAXIMUM
84
              DEFLATING_COMPRESSION_FAST
85
              DEFLATING_COMPRESSION_SUPER_FAST
86
              COMPRESSION_REDUCED_1
87
              COMPRESSION_REDUCED_2
88
              COMPRESSION_REDUCED_3
89
              COMPRESSION_REDUCED_4
90
              COMPRESSION_IMPLODED
91
              COMPRESSION_TOKENIZED
92
              COMPRESSION_DEFLATED_ENHANCED
93
              COMPRESSION_PKWARE_DATA_COMPRESSION_LIBRARY_IMPLODED
94
              )
95
        ],
96
 
97
        ERROR_CODES => [
98
            qw(
99
              AZ_OK
100
              AZ_STREAM_END
101
              AZ_ERROR
102
              AZ_FORMAT_ERROR
103
              AZ_IO_ERROR
104
              )
105
        ],
106
 
107
        # For Internal Use Only
108
        PKZIP_CONSTANTS => [
109
            qw(
110
              SIGNATURE_FORMAT
111
              SIGNATURE_LENGTH
112
 
113
              LOCAL_FILE_HEADER_SIGNATURE
114
              LOCAL_FILE_HEADER_FORMAT
115
              LOCAL_FILE_HEADER_LENGTH
116
 
117
              DATA_DESCRIPTOR_SIGNATURE
118
              DATA_DESCRIPTOR_FORMAT
119
              DATA_DESCRIPTOR_LENGTH
120
 
121
              DATA_DESCRIPTOR_FORMAT_NO_SIG
122
              DATA_DESCRIPTOR_LENGTH_NO_SIG
123
 
124
              CENTRAL_DIRECTORY_FILE_HEADER_SIGNATURE
125
              CENTRAL_DIRECTORY_FILE_HEADER_FORMAT
126
              CENTRAL_DIRECTORY_FILE_HEADER_LENGTH
127
 
128
              ZIP64_END_OF_CENTRAL_DIRECTORY_RECORD_SIGNATURE
129
              ZIP64_END_OF_CENTRAL_DIRECTORY_RECORD_FORMAT
130
              ZIP64_END_OF_CENTRAL_DIRECTORY_RECORD_LENGTH
131
 
132
              ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_SIGNATURE
133
              ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_FORMAT
134
              ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_LENGTH
135
 
136
              END_OF_CENTRAL_DIRECTORY_SIGNATURE
137
              END_OF_CENTRAL_DIRECTORY_FORMAT
138
              END_OF_CENTRAL_DIRECTORY_LENGTH
139
 
140
              END_OF_CENTRAL_DIRECTORY_SIGNATURE_STRING
141
              )
142
        ],
143
 
144
        # For Internal Use Only
145
        UTILITY_METHODS => [
146
            qw(
147
              _error
148
              _printError
149
              _ioError
150
              _formatError
151
              _subclassResponsibility
152
              _binmode
153
              _isSeekable
154
              _newFileHandle
155
              _readSignature
156
              _asZipDirName
157
              )
158
        ],
159
    );
160
 
161
    # Add all the constant names and error code names to @EXPORT_OK
162
    Exporter::export_ok_tags(
163
        qw(
164
          CONSTANTS
165
          ERROR_CODES
166
          PKZIP_CONSTANTS
167
          UTILITY_METHODS
168
          MISC_CONSTANTS
169
          ));
170
 
171
}
172
 
173
# Error codes
174
use constant AZ_OK           => 0;
175
use constant AZ_STREAM_END   => 1;
176
use constant AZ_ERROR        => 2;
177
use constant AZ_FORMAT_ERROR => 3;
178
use constant AZ_IO_ERROR     => 4;
179
 
180
# File types
181
# Values of Archive::Zip::Member->fileAttributeFormat()
182
 
183
use constant FA_MSDOS        => 0;
184
use constant FA_AMIGA        => 1;
185
use constant FA_VAX_VMS      => 2;
186
use constant FA_UNIX         => 3;
187
use constant FA_VM_CMS       => 4;
188
use constant FA_ATARI_ST     => 5;
189
use constant FA_OS2_HPFS     => 6;
190
use constant FA_MACINTOSH    => 7;
191
use constant FA_Z_SYSTEM     => 8;
192
use constant FA_CPM          => 9;
193
use constant FA_TOPS20       => 10;
194
use constant FA_WINDOWS_NTFS => 11;
195
use constant FA_QDOS         => 12;
196
use constant FA_ACORN        => 13;
197
use constant FA_VFAT         => 14;
198
use constant FA_MVS          => 15;
199
use constant FA_BEOS         => 16;
200
use constant FA_TANDEM       => 17;
201
use constant FA_THEOS        => 18;
202
 
203
# general-purpose bit flag masks
204
# Found in Archive::Zip::Member->bitFlag()
205
 
206
use constant GPBF_ENCRYPTED_MASK             => 1 << 0;
207
use constant GPBF_DEFLATING_COMPRESSION_MASK => 3 << 1;
208
use constant GPBF_HAS_DATA_DESCRIPTOR_MASK   => 1 << 3;
209
 
210
# deflating compression types, if compressionMethod == COMPRESSION_DEFLATED
211
# ( Archive::Zip::Member->bitFlag() & GPBF_DEFLATING_COMPRESSION_MASK )
212
 
213
use constant DEFLATING_COMPRESSION_NORMAL     => 0 << 1;
214
use constant DEFLATING_COMPRESSION_MAXIMUM    => 1 << 1;
215
use constant DEFLATING_COMPRESSION_FAST       => 2 << 1;
216
use constant DEFLATING_COMPRESSION_SUPER_FAST => 3 << 1;
217
 
218
# compression method
219
 
220
# these two are the only ones supported in this module
221
use constant COMPRESSION_STORED        => 0;   # file is stored (no compression)
222
use constant COMPRESSION_DEFLATED      => 8;   # file is Deflated
223
use constant COMPRESSION_LEVEL_NONE    => 0;
224
use constant COMPRESSION_LEVEL_DEFAULT => -1;
225
use constant COMPRESSION_LEVEL_FASTEST => 1;
226
use constant COMPRESSION_LEVEL_BEST_COMPRESSION => 9;
227
 
228
# internal file attribute bits
229
# Found in Archive::Zip::Member::internalFileAttributes()
230
 
231
use constant IFA_TEXT_FILE_MASK => 1;
232
use constant IFA_TEXT_FILE      => 1;
233
use constant IFA_BINARY_FILE    => 0;
234
 
235
# PKZIP file format miscellaneous constants (for internal use only)
236
use constant SIGNATURE_FORMAT => "V";
237
use constant SIGNATURE_LENGTH => 4;
238
 
239
# these lengths are without the signature.
240
use constant LOCAL_FILE_HEADER_SIGNATURE => 0x04034b50;
241
use constant LOCAL_FILE_HEADER_FORMAT    => "v3 V4 v2";
242
use constant LOCAL_FILE_HEADER_LENGTH    => 26;
243
 
244
# PKZIP docs don't mention the signature, but Info-Zip writes it.
245
use constant DATA_DESCRIPTOR_SIGNATURE => 0x08074b50;
246
use constant DATA_DESCRIPTOR_FORMAT    => "V3";
247
use constant DATA_DESCRIPTOR_LENGTH    => 12;
248
 
249
# but the signature is apparently optional.
250
use constant DATA_DESCRIPTOR_FORMAT_NO_SIG => "V2";
251
use constant DATA_DESCRIPTOR_LENGTH_NO_SIG => 8;
252
 
253
use constant CENTRAL_DIRECTORY_FILE_HEADER_SIGNATURE => 0x02014b50;
254
use constant CENTRAL_DIRECTORY_FILE_HEADER_FORMAT    => "C2 v3 V4 v5 V2";
255
use constant CENTRAL_DIRECTORY_FILE_HEADER_LENGTH    => 42;
256
 
257
# zip64 support
258
use constant ZIP64_END_OF_CENTRAL_DIRECTORY_RECORD_SIGNATURE => 0x06064b50;
259
use constant ZIP64_END_OF_CENTRAL_DIRECTORY_RECORD_FORMAT => 0;
260
use constant ZIP64_END_OF_CENTRAL_DIRECTORY_RECORD_LENGTH => 0;
261
 
262
use constant ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_SIGNATURE => 0x07064b50;
263
use constant ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_FORMAT => 0;
264
use constant ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_LENGTH => 0;
265
 
266
 
267
use constant END_OF_CENTRAL_DIRECTORY_SIGNATURE => 0x06054b50;
268
use constant END_OF_CENTRAL_DIRECTORY_SIGNATURE_STRING =>
269
  pack("V", END_OF_CENTRAL_DIRECTORY_SIGNATURE);
270
use constant END_OF_CENTRAL_DIRECTORY_FORMAT => "v4 V2 v";
271
use constant END_OF_CENTRAL_DIRECTORY_LENGTH => 18;
272
 
273
use constant GPBF_IMPLODING_8K_SLIDING_DICTIONARY_MASK => 1 << 1;
274
use constant GPBF_IMPLODING_3_SHANNON_FANO_TREES_MASK  => 1 << 2;
275
use constant GPBF_IS_COMPRESSED_PATCHED_DATA_MASK      => 1 << 5;
276
 
277
# the rest of these are not supported in this module
278
use constant COMPRESSION_SHRUNK    => 1;    # file is Shrunk
279
use constant COMPRESSION_REDUCED_1 => 2;    # file is Reduced CF=1
280
use constant COMPRESSION_REDUCED_2 => 3;    # file is Reduced CF=2
281
use constant COMPRESSION_REDUCED_3 => 4;    # file is Reduced CF=3
282
use constant COMPRESSION_REDUCED_4 => 5;    # file is Reduced CF=4
283
use constant COMPRESSION_IMPLODED  => 6;    # file is Imploded
284
use constant COMPRESSION_TOKENIZED => 7;    # reserved for Tokenizing compr.
285
use constant COMPRESSION_DEFLATED_ENHANCED => 9;   # reserved for enh. Deflating
286
use constant COMPRESSION_PKWARE_DATA_COMPRESSION_LIBRARY_IMPLODED => 10;
287
 
288
# Load the various required classes
289
require Archive::Zip::Archive;
290
require Archive::Zip::Member;
291
require Archive::Zip::FileMember;
292
require Archive::Zip::DirectoryMember;
293
require Archive::Zip::ZipFileMember;
294
require Archive::Zip::NewFileMember;
295
require Archive::Zip::StringMember;
296
 
297
# Convenience functions
298
 
299
sub _ISA ($$) {
300
 
301
    # Can't rely on Scalar::Util, so use the next best way
302
    local $@;
303
    !!eval { ref $_[0] and $_[0]->isa($_[1]) };
304
}
305
 
306
sub _CAN ($$) {
307
    local $@;
308
    !!eval { ref $_[0] and $_[0]->can($_[1]) };
309
}
310
 
311
#####################################################################
312
# Methods
313
 
314
sub new {
315
    my $class = shift;
316
    return Archive::Zip::Archive->new(@_);
317
}
318
 
319
sub computeCRC32 {
320
    my ($data, $crc);
321
 
322
    if (ref($_[0]) eq 'HASH') {
323
        $data = $_[0]->{string};
324
        $crc  = $_[0]->{checksum};
325
    } else {
326
        $data = shift;
327
        $data = shift if ref($data);
328
        $crc  = shift;
329
    }
330
 
331
    return Compress::Raw::Zlib::crc32($data, $crc);
332
}
333
 
334
# Report or change chunk size used for reading and writing.
335
# Also sets Zlib's default buffer size (eventually).
336
sub setChunkSize {
337
    shift if ref($_[0]) eq 'Archive::Zip::Archive';
338
    my $chunkSize = (ref($_[0]) eq 'HASH') ? shift->{chunkSize} : shift;
339
    my $oldChunkSize = $Archive::Zip::ChunkSize;
340
    $Archive::Zip::ChunkSize = $chunkSize if ($chunkSize);
341
    return $oldChunkSize;
342
}
343
 
344
sub chunkSize {
345
    return $Archive::Zip::ChunkSize;
346
}
347
 
348
sub setErrorHandler {
349
    my $errorHandler = (ref($_[0]) eq 'HASH') ? shift->{subroutine} : shift;
350
    $errorHandler = \&Carp::carp unless defined($errorHandler);
351
    my $oldErrorHandler = $Archive::Zip::ErrorHandler;
352
    $Archive::Zip::ErrorHandler = $errorHandler;
353
    return $oldErrorHandler;
354
}
355
 
356
######################################################################
357
# Private utility functions (not methods).
358
 
359
sub _printError {
360
    my $string = join(' ', @_, "\n");
361
    my $oldCarpLevel = $Carp::CarpLevel;
362
    $Carp::CarpLevel += 2;
363
    &{$ErrorHandler}($string);
364
    $Carp::CarpLevel = $oldCarpLevel;
365
}
366
 
367
# This is called on format errors.
368
sub _formatError {
369
    shift if ref($_[0]);
370
    _printError('format error:', @_);
371
    return AZ_FORMAT_ERROR;
372
}
373
 
374
# This is called on IO errors.
375
sub _ioError {
376
    shift if ref($_[0]);
377
    _printError('IO error:', @_, ':', $!);
378
    return AZ_IO_ERROR;
379
}
380
 
381
# This is called on generic errors.
382
sub _error {
383
    shift if ref($_[0]);
384
    _printError('error:', @_);
385
    return AZ_ERROR;
386
}
387
 
388
# Called when a subclass should have implemented
389
# something but didn't
390
sub _subclassResponsibility {
391
    Carp::croak("subclass Responsibility\n");
392
}
393
 
394
# Try to set the given file handle or object into binary mode.
395
sub _binmode {
396
    my $fh = shift;
397
    return _CAN($fh, 'binmode') ? $fh->binmode() : binmode($fh);
398
}
399
 
400
# Attempt to guess whether file handle is seekable.
401
# Because of problems with Windows, this only returns true when
402
# the file handle is a real file.
403
sub _isSeekable {
404
    my $fh = shift;
405
    return 0 unless ref $fh;
406
    _ISA($fh, "IO::Scalar")    # IO::Scalar objects are brokenly-seekable
407
      and return 0;
408
    _ISA($fh, "IO::String")
409
      and return 1;
410
    if (_ISA($fh, "IO::Seekable")) {
411
 
412
        # Unfortunately, some things like FileHandle objects
413
        # return true for Seekable, but AREN'T!!!!!
414
        _ISA($fh, "FileHandle")
415
          and return 0;
416
        return 1;
417
    }
418
 
419
    # open my $fh, "+<", \$data;
420
    ref $fh eq "GLOB" && eval { seek $fh, 0, 1 } and return 1;
421
    _CAN($fh, "stat")
422
      and return -f $fh;
423
    return (_CAN($fh, "seek") and _CAN($fh, "tell")) ? 1 : 0;
424
}
425
 
426
# Print to the filehandle, while making sure the pesky Perl special global
427
# variables don't interfere.
428
sub _print {
429
    my ($self, $fh, @data) = @_;
430
 
431
    local $\;
432
 
433
    return $fh->print(@data);
434
}
435
 
436
# Return an opened IO::Handle
437
# my ( $status, fh ) = _newFileHandle( 'fileName', 'w' );
438
# Can take a filename, file handle, or ref to GLOB
439
# Or, if given something that is a ref but not an IO::Handle,
440
# passes back the same thing.
441
sub _newFileHandle {
442
    my $fd     = shift;
443
    my $status = 1;
444
    my $handle;
445
 
446
    if (ref($fd)) {
447
        if (_ISA($fd, 'IO::Scalar') or _ISA($fd, 'IO::String')) {
448
            $handle = $fd;
449
        } elsif (_ISA($fd, 'IO::Handle') or ref($fd) eq 'GLOB') {
450
            $handle = IO::File->new;
451
            $status = $handle->fdopen($fd, @_);
452
        } else {
453
            $handle = $fd;
454
        }
455
    } else {
456
        $handle = IO::File->new;
457
        $status = $handle->open($fd, @_);
458
    }
459
 
460
    return ($status, $handle);
461
}
462
 
463
# Returns next signature from given file handle, leaves
464
# file handle positioned afterwards.
465
# In list context, returns ($status, $signature)
466
# ( $status, $signature) = _readSignature( $fh, $fileName );
467
 
468
sub _readSignature {
469
    my $fh                = shift;
470
    my $fileName          = shift;
471
    my $expectedSignature = shift;    # optional
472
 
473
    my $signatureData;
474
    my $bytesRead = $fh->read($signatureData, SIGNATURE_LENGTH);
475
    if ($bytesRead != SIGNATURE_LENGTH) {
476
        return _ioError("reading header signature");
477
    }
478
    my $signature = unpack(SIGNATURE_FORMAT, $signatureData);
479
    my $status = AZ_OK;
480
 
481
    # compare with expected signature, if any, or any known signature.
482
    if (
483
        (defined($expectedSignature) && $signature != $expectedSignature)
484
        || (   !defined($expectedSignature)
485
            && $signature != CENTRAL_DIRECTORY_FILE_HEADER_SIGNATURE
486
            && $signature != LOCAL_FILE_HEADER_SIGNATURE
487
            && $signature != END_OF_CENTRAL_DIRECTORY_SIGNATURE
488
            && $signature != DATA_DESCRIPTOR_SIGNATURE
489
            && $signature != ZIP64_END_OF_CENTRAL_DIRECTORY_RECORD_SIGNATURE
490
            && $signature != ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_SIGNATURE
491
        )
492
      ) {
493
        my $errmsg = sprintf("bad signature: 0x%08x", $signature);
494
        if (_isSeekable($fh)) {
495
            $errmsg .= sprintf(" at offset %d", $fh->tell() - SIGNATURE_LENGTH);
496
        }
497
 
498
        $status = _formatError("$errmsg in file $fileName");
499
    }
500
 
501
    return ($status, $signature);
502
}
503
 
504
# Utility method to make and open a temp file.
505
# Will create $temp_dir if it does not exist.
506
# Returns file handle and name:
507
#
508
# my ($fh, $name) = Archive::Zip::tempFile();
509
# my ($fh, $name) = Archive::Zip::tempFile('mytempdir');
510
#
511
 
512
sub tempFile {
513
    my $dir = (ref($_[0]) eq 'HASH') ? shift->{tempDir} : shift;
514
    my ($fh, $filename) = File::Temp::tempfile(
515
        SUFFIX => '.zip',
516
        UNLINK => 1,
517
        $dir ? (DIR => $dir) : ());
518
    return (undef, undef) unless $fh;
519
    my ($status, $newfh) = _newFileHandle($fh, 'w+');
520
    $fh->close();
521
    return ($newfh, $filename);
522
}
523
 
524
# Return the normalized directory name as used in a zip file (path
525
# separators become slashes, etc.).
526
# Will translate internal slashes in path components (i.e. on Macs) to
527
# underscores.  Discards volume names.
528
# When $forceDir is set, returns paths with trailing slashes (or arrays
529
# with trailing blank members).
530
#
531
# If third argument is a reference, returns volume information there.
532
#
533
# input         output
534
# .             ('.')   '.'
535
# ./a           ('a')   a
536
# ./a/b         ('a','b')   a/b
537
# ./a/b/        ('a','b')   a/b
538
# a/b/          ('a','b')   a/b
539
# /a/b/         ('','a','b')    a/b
540
# c:\a\b\c.doc  ('','a','b','c.doc')    a/b/c.doc      # on Windows
541
# "i/o maps:whatever"   ('i_o maps', 'whatever')  "i_o maps/whatever"   # on Macs
542
sub _asZipDirName {
543
    my $name      = shift;
544
    my $forceDir  = shift;
545
    my $volReturn = shift;
546
    my ($volume, $directories, $file) =
547
      File::Spec->splitpath(File::Spec->canonpath($name), $forceDir);
548
    $$volReturn = $volume if (ref($volReturn));
549
    my @dirs = map { $_ =~ s{/}{_}g; $_ } File::Spec->splitdir($directories);
550
    if (@dirs > 0) { pop(@dirs) unless $dirs[-1] }    # remove empty component
551
    push(@dirs, defined($file) ? $file : '');
552
 
553
    #return wantarray ? @dirs : join ( '/', @dirs );
554
 
555
    my $normalised_path = join '/', @dirs;
556
 
557
    # Leading directory separators should not be stored in zip archives.
558
    # Example:
559
    #   C:\a\b\c\      a/b/c
560
    #   C:\a\b\c.txt   a/b/c.txt
561
    #   /a/b/c/        a/b/c
562
    #   /a/b/c.txt     a/b/c.txt
563
    $normalised_path =~ s{^/}{};    # remove leading separator
564
 
565
    return $normalised_path;
566
}
567
 
568
# Return an absolute local name for a zip name.
569
# Assume a directory if zip name has trailing slash.
570
# Takes an optional volume name in FS format (like 'a:').
571
#
572
sub _asLocalName {
573
    my $name   = shift;    # zip format
574
    my $volume = shift;
575
    $volume = '' unless defined($volume);    # local FS format
576
 
577
    my @paths = split(/\//, $name);
578
    my $filename = pop(@paths);
579
    $filename = '' unless defined($filename);
580
    my $localDirs = @paths ? File::Spec->catdir(@paths) : '';
581
    my $localName = File::Spec->catpath($volume, $localDirs, $filename);
582
    unless ($volume) {
583
        $localName = File::Spec->rel2abs($localName, Cwd::getcwd());
584
    }
585
    return $localName;
586
}
587
 
588
1;
589
 
590
__END__
591
 
592
=pod
593
 
594
=encoding utf8
595
 
596
=head1 NAME
597
 
598
Archive::Zip - Provide an interface to ZIP archive files.
599
 
600
=head1 SYNOPSIS
601
 
602
   # Create a Zip file
603
   use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
604
   my $zip = Archive::Zip->new();
605
 
606
   # Add a directory
607
   my $dir_member = $zip->addDirectory( 'dirname/' );
608
 
609
   # Add a file from a string with compression
610
   my $string_member = $zip->addString( 'This is a test', 'stringMember.txt' );
611
   $string_member->desiredCompressionMethod( COMPRESSION_DEFLATED );
612
 
613
   # Add a file from disk
614
   my $file_member = $zip->addFile( 'xyz.pl', 'AnotherName.pl' );
615
 
616
   # Save the Zip file
617
   unless ( $zip->writeToFileNamed('someZip.zip') == AZ_OK ) {
618
       die 'write error';
619
   }
620
 
621
   # Read a Zip file
622
   my $somezip = Archive::Zip->new();
623
   unless ( $somezip->read( 'someZip.zip' ) == AZ_OK ) {
624
       die 'read error';
625
   }
626
 
627
   # Change the compression type for a file in the Zip
628
   my $member = $somezip->memberNamed( 'stringMember.txt' );
629
   $member->desiredCompressionMethod( COMPRESSION_STORED );
630
   unless ( $zip->writeToFileNamed( 'someOtherZip.zip' ) == AZ_OK ) {
631
       die 'write error';
632
   }
633
 
634
=head1 DESCRIPTION
635
 
636
The Archive::Zip module allows a Perl program to create, manipulate, read,
637
and write Zip archive files.
638
 
639
Zip archives can be created, or you can read from existing zip files.
640
 
641
Once created, they can be written to files, streams, or strings. Members
642
can be added, removed, extracted, replaced, rearranged, and enumerated.
643
They can also be renamed or have their dates, comments, or other attributes
644
queried or modified. Their data can be compressed or uncompressed as needed.
645
 
646
Members can be created from members in existing Zip files, or from existing
647
directories, files, or strings.
648
 
649
This module uses the L<Compress::Raw::Zlib> library to read and write the
650
compressed streams inside the files.
651
 
652
One can use L<Archive::Zip::MemberRead> to read the zip file archive members
653
as if they were files.
654
 
655
=head2 File Naming
656
 
657
Regardless of what your local file system uses for file naming, names in a
658
Zip file are in Unix format (I<forward> slashes (/) separating directory
659
names, etc.).
660
 
661
C<Archive::Zip> tries to be consistent with file naming conventions, and will
662
translate back and forth between native and Zip file names.
663
 
664
However, it can't guess which format names are in. So two rules control what
665
kind of file name you must pass various routines:
666
 
667
=over 4
668
 
669
=item Names of files are in local format.
670
 
671
C<File::Spec> and C<File::Basename> are used for various file
672
operations. When you're referring to a file on your system, use its
673
file naming conventions.
674
 
675
=item Names of archive members are in Unix format.
676
 
677
This applies to every method that refers to an archive member, or
678
provides a name for new archive members. The C<extract()> methods
679
that can take one or two names will convert from local to zip names
680
if you call them with a single name.
681
 
682
=back
683
 
684
=head2 Archive::Zip Object Model
685
 
686
=head3 Overview
687
 
688
Archive::Zip::Archive objects are what you ordinarily deal with.
689
These maintain the structure of a zip file, without necessarily
690
holding data. When a zip is read from a disk file, the (possibly
691
compressed) data still lives in the file, not in memory. Archive
692
members hold information about the individual members, but not
693
(usually) the actual member data. When the zip is written to a
694
(different) file, the member data is compressed or copied as needed.
695
It is possible to make archive members whose data is held in a string
696
in memory, but this is not done when a zip file is read. Directory
697
members don't have any data.
698
 
699
=head2 Inheritance
700
 
701
  Exporter
702
   Archive::Zip                            Common base class, has defs.
703
       Archive::Zip::Archive               A Zip archive.
704
       Archive::Zip::Member                Abstract superclass for all members.
705
           Archive::Zip::StringMember      Member made from a string
706
           Archive::Zip::FileMember        Member made from an external file
707
               Archive::Zip::ZipFileMember Member that lives in a zip file
708
               Archive::Zip::NewFileMember Member whose data is in a file
709
           Archive::Zip::DirectoryMember   Member that is a directory
710
 
711
=head1 EXPORTS
712
 
713
=over 4
714
 
715
=item :CONSTANTS
716
 
717
Exports the following constants:
718
 
719
FA_MSDOS FA_UNIX GPBF_ENCRYPTED_MASK
720
GPBF_DEFLATING_COMPRESSION_MASK GPBF_HAS_DATA_DESCRIPTOR_MASK
721
COMPRESSION_STORED COMPRESSION_DEFLATED IFA_TEXT_FILE_MASK
722
IFA_TEXT_FILE IFA_BINARY_FILE COMPRESSION_LEVEL_NONE
723
COMPRESSION_LEVEL_DEFAULT COMPRESSION_LEVEL_FASTEST
724
COMPRESSION_LEVEL_BEST_COMPRESSION
725
 
726
=item :MISC_CONSTANTS
727
 
728
Exports the following constants (only necessary for extending the
729
module):
730
 
731
FA_AMIGA FA_VAX_VMS FA_VM_CMS FA_ATARI_ST FA_OS2_HPFS
732
FA_MACINTOSH FA_Z_SYSTEM FA_CPM FA_WINDOWS_NTFS
733
GPBF_IMPLODING_8K_SLIDING_DICTIONARY_MASK
734
GPBF_IMPLODING_3_SHANNON_FANO_TREES_MASK
735
GPBF_IS_COMPRESSED_PATCHED_DATA_MASK COMPRESSION_SHRUNK
736
DEFLATING_COMPRESSION_NORMAL DEFLATING_COMPRESSION_MAXIMUM
737
DEFLATING_COMPRESSION_FAST DEFLATING_COMPRESSION_SUPER_FAST
738
COMPRESSION_REDUCED_1 COMPRESSION_REDUCED_2 COMPRESSION_REDUCED_3
739
COMPRESSION_REDUCED_4 COMPRESSION_IMPLODED COMPRESSION_TOKENIZED
740
COMPRESSION_DEFLATED_ENHANCED
741
COMPRESSION_PKWARE_DATA_COMPRESSION_LIBRARY_IMPLODED
742
 
743
=item :ERROR_CODES
744
 
745
Explained below. Returned from most methods.
746
 
747
AZ_OK AZ_STREAM_END AZ_ERROR AZ_FORMAT_ERROR AZ_IO_ERROR
748
 
749
=back
750
 
751
=head1 ERROR CODES
752
 
753
Many of the methods in Archive::Zip return error codes. These are implemented
754
as inline subroutines, using the C<use constant> pragma. They can be imported
755
into your namespace using the C<:ERROR_CODES> tag:
756
 
757
  use Archive::Zip qw( :ERROR_CODES );
758
 
759
  ...
760
 
761
  unless ( $zip->read( 'myfile.zip' ) == AZ_OK ) {
762
      die "whoops!";
763
  }
764
 
765
=over 4
766
 
767
=item AZ_OK (0)
768
 
769
Everything is fine.
770
 
771
=item AZ_STREAM_END (1)
772
 
773
The read stream (or central directory) ended normally.
774
 
775
=item AZ_ERROR (2)
776
 
777
There was some generic kind of error.
778
 
779
=item AZ_FORMAT_ERROR (3)
780
 
781
There is a format error in a ZIP file being read.
782
 
783
=item AZ_IO_ERROR (4)
784
 
785
There was an IO error.
786
 
787
=back
788
 
789
=head2 Compression
790
 
791
Archive::Zip allows each member of a ZIP file to be compressed (using the
792
Deflate algorithm) or uncompressed.
793
 
794
Other compression algorithms that some versions of ZIP have been able to
795
produce are not supported. Each member has two compression methods: the
796
one it's stored as (this is always COMPRESSION_STORED for string and external
797
file members), and the one you desire for the member in the zip file.
798
 
799
These can be different, of course, so you can make a zip member that is not
800
compressed out of one that is, and vice versa.
801
 
802
You can inquire about the current compression and set the desired
803
compression method:
804
 
805
  my $member = $zip->memberNamed( 'xyz.txt' );
806
  $member->compressionMethod();    # return current compression
807
 
808
  # set to read uncompressed
809
  $member->desiredCompressionMethod( COMPRESSION_STORED );
810
 
811
  # set to read compressed
812
  $member->desiredCompressionMethod( COMPRESSION_DEFLATED );
813
 
814
There are two different compression methods:
815
 
816
=over 4
817
 
818
=item COMPRESSION_STORED
819
 
820
File is stored (no compression)
821
 
822
=item COMPRESSION_DEFLATED
823
 
824
File is Deflated
825
 
826
=back
827
 
828
=head2 Compression Levels
829
 
830
If a member's desiredCompressionMethod is COMPRESSION_DEFLATED, you
831
can choose different compression levels. This choice may affect the
832
speed of compression and decompression, as well as the size of the
833
compressed member data.
834
 
835
  $member->desiredCompressionLevel( 9 );
836
 
837
The levels given can be:
838
 
839
=over 4
840
 
841
=item * 0 or COMPRESSION_LEVEL_NONE
842
 
843
This is the same as saying
844
 
845
  $member->desiredCompressionMethod( COMPRESSION_STORED );
846
 
847
=item * 1 .. 9
848
 
849
1 gives the best speed and worst compression, and 9 gives the
850
best compression and worst speed.
851
 
852
=item * COMPRESSION_LEVEL_FASTEST
853
 
854
This is a synonym for level 1.
855
 
856
=item * COMPRESSION_LEVEL_BEST_COMPRESSION
857
 
858
This is a synonym for level 9.
859
 
860
=item * COMPRESSION_LEVEL_DEFAULT
861
 
862
This gives a good compromise between speed and compression,
863
and is currently equivalent to 6 (this is in the zlib code).
864
This is the level that will be used if not specified.
865
 
866
=back
867
 
868
=head1 Archive::Zip Methods
869
 
870
The Archive::Zip class (and its invisible subclass Archive::Zip::Archive)
871
implement generic zip file functionality. Creating a new Archive::Zip object
872
actually makes an Archive::Zip::Archive object, but you don't have to worry
873
about this unless you're subclassing.
874
 
875
=head2 Constructor
876
 
877
=over 4
878
 
879
=item new( [$fileName] )
880
 
881
=item new( { filename => $fileName } )
882
 
883
Make a new, empty zip archive.
884
 
885
    my $zip = Archive::Zip->new();
886
 
887
If an additional argument is passed, new() will call read()
888
to read the contents of an archive:
889
 
890
    my $zip = Archive::Zip->new( 'xyz.zip' );
891
 
892
If a filename argument is passed and the read fails for any
893
reason, new will return undef. For this reason, it may be
894
better to call read separately.
895
 
896
=back
897
 
898
=head2 Zip Archive Utility Methods
899
 
900
These Archive::Zip methods may be called as functions or as object
901
methods. Do not call them as class methods:
902
 
903
    $zip = Archive::Zip->new();
904
    $crc = Archive::Zip::computeCRC32( 'ghijkl' );    # OK
905
    $crc = $zip->computeCRC32( 'ghijkl' );            # also OK
906
    $crc = Archive::Zip->computeCRC32( 'ghijkl' );    # NOT OK
907
 
908
=over 4
909
 
910
=item Archive::Zip::computeCRC32( $string [, $crc] )
911
 
912
=item Archive::Zip::computeCRC32( { string => $string [, checksum => $crc ] } )
913
 
914
This is a utility function that uses the Compress::Raw::Zlib CRC
915
routine to compute a CRC-32. You can get the CRC of a string:
916
 
917
    $crc = Archive::Zip::computeCRC32( $string );
918
 
919
Or you can compute the running CRC:
920
 
921
    $crc = 0;
922
    $crc = Archive::Zip::computeCRC32( 'abcdef', $crc );
923
    $crc = Archive::Zip::computeCRC32( 'ghijkl', $crc );
924
 
925
=item Archive::Zip::setChunkSize( $number )
926
 
927
=item Archive::Zip::setChunkSize( { chunkSize => $number } )
928
 
929
Report or change chunk size used for reading and writing.
930
This can make big differences in dealing with large files.
931
Currently, this defaults to 32K. This also changes the chunk
932
size used for Compress::Raw::Zlib. You must call setChunkSize()
933
before reading or writing. This is not exportable, so you
934
must call it like:
935
 
936
    Archive::Zip::setChunkSize( 4096 );
937
 
938
or as a method on a zip (though this is a global setting).
939
Returns old chunk size.
940
 
941
=item Archive::Zip::chunkSize()
942
 
943
Returns the current chunk size:
944
 
945
    my $chunkSize = Archive::Zip::chunkSize();
946
 
947
=item Archive::Zip::setErrorHandler( \&subroutine )
948
 
949
=item Archive::Zip::setErrorHandler( { subroutine => \&subroutine } )
950
 
951
Change the subroutine called with error strings. This
952
defaults to \&Carp::carp, but you may want to change it to
953
get the error strings. This is not exportable, so you must
954
call it like:
955
 
956
    Archive::Zip::setErrorHandler( \&myErrorHandler );
957
 
958
If myErrorHandler is undef, resets handler to default.
959
Returns old error handler. Note that if you call Carp::carp
960
or a similar routine or if you're chaining to the default
961
error handler from your error handler, you may want to
962
increment the number of caller levels that are skipped (do
963
not just set it to a number):
964
 
965
    $Carp::CarpLevel++;
966
 
967
=item Archive::Zip::tempFile( [ $tmpdir ] )
968
 
969
=item Archive::Zip::tempFile( { tempDir => $tmpdir } )
970
 
971
Create a uniquely named temp file. It will be returned open
972
for read/write. If C<$tmpdir> is given, it is used as the
973
name of a directory to create the file in. If not given,
974
creates the file using C<File::Spec::tmpdir()>. Generally, you can
975
override this choice using the
976
 
977
    $ENV{TMPDIR}
978
 
979
environment variable. But see the L<File::Spec|File::Spec>
980
documentation for your system. Note that on many systems, if you're
981
running in taint mode, then you must make sure that C<$ENV{TMPDIR}> is
982
untainted for it to be used.
983
Will I<NOT> create C<$tmpdir> if it does not exist (this is a change
984
from prior versions!). Returns file handle and name:
985
 
986
    my ($fh, $name) = Archive::Zip::tempFile();
987
    my ($fh, $name) = Archive::Zip::tempFile('myTempDir');
988
    my $fh = Archive::Zip::tempFile();  # if you don't need the name
989
 
990
=back
991
 
992
=head2 Zip Archive Accessors
993
 
994
=over 4
995
 
996
=item members()
997
 
998
Return a copy of the members array
999
 
1000
    my @members = $zip->members();
1001
 
1002
=item numberOfMembers()
1003
 
1004
Return the number of members I have
1005
 
1006
=item memberNames()
1007
 
1008
Return a list of the (internal) file names of the zip members
1009
 
1010
=item memberNamed( $string )
1011
 
1012
=item memberNamed( { zipName => $string } )
1013
 
1014
Return ref to member whose filename equals given filename or
1015
undef. C<$string> must be in Zip (Unix) filename format.
1016
 
1017
=item membersMatching( $regex )
1018
 
1019
=item membersMatching( { regex => $regex } )
1020
 
1021
Return array of members whose filenames match given regular
1022
expression in list context. Returns number of matching
1023
members in scalar context.
1024
 
1025
    my @textFileMembers = $zip->membersMatching( '.*\.txt' );
1026
    # or
1027
    my $numberOfTextFiles = $zip->membersMatching( '.*\.txt' );
1028
 
1029
=item diskNumber()
1030
 
1031
Return the disk that I start on. Not used for writing zips,
1032
but might be interesting if you read a zip in. This should be
1033
0, as Archive::Zip does not handle multi-volume archives.
1034
 
1035
=item diskNumberWithStartOfCentralDirectory()
1036
 
1037
Return the disk number that holds the beginning of the
1038
central directory. Not used for writing zips, but might be
1039
interesting if you read a zip in. This should be 0, as
1040
Archive::Zip does not handle multi-volume archives.
1041
 
1042
=item numberOfCentralDirectoriesOnThisDisk()
1043
 
1044
Return the number of CD structures in the zipfile last read in.
1045
Not used for writing zips, but might be interesting if you read a zip
1046
in.
1047
 
1048
=item numberOfCentralDirectories()
1049
 
1050
Return the number of CD structures in the zipfile last read in.
1051
Not used for writing zips, but might be interesting if you read a zip
1052
in.
1053
 
1054
=item centralDirectorySize()
1055
 
1056
Returns central directory size, as read from an external zip
1057
file. Not used for writing zips, but might be interesting if
1058
you read a zip in.
1059
 
1060
=item centralDirectoryOffsetWRTStartingDiskNumber()
1061
 
1062
Returns the offset into the zip file where the CD begins. Not
1063
used for writing zips, but might be interesting if you read a
1064
zip in.
1065
 
1066
=item zipfileComment( [ $string ] )
1067
 
1068
=item zipfileComment( [ { comment => $string } ] )
1069
 
1070
Get or set the zipfile comment. Returns the old comment.
1071
 
1072
    print $zip->zipfileComment();
1073
    $zip->zipfileComment( 'New Comment' );
1074
 
1075
=item eocdOffset()
1076
 
1077
Returns the (unexpected) number of bytes between where the
1078
EOCD was found and where it expected to be. This is normally
1079
0, but would be positive if something (a virus, perhaps) had
1080
added bytes somewhere before the EOCD. Not used for writing
1081
zips, but might be interesting if you read a zip in. Here is
1082
an example of how you can diagnose this:
1083
 
1084
  my $zip = Archive::Zip->new('somefile.zip');
1085
  if ($zip->eocdOffset())
1086
  {
1087
    warn "A virus has added ", $zip->eocdOffset, " bytes of garbage\n";
1088
  }
1089
 
1090
The C<eocdOffset()> is used to adjust the starting position of member
1091
headers, if necessary.
1092
 
1093
=item fileName()
1094
 
1095
Returns the name of the file last read from. If nothing has
1096
been read yet, returns an empty string; if read from a file
1097
handle, returns the handle in string form.
1098
 
1099
=back
1100
 
1101
=head2 Zip Archive Member Operations
1102
 
1103
Various operations on a zip file modify members. When a member is
1104
passed as an argument, you can either use a reference to the member
1105
itself, or the name of a member. Of course, using the name requires
1106
that names be unique within a zip (this is not enforced).
1107
 
1108
=over 4
1109
 
1110
=item removeMember( $memberOrName )
1111
 
1112
=item removeMember( { memberOrZipName => $memberOrName } )
1113
 
1114
Remove and return the given member, or match its name and
1115
remove it. Returns undef if member or name does not exist in this
1116
Zip. No-op if member does not belong to this zip.
1117
 
1118
=item replaceMember( $memberOrName, $newMember )
1119
 
1120
=item replaceMember( { memberOrZipName => $memberOrName,
1121
    newMember => $newMember } )
1122
 
1123
Remove and return the given member, or match its name and
1124
remove it. Replace with new member. Returns undef if member or
1125
name does not exist in this Zip, or if C<$newMember> is undefined.
1126
 
1127
It is an (undiagnosed) error to provide a C<$newMember> that is a
1128
member of the zip being modified.
1129
 
1130
    my $member1 = $zip->removeMember( 'xyz' );
1131
    my $member2 = $zip->replaceMember( 'abc', $member1 );
1132
    # now, $member2 (named 'abc') is not in $zip,
1133
    # and $member1 (named 'xyz') is, having taken $member2's place.
1134
 
1135
=item extractMember( $memberOrName [, $extractedName ] )
1136
 
1137
=item extractMember( { memberOrZipName => $memberOrName
1138
    [, name => $extractedName ] } )
1139
 
1140
Extract the given member, or match its name and extract it.
1141
Returns undef if member does not exist in this Zip. If
1142
optional second arg is given, use it as the name of the
1143
extracted member. Otherwise, the internal filename of the
1144
member is used as the name of the extracted file or
1145
directory.
1146
If you pass C<$extractedName>, it should be in the local file
1147
system's format.
1148
All necessary directories will be created. Returns C<AZ_OK>
1149
on success.
1150
 
1151
=item extractMemberWithoutPaths( $memberOrName [, $extractedName ] )
1152
 
1153
=item extractMemberWithoutPaths( { memberOrZipName => $memberOrName
1154
    [, name => $extractedName ] } )
1155
 
1156
Extract the given member, or match its name and extract it.
1157
Does not use path information (extracts into the current
1158
directory). Returns undef if member does not exist in this
1159
Zip.
1160
If optional second arg is given, use it as the name of the
1161
extracted member (its paths will be deleted too). Otherwise,
1162
the internal filename of the member (minus paths) is used as
1163
the name of the extracted file or directory. Returns C<AZ_OK>
1164
on success.
1165
 
1166
=item addMember( $member )
1167
 
1168
=item addMember( { member => $member } )
1169
 
1170
Append a member (possibly from another zip file) to the zip
1171
file. Returns the new member. Generally, you will use
1172
addFile(), addDirectory(), addFileOrDirectory(), addString(),
1173
or read() to add members.
1174
 
1175
    # Move member named 'abc' to end of zip:
1176
    my $member = $zip->removeMember( 'abc' );
1177
    $zip->addMember( $member );
1178
 
1179
=item updateMember( $memberOrName, $fileName )
1180
 
1181
=item updateMember( { memberOrZipName => $memberOrName, name => $fileName } )
1182
 
1183
Update a single member from the file or directory named C<$fileName>.
1184
Returns the (possibly added or updated) member, if any; C<undef> on
1185
errors.
1186
The comparison is based on C<lastModTime()> and (in the case of a
1187
non-directory) the size of the file.
1188
 
1189
=item addFile( $fileName [, $newName, $compressionLevel ] )
1190
 
1191
=item addFile( { filename => $fileName
1192
    [, zipName => $newName, compressionLevel => $compressionLevel } ] )
1193
 
1194
Append a member whose data comes from an external file,
1195
returning the member or undef. The member will have its file
1196
name set to the name of the external file, and its
1197
desiredCompressionMethod set to COMPRESSION_DEFLATED. The
1198
file attributes and last modification time will be set from
1199
the file.
1200
If the name given does not represent a readable plain file or
1201
symbolic link, undef will be returned. C<$fileName> must be
1202
in the format required for the local file system.
1203
The optional C<$newName> argument sets the internal file name
1204
to something different than the given $fileName. C<$newName>,
1205
if given, must be in Zip name format (i.e. Unix).
1206
The text mode bit will be set if the contents appears to be
1207
text (as returned by the C<-T> perl operator).
1208
 
1209
 
1210
I<NOTE> that you should not (generally) use absolute path names
1211
in zip member names, as this will cause problems with some zip
1212
tools as well as introduce a security hole and make the zip
1213
harder to use.
1214
 
1215
=item addDirectory( $directoryName [, $fileName ] )
1216
 
1217
=item addDirectory( { directoryName => $directoryName
1218
    [, zipName => $fileName ] } )
1219
 
1220
 
1221
Append a member created from the given directory name. The
1222
directory name does not have to name an existing directory.
1223
If the named directory exists, the file modification time and
1224
permissions are set from the existing directory, otherwise
1225
they are set to now and permissive default permissions.
1226
C<$directoryName> must be in local file system format.
1227
The optional second argument sets the name of the archive
1228
member (which defaults to C<$directoryName>). If given, it
1229
must be in Zip (Unix) format.
1230
Returns the new member.
1231
 
1232
=item addFileOrDirectory( $name [, $newName, $compressionLevel ] )
1233
 
1234
=item addFileOrDirectory( { name => $name [, zipName => $newName,
1235
    compressionLevel => $compressionLevel ] } )
1236
 
1237
 
1238
Append a member from the file or directory named $name. If
1239
$newName is given, use it for the name of the new member.
1240
Will add or remove trailing slashes from $newName as needed.
1241
C<$name> must be in local file system format.
1242
The optional second argument sets the name of the archive
1243
member (which defaults to C<$name>). If given, it must be in
1244
Zip (Unix) format.
1245
 
1246
=item addString( $stringOrStringRef, $name, [$compressionLevel] )
1247
 
1248
=item addString( { string => $stringOrStringRef [, zipName => $name,
1249
    compressionLevel => $compressionLevel ] } )
1250
 
1251
Append a member created from the given string or string
1252
reference. The name is given by the second argument.
1253
Returns the new member. The last modification time will be
1254
set to now, and the file attributes will be set to permissive
1255
defaults.
1256
 
1257
    my $member = $zip->addString( 'This is a test', 'test.txt' );
1258
 
1259
=item contents( $memberOrMemberName [, $newContents ] )
1260
 
1261
=item contents( { memberOrZipName => $memberOrMemberName
1262
    [, contents => $newContents ] } )
1263
 
1264
 
1265
Returns the uncompressed data for a particular member, or
1266
undef.
1267
 
1268
    print "xyz.txt contains " . $zip->contents( 'xyz.txt' );
1269
 
1270
Also can change the contents of a member:
1271
 
1272
    $zip->contents( 'xyz.txt', 'This is the new contents' );
1273
 
1274
If called expecting an array as the return value, it will include
1275
the status as the second value in the array.
1276
 
1277
    ($content, $status) = $zip->contents( 'xyz.txt');
1278
 
1279
=back
1280
 
1281
=head2 Zip Archive I/O operations
1282
 
1283
 
1284
A Zip archive can be written to a file or file handle, or read from
1285
one.
1286
 
1287
=over 4
1288
 
1289
=item writeToFileNamed( $fileName )
1290
 
1291
=item writeToFileNamed( { fileName => $fileName } )
1292
 
1293
Write a zip archive to named file. Returns C<AZ_OK> on
1294
success.
1295
 
1296
    my $status = $zip->writeToFileNamed( 'xx.zip' );
1297
    die "error somewhere" if $status != AZ_OK;
1298
 
1299
Note that if you use the same name as an existing zip file
1300
that you read in, you will clobber ZipFileMembers. So
1301
instead, write to a different file name, then delete the
1302
original.
1303
If you use the C<overwrite()> or C<overwriteAs()> methods, you can
1304
re-write the original zip in this way.
1305
C<$fileName> should be a valid file name on your system.
1306
 
1307
=item writeToFileHandle( $fileHandle [, $seekable] )
1308
 
1309
Write a zip archive to a file handle. Return AZ_OK on
1310
success. The optional second arg tells whether or not to try
1311
to seek backwards to re-write headers. If not provided, it is
1312
set if the Perl C<-f> test returns true. This could fail on
1313
some operating systems, though.
1314
 
1315
    my $fh = IO::File->new( 'someFile.zip', 'w' );
1316
    unless ( $zip->writeToFileHandle( $fh ) == AZ_OK ) {
1317
        # error handling
1318
    }
1319
 
1320
If you pass a file handle that is not seekable (like if
1321
you're writing to a pipe or a socket), pass a false second
1322
argument:
1323
 
1324
    my $fh = IO::File->new( '| cat > somefile.zip', 'w' );
1325
    $zip->writeToFileHandle( $fh, 0 );   # fh is not seekable
1326
 
1327
If this method fails during the write of a member, that
1328
member and all following it will return false from
1329
C<wasWritten()>. See writeCentralDirectory() for a way to
1330
deal with this.
1331
If you want, you can write data to the file handle before
1332
passing it to writeToFileHandle(); this could be used (for
1333
instance) for making self-extracting archives. However, this
1334
only works reliably when writing to a real file (as opposed
1335
to STDOUT or some other possible non-file).
1336
 
1337
See examples/selfex.pl for how to write a self-extracting
1338
archive.
1339
 
1340
=item writeCentralDirectory( $fileHandle [, $offset ] )
1341
 
1342
=item writeCentralDirectory( { fileHandle => $fileHandle
1343
    [, offset => $offset ] } )
1344
 
1345
Writes the central directory structure to the given file
1346
handle.
1347
 
1348
Returns AZ_OK on success. If given an $offset, will
1349
seek to that point before writing. This can be used for
1350
recovery in cases where writeToFileHandle or writeToFileNamed
1351
returns an IO error because of running out of space on the
1352
destination file.
1353
 
1354
You can truncate the zip by seeking backwards and then writing the
1355
directory:
1356
 
1357
    my $fh = IO::File->new( 'someFile.zip', 'w' );
1358
        my $retval = $zip->writeToFileHandle( $fh );
1359
    if ( $retval == AZ_IO_ERROR ) {
1360
        my @unwritten = grep { not $_->wasWritten() } $zip->members();
1361
        if (@unwritten) {
1362
            $zip->removeMember( $member ) foreach my $member ( @unwritten );
1363
            $zip->writeCentralDirectory( $fh,
1364
            $unwritten[0]->writeLocalHeaderRelativeOffset());
1365
        }
1366
    }
1367
 
1368
=item overwriteAs( $newName )
1369
 
1370
=item overwriteAs( { filename => $newName } )
1371
 
1372
Write the zip to the specified file, as safely as possible.
1373
This is done by first writing to a temp file, then renaming
1374
the original if it exists, then renaming the temp file, then
1375
deleting the renamed original if it exists. Returns AZ_OK if
1376
successful.
1377
 
1378
=item overwrite()
1379
 
1380
Write back to the original zip file. See overwriteAs() above.
1381
If the zip was not ever read from a file, this generates an
1382
error.
1383
 
1384
=item read( $fileName )
1385
 
1386
=item read( { filename => $fileName } )
1387
 
1388
Read zipfile headers from a zip file, appending new members.
1389
Returns C<AZ_OK> or error code.
1390
 
1391
    my $zipFile = Archive::Zip->new();
1392
    my $status = $zipFile->read( '/some/FileName.zip' );
1393
 
1394
=item readFromFileHandle( $fileHandle, $filename )
1395
 
1396
=item readFromFileHandle( { fileHandle => $fileHandle, filename => $filename } )
1397
 
1398
Read zipfile headers from an already-opened file handle,
1399
appending new members. Does not close the file handle.
1400
Returns C<AZ_OK> or error code. Note that this requires a
1401
seekable file handle; reading from a stream is not yet
1402
supported, but using in-memory data is.
1403
 
1404
    my $fh = IO::File->new( '/some/FileName.zip', 'r' );
1405
    my $zip1 = Archive::Zip->new();
1406
    my $status = $zip1->readFromFileHandle( $fh );
1407
    my $zip2 = Archive::Zip->new();
1408
    $status = $zip2->readFromFileHandle( $fh );
1409
 
1410
Read zip using in-memory data (recursable):
1411
 
1412
    open my $fh, "<", "archive.zip" or die $!;
1413
    my $zip_data = do { local $.; <$fh> };
1414
    my $zip = Archive::Zip->new;
1415
    open my $dh, "+<", \$zip_data;
1416
    $zip->readFromFileHandle ($dh);
1417
 
1418
=back
1419
 
1420
=head2 Zip Archive Tree operations
1421
 
1422
These used to be in Archive::Zip::Tree but got moved into
1423
Archive::Zip. They enable operation on an entire tree of members or
1424
files.
1425
A usage example:
1426
 
1427
  use Archive::Zip;
1428
  my $zip = Archive::Zip->new();
1429
 
1430
  # add all readable files and directories below . as xyz/*
1431
  $zip->addTree( '.', 'xyz' );
1432
 
1433
  # add all readable plain files below /abc as def/*
1434
  $zip->addTree( '/abc', 'def', sub { -f && -r } );
1435
 
1436
  # add all .c files below /tmp as stuff/*
1437
  $zip->addTreeMatching( '/tmp', 'stuff', '\.c$' );
1438
 
1439
  # add all .o files below /tmp as stuff/* if they aren't writable
1440
  $zip->addTreeMatching( '/tmp', 'stuff', '\.o$', sub { ! -w } );
1441
 
1442
  # add all .so files below /tmp that are smaller than 200 bytes as stuff/*
1443
  $zip->addTreeMatching( '/tmp', 'stuff', '\.o$', sub { -s < 200 } );
1444
 
1445
  # and write them into a file
1446
  $zip->writeToFileNamed('xxx.zip');
1447
 
1448
  # now extract the same files into /tmpx
1449
  $zip->extractTree( 'stuff', '/tmpx' );
1450
 
1451
=over 4
1452
 
1453
=item $zip->addTree( $root, $dest [, $pred, $compressionLevel ] ) -- Add tree of files to a zip
1454
 
1455
=item $zip->addTree( { root => $root, zipName => $dest [, select => $pred,
1456
    compressionLevel => $compressionLevel ] )
1457
 
1458
C<$root> is the root of the tree of files and directories to be
1459
added. It is a valid directory name on your system. C<$dest> is
1460
the name for the root in the zip file (undef or blank means
1461
to use relative pathnames). It is a valid ZIP directory name
1462
(that is, it uses forward slashes (/) for separating
1463
directory components). C<$pred> is an optional subroutine
1464
reference to select files: it is passed the name of the
1465
prospective file or directory using C<$_>, and if it returns
1466
true, the file or directory will be included. The default is
1467
to add all readable files and directories. For instance,
1468
using
1469
 
1470
  my $pred = sub { /\.txt/ };
1471
  $zip->addTree( '.', '', $pred );
1472
 
1473
will add all the .txt files in and below the current
1474
directory, using relative names, and making the names
1475
identical in the zipfile:
1476
 
1477
  original name           zip member name
1478
  ./xyz                   xyz
1479
  ./a/                    a/
1480
  ./a/b                   a/b
1481
 
1482
To translate absolute to relative pathnames, just pass them
1483
in: $zip->addTree( '/c/d', 'a' );
1484
 
1485
  original name           zip member name
1486
  /c/d/xyz                a/xyz
1487
  /c/d/a/                 a/a/
1488
  /c/d/a/b                a/a/b
1489
 
1490
Returns AZ_OK on success. Note that this will not follow
1491
symbolic links to directories. Note also that this does not
1492
check for the validity of filenames.
1493
 
1494
Note that you generally I<don't> want to make zip archive member names
1495
absolute.
1496
 
1497
=item $zip->addTreeMatching( $root, $dest, $pattern [, $pred, $compressionLevel ] )
1498
 
1499
=item $zip->addTreeMatching( { root => $root, zipName => $dest, pattern =>
1500
    $pattern [, select => $pred, compressionLevel => $compressionLevel ] } )
1501
 
1502
$root is the root of the tree of files and directories to be
1503
added $dest is the name for the root in the zip file (undef
1504
means to use relative pathnames) $pattern is a (non-anchored)
1505
regular expression for filenames to match $pred is an
1506
optional subroutine reference to select files: it is passed
1507
the name of the prospective file or directory in C<$_>, and
1508
if it returns true, the file or directory will be included.
1509
The default is to add all readable files and directories. To
1510
add all files in and below the current directory whose names
1511
end in C<.pl>, and make them extract into a subdirectory
1512
named C<xyz>, do this:
1513
 
1514
  $zip->addTreeMatching( '.', 'xyz', '\.pl$' )
1515
 
1516
To add all I<writable> files in and below the directory named
1517
C</abc> whose names end in C<.pl>, and make them extract into
1518
a subdirectory named C<xyz>, do this:
1519
 
1520
  $zip->addTreeMatching( '/abc', 'xyz', '\.pl$', sub { -w } )
1521
 
1522
Returns AZ_OK on success. Note that this will not follow
1523
symbolic links to directories.
1524
 
1525
=item $zip->updateTree( $root [, $dest , $pred , $mirror, $compressionLevel ] );
1526
 
1527
=item $zip->updateTree( { root => $root [, zipName => $dest, select => $pred,
1528
    mirror => $mirror, compressionLevel => $compressionLevel ] } );
1529
 
1530
Update a zip file from a directory tree.
1531
 
1532
C<updateTree()> takes the same arguments as C<addTree()>, but first
1533
checks to see whether the file or directory already exists in the zip
1534
file, and whether it has been changed.
1535
 
1536
If the fourth argument C<$mirror> is true, then delete all my members
1537
if corresponding files were not found.
1538
 
1539
Returns an error code or AZ_OK if all is well.
1540
 
1541
=item $zip->extractTree( [ $root, $dest, $volume } ] )
1542
 
1543
=item $zip->extractTree( [ { root => $root, zipName => $dest, volume => $volume } ] )
1544
 
1545
If you don't give any arguments at all, will extract all the
1546
files in the zip with their original names.
1547
 
1548
If you supply one argument for C<$root>, C<extractTree> will extract
1549
all the members whose names start with C<$root> into the current
1550
directory, stripping off C<$root> first.
1551
C<$root> is in Zip (Unix) format.
1552
For instance,
1553
 
1554
  $zip->extractTree( 'a' );
1555
 
1556
when applied to a zip containing the files:
1557
a/x a/b/c ax/d/e d/e will extract:
1558
 
1559
a/x as ./x
1560
 
1561
a/b/c as ./b/c
1562
 
1563
If you give two arguments, C<extractTree> extracts all the members
1564
whose names start with C<$root>. It will translate C<$root> into
1565
C<$dest> to construct the destination file name.
1566
C<$root> and C<$dest> are in Zip (Unix) format.
1567
For instance,
1568
 
1569
   $zip->extractTree( 'a', 'd/e' );
1570
 
1571
when applied to a zip containing the files:
1572
a/x a/b/c ax/d/e d/e will extract:
1573
 
1574
a/x to d/e/x
1575
 
1576
a/b/c to d/e/b/c and ignore ax/d/e and d/e
1577
 
1578
If you give three arguments, C<extractTree> extracts all the members
1579
whose names start with C<$root>. It will translate C<$root> into
1580
C<$dest> to construct the destination file name, and then it will
1581
convert to local file system format, using C<$volume> as the name of
1582
the destination volume.
1583
 
1584
C<$root> and C<$dest> are in Zip (Unix) format.
1585
 
1586
C<$volume> is in local file system format.
1587
 
1588
For instance, under Windows,
1589
 
1590
   $zip->extractTree( 'a', 'd/e', 'f:' );
1591
 
1592
when applied to a zip containing the files:
1593
a/x a/b/c ax/d/e d/e will extract:
1594
 
1595
a/x to f:d/e/x
1596
 
1597
a/b/c to f:d/e/b/c and ignore ax/d/e and d/e
1598
 
1599
If you want absolute paths (the prior example used paths relative to
1600
the current directory on the destination volume, you can specify these
1601
in C<$dest>:
1602
 
1603
   $zip->extractTree( 'a', '/d/e', 'f:' );
1604
 
1605
when applied to a zip containing the files:
1606
a/x a/b/c ax/d/e d/e will extract:
1607
 
1608
a/x to f:\d\e\x
1609
 
1610
a/b/c to f:\d\e\b\c and ignore ax/d/e and d/e
1611
 
1612
Returns an error code or AZ_OK if everything worked OK.
1613
 
1614
=back
1615
 
1616
=head1 Archive::Zip Global Variables
1617
 
1618
=over 4
1619
 
1620
=item $Archive::Zip::UNICODE
1621
 
1622
This variable governs how Unicode file and directory names are added
1623
to or extracted from an archive. If set, file and directory names are considered
1624
to be UTF-8 encoded. This is I<EXPERIMENTAL AND BUGGY (there are some edge cases
1625
on Win32)>. Please report problems.
1626
 
1627
    {
1628
        local $Archive::Zip::UNICODE = 1;
1629
        $zip->addFile('Déjà vu.txt');
1630
    }
1631
 
1632
=back
1633
 
1634
=head1 MEMBER OPERATIONS
1635
 
1636
=head2 Member Class Methods
1637
 
1638
Several constructors allow you to construct members without adding
1639
them to a zip archive. These work the same as the addFile(),
1640
addDirectory(), and addString() zip instance methods described above,
1641
but they don't add the new members to a zip.
1642
 
1643
=over 4
1644
 
1645
=item Archive::Zip::Member->newFromString( $stringOrStringRef [, $fileName ] )
1646
 
1647
=item Archive::Zip::Member->newFromString( { string => $stringOrStringRef
1648
    [, zipName => $fileName ] )
1649
 
1650
Construct a new member from the given string. Returns undef
1651
on error.
1652
 
1653
    my $member = Archive::Zip::Member->newFromString( 'This is a test',
1654
 
1655
=item newFromFile( $fileName [, $zipName ] )
1656
 
1657
=item newFromFile( { filename => $fileName [, zipName => $zipName ] } )
1658
 
1659
Construct a new member from the given file. Returns undef on
1660
error.
1661
 
1662
    my $member = Archive::Zip::Member->newFromFile( 'xyz.txt' );
1663
 
1664
=item newDirectoryNamed( $directoryName [, $zipname ] )
1665
 
1666
=item newDirectoryNamed( { directoryName => $directoryName
1667
    [, zipName => $zipname ] } )
1668
 
1669
Construct a new member from the given directory.
1670
C<$directoryName> must be a valid name on your file system; it does not
1671
have to exist.
1672
 
1673
If given, C<$zipname> will be the name of the zip member; it must be a
1674
valid Zip (Unix) name. If not given, it will be converted from
1675
C<$directoryName>.
1676
 
1677
Returns undef on error.
1678
 
1679
    my $member = Archive::Zip::Member->newDirectoryNamed( 'CVS/' );
1680
 
1681
=back
1682
 
1683
=head2 Member Simple accessors
1684
 
1685
These methods get (and/or set) member attribute values.
1686
 
1687
=over 4
1688
 
1689
=item versionMadeBy()
1690
 
1691
Gets the field from the member header.
1692
 
1693
=item fileAttributeFormat( [ $format ] )
1694
 
1695
=item fileAttributeFormat( [ { format => $format ] } )
1696
 
1697
Gets or sets the field from the member header. These are
1698
C<FA_*> values.
1699
 
1700
=item versionNeededToExtract()
1701
 
1702
Gets the field from the member header.
1703
 
1704
=item bitFlag()
1705
 
1706
Gets the general purpose bit field from the member header.
1707
This is where the C<GPBF_*> bits live.
1708
 
1709
=item compressionMethod()
1710
 
1711
Returns the member compression method. This is the method
1712
that is currently being used to compress the member data.
1713
This will be COMPRESSION_STORED for added string or file
1714
members, or any of the C<COMPRESSION_*> values for members
1715
from a zip file. However, this module can only handle members
1716
whose data is in COMPRESSION_STORED or COMPRESSION_DEFLATED
1717
format.
1718
 
1719
=item desiredCompressionMethod( [ $method ] )
1720
 
1721
=item desiredCompressionMethod( [ { compressionMethod => $method } ] )
1722
 
1723
Get or set the member's C<desiredCompressionMethod>. This is
1724
the compression method that will be used when the member is
1725
written. Returns prior desiredCompressionMethod. Only
1726
COMPRESSION_DEFLATED or COMPRESSION_STORED are valid
1727
arguments. Changing to COMPRESSION_STORED will change the
1728
member desiredCompressionLevel to 0; changing to
1729
COMPRESSION_DEFLATED will change the member
1730
desiredCompressionLevel to COMPRESSION_LEVEL_DEFAULT.
1731
 
1732
=item desiredCompressionLevel( [ $level ] )
1733
 
1734
=item desiredCompressionLevel( [ { compressionLevel => $level } ] )
1735
 
1736
Get or set the member's desiredCompressionLevel This is the
1737
method that will be used to write. Returns prior
1738
desiredCompressionLevel. Valid arguments are 0 through 9,
1739
COMPRESSION_LEVEL_NONE, COMPRESSION_LEVEL_DEFAULT,
1740
COMPRESSION_LEVEL_BEST_COMPRESSION, and
1741
COMPRESSION_LEVEL_FASTEST. 0 or COMPRESSION_LEVEL_NONE will
1742
change the desiredCompressionMethod to COMPRESSION_STORED.
1743
All other arguments will change the desiredCompressionMethod
1744
to COMPRESSION_DEFLATED.
1745
 
1746
=item externalFileName()
1747
 
1748
Return the member's external file name, if any, or undef.
1749
 
1750
=item fileName()
1751
 
1752
Get or set the member's internal filename. Returns the
1753
(possibly new) filename. Names will have backslashes
1754
converted to forward slashes, and will have multiple
1755
consecutive slashes converted to single ones.
1756
 
1757
=item lastModFileDateTime()
1758
 
1759
Return the member's last modification date/time stamp in
1760
MS-DOS format.
1761
 
1762
=item lastModTime()
1763
 
1764
Return the member's last modification date/time stamp,
1765
converted to unix localtime format.
1766
 
1767
    print "Mod Time: " . scalar( localtime( $member->lastModTime() ) );
1768
 
1769
=item setLastModFileDateTimeFromUnix()
1770
 
1771
Set the member's lastModFileDateTime from the given unix
1772
time.
1773
 
1774
    $member->setLastModFileDateTimeFromUnix( time() );
1775
 
1776
=item internalFileAttributes()
1777
 
1778
Return the internal file attributes field from the zip
1779
header. This is only set for members read from a zip file.
1780
 
1781
=item externalFileAttributes()
1782
 
1783
Return member attributes as read from the ZIP file. Note that
1784
these are NOT UNIX!
1785
 
1786
=item unixFileAttributes( [ $newAttributes ] )
1787
 
1788
=item unixFileAttributes( [ { attributes => $newAttributes } ] )
1789
 
1790
Get or set the member's file attributes using UNIX file
1791
attributes. Returns old attributes.
1792
 
1793
    my $oldAttribs = $member->unixFileAttributes( 0666 );
1794
 
1795
Note that the return value has more than just the file
1796
permissions, so you will have to mask off the lowest bits for
1797
comparisons.
1798
 
1799
=item localExtraField( [ $newField ] )
1800
 
1801
=item localExtraField( [ { field => $newField } ] )
1802
 
1803
Gets or sets the extra field that was read from the local
1804
header. This is not set for a member from a zip file until
1805
after the member has been written out. The extra field must
1806
be in the proper format.
1807
 
1808
=item cdExtraField( [ $newField ] )
1809
 
1810
=item cdExtraField( [ { field => $newField } ] )
1811
 
1812
Gets or sets the extra field that was read from the central
1813
directory header. The extra field must be in the proper
1814
format.
1815
 
1816
=item extraFields()
1817
 
1818
Return both local and CD extra fields, concatenated.
1819
 
1820
=item fileComment( [ $newComment ] )
1821
 
1822
=item fileComment( [ { comment => $newComment } ] )
1823
 
1824
Get or set the member's file comment.
1825
 
1826
=item hasDataDescriptor()
1827
 
1828
Get or set the data descriptor flag. If this is set, the
1829
local header will not necessarily have the correct data
1830
sizes. Instead, a small structure will be stored at the end
1831
of the member data with these values. This should be
1832
transparent in normal operation.
1833
 
1834
=item crc32()
1835
 
1836
Return the CRC-32 value for this member. This will not be set
1837
for members that were constructed from strings or external
1838
files until after the member has been written.
1839
 
1840
=item crc32String()
1841
 
1842
Return the CRC-32 value for this member as an 8 character
1843
printable hex string. This will not be set for members that
1844
were constructed from strings or external files until after
1845
the member has been written.
1846
 
1847
=item compressedSize()
1848
 
1849
Return the compressed size for this member. This will not be
1850
set for members that were constructed from strings or
1851
external files until after the member has been written.
1852
 
1853
=item uncompressedSize()
1854
 
1855
Return the uncompressed size for this member.
1856
 
1857
=item password( [ $password ] )
1858
 
1859
Returns the password for this member to be used on decryption.
1860
If $password is given, it will set the password for the decryption.
1861
 
1862
=item isEncrypted()
1863
 
1864
Return true if this member is encrypted. The Archive::Zip
1865
module does not currently support creation of encrypted
1866
members. Decryption works more or less like this:
1867
 
1868
  my $zip = Archive::Zip->new;
1869
  $zip->read ("encrypted.zip");
1870
  for my $m (map { $zip->memberNamed ($_) } $zip->memberNames) {
1871
      $m->password ("secret");
1872
      $m->contents;  # is "" when password was wrong
1873
 
1874
That shows that the password has to be set per member, and not per
1875
archive. This might change in the future.
1876
 
1877
=item isTextFile( [ $flag ] )
1878
 
1879
=item isTextFile( [ { flag => $flag } ] )
1880
 
1881
Returns true if I am a text file. Also can set the status if
1882
given an argument (then returns old state). Note that this
1883
module does not currently do anything with this flag upon
1884
extraction or storage. That is, bytes are stored in native
1885
format whether or not they came from a text file.
1886
 
1887
=item isBinaryFile()
1888
 
1889
Returns true if I am a binary file. Also can set the status
1890
if given an argument (then returns old state). Note that this
1891
module does not currently do anything with this flag upon
1892
extraction or storage. That is, bytes are stored in native
1893
format whether or not they came from a text file.
1894
 
1895
=item extractToFileNamed( $fileName )
1896
 
1897
=item extractToFileNamed( { name => $fileName } )
1898
 
1899
Extract me to a file with the given name. The file will be
1900
created with default modes. Directories will be created as
1901
needed.
1902
The C<$fileName> argument should be a valid file name on your
1903
file system.
1904
Returns AZ_OK on success.
1905
 
1906
=item isDirectory()
1907
 
1908
Returns true if I am a directory.
1909
 
1910
=item writeLocalHeaderRelativeOffset()
1911
 
1912
Returns the file offset in bytes the last time I was written.
1913
 
1914
=item wasWritten()
1915
 
1916
Returns true if I was successfully written. Reset at the
1917
beginning of a write attempt.
1918
 
1919
=back
1920
 
1921
=head2 Low-level member data reading
1922
 
1923
It is possible to use lower-level routines to access member data
1924
streams, rather than the extract* methods and contents(). For
1925
instance, here is how to print the uncompressed contents of a member
1926
in chunks using these methods:
1927
 
1928
    my ( $member, $status, $bufferRef );
1929
    $member = $zip->memberNamed( 'xyz.txt' );
1930
    $member->desiredCompressionMethod( COMPRESSION_STORED );
1931
    $status = $member->rewindData();
1932
    die "error $status" unless $status == AZ_OK;
1933
    while ( ! $member->readIsDone() )
1934
    {
1935
    ( $bufferRef, $status ) = $member->readChunk();
1936
    die "error $status"
1937
                if $status != AZ_OK && $status != AZ_STREAM_END;
1938
    # do something with $bufferRef:
1939
    print $$bufferRef;
1940
    }
1941
    $member->endRead();
1942
 
1943
=over 4
1944
 
1945
=item readChunk( [ $chunkSize ] )
1946
 
1947
=item readChunk( [ { chunkSize => $chunkSize } ] )
1948
 
1949
This reads the next chunk of given size from the member's
1950
data stream and compresses or uncompresses it as necessary,
1951
returning a reference to the bytes read and a status. If size
1952
argument is not given, defaults to global set by
1953
Archive::Zip::setChunkSize. Status is AZ_OK on success until
1954
the last chunk, where it returns AZ_STREAM_END. Returns C<(
1955
\$bytes, $status)>.
1956
 
1957
    my ( $outRef, $status ) = $self->readChunk();
1958
    print $$outRef if $status != AZ_OK && $status != AZ_STREAM_END;
1959
 
1960
=item rewindData()
1961
 
1962
Rewind data and set up for reading data streams or writing
1963
zip files. Can take options for C<inflateInit()> or
1964
C<deflateInit()>, but this is not likely to be necessary.
1965
Subclass overrides should call this method. Returns C<AZ_OK>
1966
on success.
1967
 
1968
=item endRead()
1969
 
1970
Reset the read variables and free the inflater or deflater.
1971
Must be called to close files, etc. Returns AZ_OK on success.
1972
 
1973
=item readIsDone()
1974
 
1975
Return true if the read has run out of data or encountered an error.
1976
 
1977
=item contents()
1978
 
1979
Return the entire uncompressed member data or undef in scalar
1980
context. When called in array context, returns C<( $string,
1981
$status )>; status will be AZ_OK on success:
1982
 
1983
    my $string = $member->contents();
1984
    # or
1985
    my ( $string, $status ) = $member->contents();
1986
    die "error $status" unless $status == AZ_OK;
1987
 
1988
Can also be used to set the contents of a member (this may
1989
change the class of the member):
1990
 
1991
    $member->contents( "this is my new contents" );
1992
 
1993
=item extractToFileHandle( $fh )
1994
 
1995
=item extractToFileHandle( { fileHandle => $fh } )
1996
 
1997
Extract (and uncompress, if necessary) the member's contents
1998
to the given file handle. Return AZ_OK on success.
1999
 
2000
=back
2001
 
2002
=head1 Archive::Zip::FileMember methods
2003
 
2004
The Archive::Zip::FileMember class extends Archive::Zip::Member. It is the
2005
base class for both ZipFileMember and NewFileMember classes. This class adds
2006
an C<externalFileName> and an C<fh> member to keep track of the external
2007
file.
2008
 
2009
=over 4
2010
 
2011
=item externalFileName()
2012
 
2013
Return the member's external filename.
2014
 
2015
=item fh()
2016
 
2017
Return the member's read file handle. Automatically opens file if
2018
necessary.
2019
 
2020
=back
2021
 
2022
=head1 Archive::Zip::ZipFileMember methods
2023
 
2024
The Archive::Zip::ZipFileMember class represents members that have been read
2025
from external zip files.
2026
 
2027
=over 4
2028
 
2029
=item diskNumberStart()
2030
 
2031
Returns the disk number that the member's local header resides in.
2032
Should be 0.
2033
 
2034
=item localHeaderRelativeOffset()
2035
 
2036
Returns the offset into the zip file where the member's local header
2037
is.
2038
 
2039
=item dataOffset()
2040
 
2041
Returns the offset from the beginning of the zip file to the member's
2042
data.
2043
 
2044
=back
2045
 
2046
=head1 REQUIRED MODULES
2047
 
2048
L<Archive::Zip> requires several other modules:
2049
 
2050
L<Carp>
2051
 
2052
L<Compress::Raw::Zlib>
2053
 
2054
L<Cwd>
2055
 
2056
L<File::Basename>
2057
 
2058
L<File::Copy>
2059
 
2060
L<File::Find>
2061
 
2062
L<File::Path>
2063
 
2064
L<File::Spec>
2065
 
2066
L<IO::File>
2067
 
2068
L<IO::Seekable>
2069
 
2070
L<Time::Local>
2071
 
2072
=head1 BUGS AND CAVEATS
2073
 
2074
=head2 When not to use Archive::Zip
2075
 
2076
If you are just going to be extracting zips (and/or other archives) you
2077
are recommended to look at using L<Archive::Extract> instead, as it is much
2078
easier to use and factors out archive-specific functionality.
2079
 
2080
=head2 Try to avoid IO::Scalar
2081
 
2082
One of the most common ways to use Archive::Zip is to generate Zip files
2083
in-memory. Most people use L<IO::Scalar> for this purpose.
2084
 
2085
Unfortunately, as of 1.11 this module no longer works with L<IO::Scalar>
2086
as it incorrectly implements seeking.
2087
 
2088
Anybody using L<IO::Scalar> should consider porting to L<IO::String>,
2089
which is smaller, lighter, and is implemented to be perfectly compatible
2090
with regular seekable filehandles.
2091
 
2092
Support for L<IO::Scalar> most likely will B<not> be restored in the
2093
future, as L<IO::Scalar> itself cannot change the way it is implemented
2094
due to back-compatibility issues.
2095
 
2096
=head2 Wrong password for encrypted members
2097
 
2098
When an encrypted member is read using the wrong password, you currently
2099
have to re-read the entire archive to try again with the correct password.
2100
 
2101
=head1 TO DO
2102
 
2103
* auto-choosing storing vs compression
2104
 
2105
* extra field hooks (see notes.txt)
2106
 
2107
* check for duplicates on addition/renaming?
2108
 
2109
* Text file extraction (line end translation)
2110
 
2111
* Reading zip files from non-seekable inputs
2112
  (Perhaps by proxying through IO::String?)
2113
 
2114
* separate unused constants into separate module
2115
 
2116
* cookbook style docs
2117
 
2118
* Handle tainted paths correctly
2119
 
2120
* Work on better compatibility with other IO:: modules
2121
 
2122
* Support encryption
2123
 
2124
* More user-friendly decryption
2125
 
2126
=head1 SUPPORT
2127
 
2128
Bugs should be reported via the CPAN bug tracker
2129
 
2130
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Archive-Zip>
2131
 
2132
For other issues contact the maintainer
2133
 
2134
=head1 AUTHOR
2135
 
2136
Currently maintained by Fred Moyer <fred@redhotpenguin.com>
2137
 
2138
Previously maintained by Adam Kennedy <adamk@cpan.org>
2139
 
2140
Previously maintained by Steve Peters E<lt>steve@fisharerojo.orgE<gt>.
2141
 
2142
File attributes code by Maurice Aubrey E<lt>maurice@lovelyfilth.comE<gt>.
2143
 
2144
Originally by Ned Konz E<lt>nedkonz@cpan.orgE<gt>.
2145
 
2146
=head1 COPYRIGHT
2147
 
2148
Some parts copyright 2006 - 2012 Adam Kennedy.
2149
 
2150
Some parts copyright 2005 Steve Peters.
2151
 
2152
Original work copyright 2000 - 2004 Ned Konz.
2153
 
2154
This program is free software; you can redistribute it and/or modify
2155
it under the same terms as Perl itself.
2156
 
2157
=head1 SEE ALSO
2158
 
2159
Look at L<Archive::Zip::MemberRead> which is a wrapper that allows one to
2160
read Zip archive members as if they were files.
2161
 
2162
L<Compress::Raw::Zlib>, L<Archive::Tar>, L<Archive::Extract>
2163
 
2164
=cut