Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Multiple copies of the same file in same ZIP archive

by bofh_of_oz (Hermit)
on May 31, 2005 at 18:23 UTC ( [id://462149]=perlquestion: print w/replies, xml ) Need Help??

bofh_of_oz has asked for the wisdom of the Perl Monks concerning the following question:

In a script i'm working on, I create a ZIP archive and add a file to it. Or, if the zip file exists, just add the file to it. I have noticed a strange thing: I tell the script to add the same file to the ZIP archive many times, then open the archive with Winzip and what do I see? There are indeed many files inside - with the same filename! When I try to rename/delete those files, each of them is treated independently of others.

Even though this behaviour seems logical to me, I cannot reproduce the same functionality when I use PKZIP/Winzip/any other utility... Is that an 'undocumented feature' of Perl or what?

The code used is here:

use Archive::Zip qw(:CONSTANTS :ERROR_CODES); use IO::Scalar; my $fname = 'file1.txt'; my $zipfile = 'filename.zip'; my $zip = Archive::Zip -> new(); if (-e $zipfile) { $zip ->read($zipfile); } my $member = $zip->memberNamed( $fname ); if ($member eq undef) { $member = $zip->addFile( $fname ); } $member -> desiredCompressionLevel( 9 ); $fname = 'autotransParams.txt'; $member = $zip->memberNamed( $fname ); if ($member eq undef) { $member = $zip->addFile( $fname ); } $member -> desiredCompressionLevel( 9 ); my $status = $zip -> overwriteAs($zipfile);

--------------------------------
An idea is not responsible for the people who believe in it...

Replies are listed 'Best First'.
Re: Multiple copies of the same file in same ZIP archive
by thundergnat (Deacon) on May 31, 2005 at 19:11 UTC

    Is that an 'undocumented feature' of Perl or what?

    Actually, that appears to be a documented feature of Archive::Zip

    Selected quote from Archive::Zip::FAQ.


    Duplicate files in Zip?

    Q: Archive::Zip let me put the same file in my Zip twice! Why don't you prevent this?

    A: As far as I can tell, this is not disallowed by the Zip spec. If you think it's a bad idea, check for it yourself:

    $zip->addFile($someFile, $someName) unless $zip->memberNamed($someNa +me);

    I can even imagine cases where this might be useful (for instance, multiple versions of files).


    See perldoc Archive::Zip::FAQ for further info.

Re: Multiple copies of the same file in same ZIP archive
by marnanel (Beadle) on May 31, 2005 at 19:13 UTC
    Well, the original file format specification didn't say anything about multiple filenames sharing the same name, so it's not actually contrary to the spec. It's easy enough to check for an existing file and delete it if found before adding the new one: after all, that's what WinZip and friends are doing behind the scenes anyway.
Re: Multiple copies of the same file in same ZIP archive
by PodMaster (Abbot) on May 31, 2005 at 18:38 UTC
    Is that an 'undocumented feature' of Perl or what?
    Definetly no. If Winzip/pkzip/whatever can read such archives, then its probably an unused/unadvertised and/or unintentional feature of the format.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://462149]
Approved by injunjoel
Front-paged by holli
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-19 06:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found