Search code examples
perlziparchive

How to add a file to an archive using perl Archive::Zip?


I am using Archive::Zip to create archive files, I'd like to know if I can add a file to an already existing archive? looks like every time I call ->writeToFileNamed the file is truncated...


Solution

  • use strict;
    use warnings;
    use Archive::Zip;
    
    my $zip = Archive::Zip->new();
    #create your archive
    my $member = #"file you want to add to archive";
    $zip->addMember( $member );
    

    If you don't create your zip inside your script, then just "read" it and add your file...

    use warnings;
    
    use strict;
    
    use Archive::Zip qw( :ERROR_CODES );
    
    my $zip = Archive::Zip->new();
    
    $zip->read('c:\users\user\desktop\test.zip') == AZ_OK or die "read error\n";
    
    $zip->addFile('test.pl');
    
    $zip->overwrite()     == AZ_OK or die "write error\n";