File: Zip.pod

package info (click to toggle)
libarchive-zip-perl 1.14-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 548 kB
  • ctags: 317
  • sloc: perl: 3,369; makefile: 45
file content (1586 lines) | stat: -rw-r--r-- 41,069 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586

=head1 NAME


Archive::Zip - Provide an interface to ZIP archive files.

=head1 SYNOPSIS


   use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
   my $zip = Archive::Zip->new();
   my $member = $zip->addDirectory( 'dirname/' );
   $member = $zip->addString( 'This is a test', 'stringMember.txt' );
   $member->desiredCompressionMethod( COMPRESSION_DEFLATED );
   $member = $zip->addFile( 'xyz.pl', 'AnotherName.pl' );
   die 'write error' unless $zip->writeToFileNamed( 'someZip.zip' ) == AZ_OK;
   $zip = Archive::Zip->new();
   die 'read error' unless $zip->read( 'someZip.zip' ) == AZ_OK;
   $member = $zip->memberNamed( 'stringMember.txt' );
   $member->desiredCompressionMethod( COMPRESSION_STORED );
   die 'write error' unless $zip->writeToFileNamed( 'someOtherZip.zip' ) == AZ_OK;

=head1 DESCRIPTION


The Archive::Zip module allows a Perl program to create, manipulate, read,
and write Zip archive files. Zip archives can be created, or you can read
from existing zip files. Once created, they can be written to files, streams,
or strings. Members can be added, removed, extracted, replaced, rearranged,
and enumerated. They can also be renamed or have their dates, comments, or
other attributes queried or modified. Their data can be compressed or
uncompressed as needed. Members can be created from members in existing Zip
files, or from existing directories, files, or strings. This module uses the
L<Compress::Zlib|Compress::Zlib> library to read and write the compressed
streams inside the files.

=head1 FILE NAMING


Regardless of what your local file system uses for file naming, names in a
Zip file are in Unix format (I<forward> slashes (/) separating directory
names, etc.).
C<Archive::Zip> tries to be consistent with file naming
conventions, and will
translate back and forth between native and Zip file names.
However, it can't guess which format names are in. So two rules control what
kind of file name you must pass various routines:

=over 4

=item Names of files are in local format.



C<File::Spec> and C<File::Basename> are used for various file
operations. When you're referring to a file on your system, use its
file naming conventions.

=item Names of archive members are in Unix format.



This applies to every method that refers to an archive member, or
provides a name for new archive members. The C<extract()> methods
that can take one or two names will convert from local to zip names
if you call them with a single name.

=back

=head1 OBJECT MODEL


=head2 Overview


Archive::Zip::Archive objects are what you ordinarily deal with.
These maintain the structure of a zip file, without necessarily
holding data. When a zip is read from a disk file, the (possibly
compressed) data still lives in the file, not in memory. Archive
members hold information about the individual members, but not
(usually) the actual member data. When the zip is written to a
(different) file, the member data is compressed or copied as needed.
It is possible to make archive members whose data is held in a string
in memory, but this is not done when a zip file is read. Directory
members don't have any data.

=head2 Inheritance


  Exporter
   Archive::Zip                            Common base class, has defs.
       Archive::Zip::Archive               A Zip archive.
       Archive::Zip::Member                Abstract superclass for all members.
           Archive::Zip::StringMember      Member made from a string
           Archive::Zip::FileMember        Member made from an external file
               Archive::Zip::ZipFileMember Member that lives in a zip file
               Archive::Zip::NewFileMember Member whose data is in a file
           Archive::Zip::DirectoryMember   Member that is a directory

=head1 EXPORTS


=over 4

=item :CONSTANTS



Exports the following constants: FA_MSDOS FA_UNIX GPBF_ENCRYPTED_MASK
GPBF_DEFLATING_COMPRESSION_MASK GPBF_HAS_DATA_DESCRIPTOR_MASK
COMPRESSION_STORED COMPRESSION_DEFLATED IFA_TEXT_FILE_MASK
IFA_TEXT_FILE IFA_BINARY_FILE COMPRESSION_LEVEL_NONE
COMPRESSION_LEVEL_DEFAULT COMPRESSION_LEVEL_FASTEST
COMPRESSION_LEVEL_BEST_COMPRESSION

=item :MISC_CONSTANTS



Exports the following constants (only necessary for extending the
module): FA_AMIGA FA_VAX_VMS FA_VM_CMS FA_ATARI_ST FA_OS2_HPFS
FA_MACINTOSH FA_Z_SYSTEM FA_CPM FA_WINDOWS_NTFS
GPBF_IMPLODING_8K_SLIDING_DICTIONARY_MASK
GPBF_IMPLODING_3_SHANNON_FANO_TREES_MASK
GPBF_IS_COMPRESSED_PATCHED_DATA_MASK COMPRESSION_SHRUNK
DEFLATING_COMPRESSION_NORMAL DEFLATING_COMPRESSION_MAXIMUM
DEFLATING_COMPRESSION_FAST DEFLATING_COMPRESSION_SUPER_FAST
COMPRESSION_REDUCED_1 COMPRESSION_REDUCED_2 COMPRESSION_REDUCED_3
COMPRESSION_REDUCED_4 COMPRESSION_IMPLODED COMPRESSION_TOKENIZED
COMPRESSION_DEFLATED_ENHANCED
COMPRESSION_PKWARE_DATA_COMPRESSION_LIBRARY_IMPLODED

=item :ERROR_CODES



Explained below. Returned from most methods. AZ_OK AZ_STREAM_END
AZ_ERROR AZ_FORMAT_ERROR AZ_IO_ERROR

=back

=head1 ERROR CODES


Many of the methods in Archive::Zip return error codes. These are implemented
as inline subroutines, using the C<use constant> pragma. They can be imported
into your namespace using the C<:ERROR_CODES> tag:

  use Archive::Zip qw( :ERROR_CODES );
  ...
  die "whoops!" unless $zip->read( 'myfile.zip' ) == AZ_OK;

=over 4

=item AZ_OK (0)



Everything is fine.

=item AZ_STREAM_END (1)



The read stream (or central directory) ended normally.

=item AZ_ERROR (2)



There was some generic kind of error.

=item AZ_FORMAT_ERROR (3)



There is a format error in a ZIP file being read.

=item AZ_IO_ERROR (4)



There was an IO error.

=back

=head1 COMPRESSION


Archive::Zip allows each member of a ZIP file to be compressed (using the
Deflate algorithm) or uncompressed. Other compression algorithms that some
versions of ZIP have been able to produce are not supported. Each member has
two compression methods: the one it's stored as (this is always
COMPRESSION_STORED for string and external file members), and the one you
desire for the member in the zip file. These can be different, of course, so
you can make a zip member that is not compressed out of one that is, and vice
versa. You can inquire about the current compression and set the desired
compression method:

  my $member = $zip->memberNamed( 'xyz.txt' );
  $member->compressionMethod();    # return current compression
  # set to read uncompressed
  $member->desiredCompressionMethod( COMPRESSION_STORED );
  # set to read compressed
  $member->desiredCompressionMethod( COMPRESSION_DEFLATED );

There are two different compression methods:

=over 4

=item COMPRESSION_STORED



file is stored (no compression)

=item COMPRESSION_DEFLATED



file is Deflated

=back

=head2 Compression Levels


If a member's desiredCompressionMethod is COMPRESSION_DEFLATED, you
can choose different compression levels. This choice may affect the
speed of compression and decompression, as well as the size of the
compressed member data.

  $member->desiredCompressionLevel( 9 );

The levels given can be:

=over 4

=item 0 or COMPRESSION_LEVEL_NONE



This is the same as saying

  $member->desiredCompressionMethod( COMPRESSION_STORED );

=item 1 .. 9



1 gives the best speed and worst compression, and 9 gives the
best compression and worst speed.

=item COMPRESSION_LEVEL_FASTEST



This is a synonym for level 1.

=item COMPRESSION_LEVEL_BEST_COMPRESSION



This is a synonym for level 9.

=item COMPRESSION_LEVEL_DEFAULT



This gives a good compromise between speed and compression,
and is currently equivalent to 6 (this is in the zlib code).
This is the level that will be used if not specified.

=back

=head1 Archive::Zip methods


The Archive::Zip class (and its invisible subclass Archive::Zip::Archive)
implement generic zip file functionality. Creating a new Archive::Zip object
actually makes an Archive::Zip::Archive object, but you don't have to worry
about this unless you're subclassing.

=head2 Constructor


=over 4

=item new( [$fileName] )



Make a new, empty zip archive.

    my $zip = Archive::Zip->new();

If an additional argument is passed, new() will call read()
to read the contents of an archive:

    my $zip = Archive::Zip->new( 'xyz.zip' );

If a filename argument is passed and the read fails for any
reason, new will return undef. For this reason, it may be
better to call read separately.

=back

=head2 Zip Archive Utility Methods


These Archive::Zip methods may be called as functions or as object
methods. Do not call them as class methods:

    $zip = Archive::Zip->new();
    $crc = Archive::Zip::computeCRC32( 'ghijkl' );    # OK
    $crc = $zip->computeCRC32( 'ghijkl' );            # also OK
    $crc = Archive::Zip->computeCRC32( 'ghijkl' );    # NOT OK

=over 4

=item Archive::Zip::computeCRC32( $string [, $crc] )



This is a utility function that uses the Compress::Zlib CRC
routine to compute a CRC-32. You can get the CRC of a string:

    $crc = Archive::Zip::computeCRC32( $string );

Or you can compute the running CRC:

    $crc = 0;
    $crc = Archive::Zip::computeCRC32( 'abcdef', $crc );
    $crc = Archive::Zip::computeCRC32( 'ghijkl', $crc );

=item Archive::Zip::setChunkSize( $number )



Report or change chunk size used for reading and writing.
This can make big differences in dealing with large files.
Currently, this defaults to 32K. This also changes the chunk
size used for Compress::Zlib. You must call setChunkSize()
before reading or writing. This is not exportable, so you
must call it like:

    Archive::Zip::setChunkSize( 4096 );

or as a method on a zip (though this is a global setting).
Returns old chunk size.

=item Archive::Zip::chunkSize()



Returns the current chunk size:

    my $chunkSize = Archive::Zip::chunkSize();

=item Archive::Zip::setErrorHandler( \&subroutine )



Change the subroutine called with error strings. This
defaults to \&Carp::carp, but you may want to change it to
get the error strings. This is not exportable, so you must
call it like:

    Archive::Zip::setErrorHandler( \&myErrorHandler );

If myErrorHandler is undef, resets handler to default.
Returns old error handler. Note that if you call Carp::carp
or a similar routine or if you're chaining to the default
error handler from your error handler, you may want to
increment the number of caller levels that are skipped (do
not just set it to a number):

    $Carp::CarpLevel++;

=item Archive::Zip::tempFile( [$tmpdir] )



Create a uniquely named temp file. It will be returned open
for read/write. If C<$tmpdir> is given, it is used as the
name of a directory to create the file in. If not given,
creates the file using C<File::Spec::tmpdir()>. Generally, you can
override this choice using the

    $ENV{TMPDIR}

environment variable. But see the L<File::Spec|File::Spec>
documentation for your system. Note that on many systems, if you're
running in taint mode, then you must make sure that C<$ENV{TMPDIR}> is
untainted for it to be used.
Will I<NOT> create C<$tmpdir> if it doesn't exist (this is a change
from prior versions!). Returns file handle and name:

    my ($fh, $name) = Archive::Zip::tempFile();
    my ($fh, $name) = Archive::Zip::tempFile('myTempDir');
    my $fh = Archive::Zip::tempFile();  # if you don't need the name

=back

=head2 Zip Archive Accessors


=over 4

=item members()



Return a copy of the members array

    my @members = $zip->members();

=item numberOfMembers()



Return the number of members I have

=item memberNames()



Return a list of the (internal) file names of the zip members

=item memberNamed( $string )



Return ref to member whose filename equals given filename or
undef. C<$string> must be in Zip (Unix) filename format.

=item membersMatching( $regex )



Return array of members whose filenames match given regular
expression in list context. Returns number of matching
members in scalar context.

    my @textFileMembers = $zip->membersMatching( '.*\.txt' );
    # or
    my $numberOfTextFiles = $zip->membersMatching( '.*\.txt' );

=item diskNumber()



Return the disk that I start on. Not used for writing zips,
but might be interesting if you read a zip in. This should be
0, as Archive::Zip does not handle multi-volume archives.

=item diskNumberWithStartOfCentralDirectory()



Return the disk number that holds the beginning of the
central directory. Not used for writing zips, but might be
interesting if you read a zip in. This should be 0, as
Archive::Zip does not handle multi-volume archives.

=item numberOfCentralDirectoriesOnThisDisk()



Return the number of CD structures in the zipfile last read in.
Not used for writing zips, but might be interesting if you read a zip
in.

=item numberOfCentralDirectories()



Return the number of CD structures in the zipfile last read in.
Not used for writing zips, but might be interesting if you read a zip
in.

=item centralDirectorySize()



Returns central directory size, as read from an external zip
file. Not used for writing zips, but might be interesting if
you read a zip in.

=item centralDirectoryOffsetWRTStartingDiskNumber()



Returns the offset into the zip file where the CD begins. Not
used for writing zips, but might be interesting if you read a
zip in.

=item zipfileComment( [$string] )



Get or set the zipfile comment. Returns the old comment.

    print $zip->zipfileComment();
    $zip->zipfileComment( 'New Comment' );

=item eocdOffset()



Returns the (unexpected) number of bytes between where the
EOCD was found and where it expected to be. This is normally
0, but would be positive if something (a virus, perhaps) had
added bytes somewhere before the EOCD. Not used for writing
zips, but might be interesting if you read a zip in. Here is
an example of how you can diagnose this:

  my $zip = Archive::Zip->new('somefile.zip');
  if ($zip->eocdOffset())
  {
    warn "A virus has added ", $zip->eocdOffset, " bytes of garbage\n";
  }

The C<eocdOffset()> is used to adjust the starting position of member
headers, if necessary.

=item fileName()



Returns the name of the file last read from. If nothing has
been read yet, returns an empty string; if read from a file
handle, returns the handle in string form.

=back

=head2 Zip Archive Member Operations


Various operations on a zip file modify members. When a member is
passed as an argument, you can either use a reference to the member
itself, or the name of a member. Of course, using the name requires
that names be unique within a zip (this is not enforced).

=over 4

=item removeMember( $memberOrName )



Remove and return the given member, or match its name and
remove it. Returns undef if member or name doesn't exist in this
Zip. No-op if member does not belong to this zip.

=item replaceMember( $memberOrName, $newMember )



Remove and return the given member, or match its name and
remove it. Replace with new member. Returns undef if member or
name doesn't exist in this Zip, or if C<$newMember> is undefined.


It is an (undiagnosed) error to provide a C<$newMember> that is a
member of the zip being modified.

    my $member1 = $zip->removeMember( 'xyz' );
    my $member2 = $zip->replaceMember( 'abc', $member1 );
    # now, $member2 (named 'abc') is not in $zip,
    # and $member1 (named 'xyz') is, having taken $member2's place.

=item extractMember( $memberOrName [, $extractedName ] )



Extract the given member, or match its name and extract it.
Returns undef if member doesn't exist in this Zip. If
optional second arg is given, use it as the name of the
extracted member. Otherwise, the internal filename of the
member is used as the name of the extracted file or
directory.
If you pass C<$extractedName>, it should be in the local file
system's format.
All necessary directories will be created. Returns C<AZ_OK>
on success.

=item extractMemberWithoutPaths( $memberOrName [, $extractedName ] )



Extract the given member, or match its name and extract it.
Does not use path information (extracts into the current
directory). Returns undef if member doesn't exist in this
Zip.
If optional second arg is given, use it as the name of the
extracted member (its paths will be deleted too). Otherwise,
the internal filename of the member (minus paths) is used as
the name of the extracted file or directory. Returns C<AZ_OK>
on success.

=item addMember( $member )



Append a member (possibly from another zip file) to the zip
file. Returns the new member. Generally, you will use
addFile(), addDirectory(), addFileOrDirectory(), addString(),
or read() to add members.

    # Move member named 'abc' to end of zip:
    my $member = $zip->removeMember( 'abc' );
    $zip->addMember( $member );

=item updateMember( $memberOrName, $fileName )



Update a single member from the file or directory named C<$fileName>.
Returns the (possibly added or updated) member, if any; C<undef> on
errors.
The comparison is based on C<lastModTime()> and (in the case of a
non-directory) the size of the file.

=item addFile( $fileName [, $newName ] )



Append a member whose data comes from an external file,
returning the member or undef. The member will have its file
name set to the name of the external file, and its
desiredCompressionMethod set to COMPRESSION_DEFLATED. The
file attributes and last modification time will be set from
the file.
If the name given does not represent a readable plain file or
symbolic link, undef will be returned. C<$fileName> must be
in the format required for the local file system.
The optional C<$newName> argument sets the internal file name
to something different than the given $fileName. C<$newName>,
if given, must be in Zip name format (i.e. Unix).
The text mode bit will be set if the contents appears to be
text (as returned by the C<-T> perl operator).


I<NOTE> that you shouldn't (generally) use absolute path names
in zip member names, as this will cause problems with some zip
tools as well as introduce a security hole and make the zip
harder to use.

=item addDirectory( $directoryName [, $fileName ] )



Append a member created from the given directory name. The
directory name does not have to name an existing directory.
If the named directory exists, the file modification time and
permissions are set from the existing directory, otherwise
they are set to now and permissive default permissions.
C<$directoryName> must be in local file system format.
The optional second argument sets the name of the archive
member (which defaults to C<$directoryName>). If given, it
must be in Zip (Unix) format.
Returns the new member.

=item addFileOrDirectory( $name [, $newName ] )



Append a member from the file or directory named $name. If
$newName is given, use it for the name of the new member.
Will add or remove trailing slashes from $newName as needed.
C<$name> must be in local file system format.
The optional second argument sets the name of the archive
member (which defaults to C<$name>). If given, it must be in
Zip (Unix) format.

=item addString( $stringOrStringRef, $name )



Append a member created from the given string or string
reference. The name is given by the second argument.
Returns the new member. The last modification time will be
set to now, and the file attributes will be set to permissive
defaults.

    my $member = $zip->addString( 'This is a test', 'test.txt' );

=item contents( $memberOrMemberName [, $newContents ] )



Returns the uncompressed data for a particular member, or
undef.

    print "xyz.txt contains " . $zip->contents( 'xyz.txt' );

Also can change the contents of a member:

    $zip->contents( 'xyz.txt', 'This is the new contents' );

=back

=head2 Zip Archive I/O operations


A Zip archive can be written to a file or file handle, or read from
one.

=over 4

=item writeToFileNamed( $fileName )



Write a zip archive to named file. Returns C<AZ_OK> on
success.

    my $status = $zip->writeToFileNamed( 'xx.zip' );
    die "error somewhere" if $status != AZ_OK;

Note that if you use the same name as an existing zip file
that you read in, you will clobber ZipFileMembers. So
instead, write to a different file name, then delete the
original.
If you use the C<overwrite()> or C<overwriteAs()> methods, you can
re-write the original zip in this way.
C<$fileName> should be a valid file name on your system.

=item writeToFileHandle( $fileHandle [, $seekable] )



Write a zip archive to a file handle. Return AZ_OK on
success. The optional second arg tells whether or not to try
to seek backwards to re-write headers. If not provided, it is
set if the Perl C<-f> test returns true. This could fail on
some operating systems, though.

    my $fh = IO::File->new( 'someFile.zip', 'w' );
    if ( $zip->writeToFileHandle( $fh ) != AZ_OK)
    	{
    		# error handling
    	}

If you pass a file handle that is not seekable (like if
you're writing to a pipe or a socket), pass a false second
argument:

    my $fh = IO::File->new( '| cat > somefile.zip', 'w' );
    $zip->writeToFileHandle( $fh, 0 );   # fh is not seekable

If this method fails during the write of a member, that
member and all following it will return false from
C<wasWritten()>. See writeCentralDirectory() for a way to
deal with this.
If you want, you can write data to the file handle before
passing it to writeToFileHandle(); this could be used (for
instance) for making self-extracting archives. However, this
only works reliably when writing to a real file (as opposed
to STDOUT or some other possible non-file).
See examples/selfex.pl for how to write a self-extracting
archive.

=item writeCentralDirectory( $fileHandle [, $offset ] )



Writes the central directory structure to the given file
handle. Returns AZ_OK on success. If given an $offset, will
seek to that point before writing. This can be used for
recovery in cases where writeToFileHandle or writeToFileNamed
returns an IO error because of running out of space on the
destination file. You can truncate the zip by seeking
backwards and then writing the directory:

    my $fh = IO::File->new( 'someFile.zip', 'w' );
    	my $retval = $zip->writeToFileHandle( $fh );
    if ( $retval == AZ_IO_ERROR )
    	{
    		my @unwritten = grep { not $_->wasWritten() } $zip->members();
    		if (@unwritten)
    		{
    			$zip->removeMember( $member ) foreach my $member ( @unwritten );
    			$zip->writeCentralDirectory( $fh,
    				$unwritten[0]->writeLocalHeaderRelativeOffset());
    		}
    	}

=item overwriteAs( $newName )



Write the zip to the specified file, as safely as possible.
This is done by first writing to a temp file, then renaming
the original if it exists, then renaming the temp file, then
deleting the renamed original if it exists. Returns AZ_OK if
successful.

=item overwrite()



Write back to the original zip file. See overwriteAs() above.
If the zip was not ever read from a file, this generates an
error.

=item read( $fileName )



Read zipfile headers from a zip file, appending new members.
Returns C<AZ_OK> or error code.

    my $zipFile = Archive::Zip->new();
    my $status = $zipFile->read( '/some/FileName.zip' );

=item readFromFileHandle( $fileHandle, $filename )



Read zipfile headers from an already-opened file handle,
appending new members. Does not close the file handle.
Returns C<AZ_OK> or error code. Note that this requires a
seekable file handle; reading from a stream is not yet
supported.

    my $fh = IO::File->new( '/some/FileName.zip', 'r' );
    my $zip1 = Archive::Zip->new();
    my $status = $zip1->readFromFileHandle( $fh );
    my $zip2 = Archive::Zip->new();
    $status = $zip2->readFromFileHandle( $fh );

=back

=head2 Zip Archive Tree operations


These used to be in Archive::Zip::Tree but got moved into
Archive::Zip. They enable operation on an entire tree of members or
files.
A usage example:

  use Archive::Zip;
  my $zip = Archive::Zip->new();
  # add all readable files and directories below . as xyz/*
  $zip->addTree( '.', 'xyz' );	
  # add all readable plain files below /abc as def/*
  $zip->addTree( '/abc', 'def', sub { -f && -r } );	
  # add all .c files below /tmp as stuff/*
  $zip->addTreeMatching( '/tmp', 'stuff', '\.c$' );
  # add all .o files below /tmp as stuff/* if they aren't writable
  $zip->addTreeMatching( '/tmp', 'stuff', '\.o$', sub { ! -w } );
  # add all .so files below /tmp that are smaller than 200 bytes as stuff/*
  $zip->addTreeMatching( '/tmp', 'stuff', '\.o$', sub { -s < 200 } );
  # and write them into a file
  $zip->writeToFileNamed('xxx.zip');
  # now extract the same files into /tmpx
  $zip->extractTree( 'stuff', '/tmpx' );

=over 4

=item $zip->addTree( $root, $dest [,$pred] ) -- Add tree of files to a zip



C<$root> is the root of the tree of files and directories to be
added. It is a valid directory name on your system. C<$dest> is
the name for the root in the zip file (undef or blank means
to use relative pathnames). It is a valid ZIP directory name
(that is, it uses forward slashes (/) for separating
directory components). C<$pred> is an optional subroutine
reference to select files: it is passed the name of the
prospective file or directory using C<$_>, and if it returns
true, the file or directory will be included. The default is
to add all readable files and directories. For instance,
using

  my $pred = sub { /\.txt/ };
  $zip->addTree( '.', '', $pred );

will add all the .txt files in and below the current
directory, using relative names, and making the names
identical in the zipfile:

  original name           zip member name
  ./xyz                   xyz
  ./a/                    a/
  ./a/b                   a/b

To translate absolute to relative pathnames, just pass them
in: $zip->addTree( '/c/d', 'a' );

  original name           zip member name
  /c/d/xyz                a/xyz
  /c/d/a/                 a/a/
  /c/d/a/b                a/a/b

Returns AZ_OK on success. Note that this will not follow
symbolic links to directories. Note also that this does not
check for the validity of filenames.


Note that you generally I<don't> want to make zip archive member names
absolute.

=item $zip->addTreeMatching( $root, $dest, $pattern [,$pred] )



$root is the root of the tree of files and directories to be
added $dest is the name for the root in the zip file (undef
means to use relative pathnames) $pattern is a (non-anchored)
regular expression for filenames to match $pred is an
optional subroutine reference to select files: it is passed
the name of the prospective file or directory in C<$_>, and
if it returns true, the file or directory will be included.
The default is to add all readable files and directories. To
add all files in and below the current dirctory whose names
end in C<.pl>, and make them extract into a subdirectory
named C<xyz>, do this:

  $zip->addTreeMatching( '.', 'xyz', '\.pl$' )

To add all I<writable> files in and below the dirctory named
C</abc> whose names end in C<.pl>, and make them extract into
a subdirectory named C<xyz>, do this:

  $zip->addTreeMatching( '/abc', 'xyz', '\.pl$', sub { -w } )

Returns AZ_OK on success. Note that this will not follow
symbolic links to directories.

=item $zip->updateTree( $root, [ $dest, [ $pred [, $mirror]]] );



Update a zip file from a directory tree.

C<updateTree()> takes the same arguments as C<addTree()>, but first
checks to see whether the file or directory already exists in the zip
file, and whether it has been changed.

If the fourth argument C<$mirror> is true, then delete all my members
if corresponding files weren't found.


Returns an error code or AZ_OK of all is well.

=item $zip->extractTree()



=item $zip->extractTree( $root )



=item $zip->extractTree( $root, $dest )



=item $zip->extractTree( $root, $dest, $volume )



If you don't give any arguments at all, will extract all the
files in the zip with their original names.


If you supply one argument for C<$root>, C<extractTree> will extract
all the members whose names start with C<$root> into the current
directory, stripping off C<$root> first.
C<$root> is in Zip (Unix) format.
For instance,

  $zip->extractTree( 'a' );

when applied to a zip containing the files:
a/x a/b/c ax/d/e d/e will extract:


a/x as ./x


a/b/c as ./b/c


If you give two arguments, C<extractTree> extracts all the members
whose names start with C<$root>. It will translate C<$root> into
C<$dest> to construct the destination file name.
C<$root> and C<$dest> are in Zip (Unix) format.
For instance,

   $zip->extractTree( 'a', 'd/e' );

when applied to a zip containing the files:
a/x a/b/c ax/d/e d/e will extract:


a/x to d/e/x


a/b/c to d/e/b/c and ignore ax/d/e and d/e


If you give three arguments, C<extractTree> extracts all the members
whose names start with C<$root>. It will translate C<$root> into
C<$dest> to construct the destination file name, and then it will
convert to local file system format, using C<$volume> as the name of
the destination volume.


C<$root> and C<$dest> are in Zip (Unix) format.


C<$volume> is in local file system format.


For instance, under Windows,

   $zip->extractTree( 'a', 'd/e', 'f:' );

when applied to a zip containing the files:
a/x a/b/c ax/d/e d/e will extract:


a/x to f:d/e/x


a/b/c to f:d/e/b/c and ignore ax/d/e and d/e


If you want absolute paths (the prior example used paths relative to
the current directory on the destination volume, you can specify these
in C<$dest>:

   $zip->extractTree( 'a', '/d/e', 'f:' );

when applied to a zip containing the files:
a/x a/b/c ax/d/e d/e will extract:


a/x to f:\d\e\x


a/b/c to f:\d\e\b\c and ignore ax/d/e and d/e


=back

=head1 MEMBER OPERATIONS


=head2 Member Class Methods


Several constructors allow you to construct members without adding
them to a zip archive. These work the same as the addFile(),
addDirectory(), and addString() zip instance methods described above,
but they don't add the new members to a zip.

=over 4

=item Archive::Zip::Member->newFromString( $stringOrStringRef [, $fileName] )



Construct a new member from the given string. Returns undef
on error.

    my $member = Archive::Zip::Member->newFromString( 'This is a test',
                                                 'xyz.txt' );

=item newFromFile( $fileName )



Construct a new member from the given file. Returns undef on
error.

    my $member = Archive::Zip::Member->newFromFile( 'xyz.txt' );

=item newDirectoryNamed( $directoryName [, $zipname ] )



Construct a new member from the given directory.
C<$directoryName> must be a valid name on your file system; it doesn't
have to exist.


If given, C<$zipname> will be the name of the zip member; it must be a
valid Zip (Unix) name. If not given, it will be converted from
C<$directoryName>.


Returns undef on error.

    my $member = Archive::Zip::Member->newDirectoryNamed( 'CVS/' );

=back

=head2 Member Simple accessors


These methods get (and/or set) member attribute values.

=over 4

=item versionMadeBy()



Gets the field from the member header.

=item fileAttributeFormat( [$format] )



Gets or sets the field from the member header. These are
C<FA_*> values.

=item versionNeededToExtract()



Gets the field from the member header.

=item bitFlag()



Gets the general purpose bit field from the member header.
This is where the C<GPBF_*> bits live.

=item compressionMethod()



Returns the member compression method. This is the method
that is currently being used to compress the member data.
This will be COMPRESSION_STORED for added string or file
members, or any of the C<COMPRESSION_*> values for members
from a zip file. However, this module can only handle members
whose data is in COMPRESSION_STORED or COMPRESSION_DEFLATED
format.

=item desiredCompressionMethod( [$method] )



Get or set the member's C<desiredCompressionMethod>. This is
the compression method that will be used when the member is
written. Returns prior desiredCompressionMethod. Only
COMPRESSION_DEFLATED or COMPRESSION_STORED are valid
arguments. Changing to COMPRESSION_STORED will change the
member desiredCompressionLevel to 0; changing to
COMPRESSION_DEFLATED will change the member
desiredCompressionLevel to COMPRESSION_LEVEL_DEFAULT.

=item desiredCompressionLevel( [$method] )



Get or set the member's desiredCompressionLevel This is the
method that will be used to write. Returns prior
desiredCompressionLevel. Valid arguments are 0 through 9,
COMPRESSION_LEVEL_NONE, COMPRESSION_LEVEL_DEFAULT,
COMPRESSION_LEVEL_BEST_COMPRESSION, and
COMPRESSION_LEVEL_FASTEST. 0 or COMPRESSION_LEVEL_NONE will
change the desiredCompressionMethod to COMPRESSION_STORED.
All other arguments will change the desiredCompressionMethod
to COMPRESSION_DEFLATED.

=item externalFileName()



Return the member's external file name, if any, or undef.

=item fileName()



Get or set the member's internal filename. Returns the
(possibly new) filename. Names will have backslashes
converted to forward slashes, and will have multiple
consecutive slashes converted to single ones.

=item lastModFileDateTime()



Return the member's last modification date/time stamp in
MS-DOS format.

=item lastModTime()



Return the member's last modification date/time stamp,
converted to unix localtime format.

    print "Mod Time: " . scalar( localtime( $member->lastModTime() ) );

=item setLastModFileDateTimeFromUnix()



Set the member's lastModFileDateTime from the given unix
time.

    $member->setLastModFileDateTimeFromUnix( time() );

=item internalFileAttributes()



Return the internal file attributes field from the zip
header. This is only set for members read from a zip file.

=item externalFileAttributes()



Return member attributes as read from the ZIP file. Note that
these are NOT UNIX!

=item unixFileAttributes( [$newAttributes] )



Get or set the member's file attributes using UNIX file
attributes. Returns old attributes.

    my $oldAttribs = $member->unixFileAttributes( 0666 );

Note that the return value has more than just the file
permissions, so you will have to mask off the lowest bits for
comparisions.

=item localExtraField( [$newField] )



Gets or sets the extra field that was read from the local
header. This is not set for a member from a zip file until
after the member has been written out. The extra field must
be in the proper format.

=item cdExtraField( [$newField] )



Gets or sets the extra field that was read from the central
directory header. The extra field must be in the proper
format.

=item extraFields()



Return both local and CD extra fields, concatenated.

=item fileComment( [$newComment] )



Get or set the member's file comment.

=item hasDataDescriptor()



Get or set the data descriptor flag. If this is set, the
local header will not necessarily have the correct data
sizes. Instead, a small structure will be stored at the end
of the member data with these values. This should be
transparent in normal operation.

=item crc32()



Return the CRC-32 value for this member. This will not be set
for members that were constructed from strings or external
files until after the member has been written.

=item crc32String()



Return the CRC-32 value for this member as an 8 character
printable hex string. This will not be set for members that
were constructed from strings or external files until after
the member has been written.

=item compressedSize()



Return the compressed size for this member. This will not be
set for members that were constructed from strings or
external files until after the member has been written.

=item uncompressedSize()



Return the uncompressed size for this member.

=item isEncrypted()



Return true if this member is encrypted. The Archive::Zip
module does not currently create or extract encrypted
members.

=item isTextFile( [$flag] )



Returns true if I am a text file. Also can set the status if
given an argument (then returns old state). Note that this
module does not currently do anything with this flag upon
extraction or storage. That is, bytes are stored in native
format whether or not they came from a text file.

=item isBinaryFile()



Returns true if I am a binary file. Also can set the status
if given an argument (then returns old state). Note that this
module does not currently do anything with this flag upon
extraction or storage. That is, bytes are stored in native
format whether or not they came from a text file.

=item extractToFileNamed( $fileName )



Extract me to a file with the given name. The file will be
created with default modes. Directories will be created as
needed.
The C<$fileName> argument should be a valid file name on your
file system.
Returns AZ_OK on success.

=item isDirectory()



Returns true if I am a directory.

=item writeLocalHeaderRelativeOffset()



Returns the file offset in bytes the last time I was written.

=item wasWritten()



Returns true if I was successfully written. Reset at the
beginning of a write attempt.

=back

=head2 Low-level member data reading


It is possible to use lower-level routines to access member data
streams, rather than the extract* methods and contents(). For
instance, here is how to print the uncompressed contents of a member
in chunks using these methods:

    my ( $member, $status, $bufferRef );
    $member = $zip->memberNamed( 'xyz.txt' );
    $member->desiredCompressionMethod( COMPRESSION_STORED );
    $status = $member->rewindData();
    die "error $status" unless $status == AZ_OK;
    while ( ! $member->readIsDone() )
    {
    ( $bufferRef, $status ) = $member->readChunk();
    die "error $status"
    			if $status != AZ_OK && $status != AZ_STREAM_END;
    # do something with $bufferRef:
    print $$bufferRef;
    }
    $member->endRead();

=over 4

=item readChunk( [$chunkSize] )



This reads the next chunk of given size from the member's
data stream and compresses or uncompresses it as necessary,
returning a reference to the bytes read and a status. If size
argument is not given, defaults to global set by
Archive::Zip::setChunkSize. Status is AZ_OK on success until
the last chunk, where it returns AZ_STREAM_END. Returns C<(
\$bytes, $status)>.

    my ( $outRef, $status ) = $self->readChunk();
    print $$outRef if $status != AZ_OK && $status != AZ_STREAM_END;

=item rewindData()



Rewind data and set up for reading data streams or writing
zip files. Can take options for C<inflateInit()> or
C<deflateInit()>, but this isn't likely to be necessary.
Subclass overrides should call this method. Returns C<AZ_OK>
on success.

=item endRead()



Reset the read variables and free the inflater or deflater.
Must be called to close files, etc. Returns AZ_OK on success.

=item readIsDone()



Return true if the read has run out of data or errored out.

=item contents()



Return the entire uncompressed member data or undef in scalar
context. When called in array context, returns C<( $string,
$status )>; status will be AZ_OK on success:

    my $string = $member->contents();
    # or
    my ( $string, $status ) = $member->contents();
    die "error $status" unless $status == AZ_OK;

Can also be used to set the contents of a member (this may
change the class of the member):

    $member->contents( "this is my new contents" );

=item extractToFileHandle( $fh )



Extract (and uncompress, if necessary) the member's contents
to the given file handle. Return AZ_OK on success.

=back

=head1 Archive::Zip::FileMember methods


The Archive::Zip::FileMember class extends Archive::Zip::Member. It is the
base class for both ZipFileMember and NewFileMember classes. This class adds
an C<externalFileName> and an C<fh> member to keep track of the external
file.

=over 4

=item externalFileName()



Return the member's external filename.

=item fh()



Return the member's read file handle. Automatically opens file if
necessary.

=back

=head1 Archive::Zip::ZipFileMember methods


The Archive::Zip::ZipFileMember class represents members that have been read
from external zip files.

=over 4

=item diskNumberStart()



Returns the disk number that the member's local header resides in.
Should be 0.

=item localHeaderRelativeOffset()



Returns the offset into the zip file where the member's local header
is.

=item dataOffset()



Returns the offset from the beginning of the zip file to the member's
data.

=back

=head1 REQUIRED MODULES


L<Archive::Zip|Archive::Zip> requires several other modules:


L<Carp|Carp>


L<Compress::Zlib|Compress::Zlib>


L<Cwd|Cwd>


L<File::Basename|File::Basename>


L<File::Copy|File::Copy>


L<File::Find|File::Find>


L<File::Path|File::Path>


L<File::Spec|File::Spec>


L<File::Spec|File::Spec>


L<IO::File|IO::File>


L<IO::Seekable|IO::Seekable>


L<Time::Local|Time::Local>


=head1 AUTHOR


Ned Konz, <nedkonz@cpan.org>


File attributes code by Maurice Aubrey <maurice@lovelyfilth.com>

=head1 COPYRIGHT


Copyright (c) 2000-2003 Ned Konz. All rights reserved. This program is free
software; you can redistribute it and/or modify it under the same terms as
Perl itself.

=head1 SEE ALSO


L<Compress::Zlib>


L<Archive::Tar>


There is a Japanese translation of this
document at L<http://www.memb.jp/~deq/perl/doc-ja/Archive-Zip.html> that
was done by DEQ <deq@oct.zaq.ne.jp> . Thanks!