thclpr
thclpr

Reputation: 5938

Archive::Zip duplicating added files - How do i update files inside a zip correctly?

I have a zip file that i must update an file under a folder , the following block works "well", it add correctly the file node.ndf under the path specified.

But if i run it for the second time or more , it duplicates the file under the folder

as example:


first time run:

file.zip content:

manifest.v3

ns/adapt/node.ndf


second time run:

manifest.v3

ns/adapt/node.ndf

ns/adapt/node.ndf


and so on...

Here is the code im currently using:

$obj = Archive::Zip->new();  

$status = $obj->read($file); 

if ($status != AZ_OK) {
die('Error in file!');
    } else {
        @files = ('node.ndf');

       foreach $filea (@files) {
    $obj->addFile($filea); 
    $obj->addTreeMatching( '.', 'ns/adapt/', 'node.ndf' );
   }

   if ($obj->overwrite() != AZ_OK) {  
    print "Error in archive creation!";
    exit;

    } else {
    print "Archive created successfully!";
    }
unlink('node.ndf');
 }

Anyone know the correct usage of Archive::Zip in order to update a file under a folder structure at the zip file?

Thanks in advance.

Upvotes: 0

Views: 286

Answers (1)

Borodin
Borodin

Reputation: 126722

You can either

$obj->removeMember('ns/adapt/node.ndf');
$obj->addFile('node.ndf', 'ns/adapt/node.ndf');

or, if your node.ndf file has changed size or modification time, you can do both in a single step with

$obj->updateMember('ns/adapt/node.ndf', 'node.ndf');

Upvotes: 1

Related Questions