Posted By

icecreamboyy on 02/24/11


Tagged


Versions (?)

Perl Zipz


 / Published in: Perl
 

  1. # zip.pl
  2. # Author: Wraithnix
  3. use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
  4.  
  5. if (scalar(@ARGV) < 2){die "Zip.pl\nAuthor:Wraithnix\nUsage: zip.pl <c=create, x=extract> <filename> <files>\n";};
  6.  
  7. my $opt = shift(@ARGV);
  8. my $ofile = shift(@ARGV);
  9.  
  10. # extract a zip file
  11. if($opt=~/x/i){
  12. print "Zip.pl\nAuthor:Wraithnix\n";
  13. print "Extracting $ofile...\n";
  14. $zip = Archive::Zip->new();
  15. die 'Error reading zip file.' if $zip->read( $ofile ) != AZ_OK;
  16. my @members = $zip->members();
  17. foreach $element(@members)
  18. {
  19. print "$element\n";
  20. $zip->extractMember($element);
  21. }
  22. print "Done!\n";
  23. }
  24.  
  25. # create a zip file
  26. if($opt=~/c/i){
  27. print "Zip.pl\nAuthor:Wraithnix\n";
  28. print "Creating $ofile...\n";
  29. my $zip = Archive::Zip->new();
  30. foreach my $memberName (map { glob } @ARGV)
  31. {
  32. if (-d $memberName )
  33. {
  34. print "Adding $memberName\n";
  35. warn "Error adding directory $memberName\n"
  36. if $zip->addTree( $memberName, $memberName ) != AZ_OK;
  37. }
  38. else
  39. {
  40. print "Adding $memberName\n";
  41. $zip->addFile( $memberName )
  42. or warn "Error adding file $memberName\n";
  43. }
  44. }
  45. die 'Write error.' if $zip->writeToFileNamed( $ofile ) != AZ_OK;
  46. print "Done!\n";
  47. }

Report this snippet  

You need to login to post a comment.