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


Hi

I am using the Zip::Archive module to create zip files. I have a question; how can i use this module to create zip files that do not preserve the folder structure of the files that are being zipped? Currently, the folder structure is getting preserved; i don't want that to happen.

Replies are listed 'Best First'.
Re: Question on Zip::Archive
by zek152 (Pilgrim) on Jun 28, 2010 at 20:23 UTC

    It will be helpful if you can show the code that leads to this issue, if that is not possible try to write some that will replicate the problem you are having. Also it would help us have a better idea of how you are trying to create the zip file. Something to look at is the CPAN docpage for the Archive::Zip module. Just something to try is using the extractMemberWithoutPaths operation to get the member from the zip file without any path information

      thanks for the reply. you had suggested an option how to not preserve paths while unzipping from Perl script; but i would likt to do the opposite. I would like to NOT preserve the folder structure while zipping itself; so if i create zip files programatically and give it to someone, and when that guy unzips it using Winzip, he should not get the folder structure back. I tried looking at the CPAN doc page for Zip::Archive; but it did not show me any option.

        The link is:Archive::Zip My suggestion is that if you couldnt figure out how to directly make the zip file without directory information you could make the zip file like you already have, then read it back without path information to make temp files without path information and then write those to the final zip file. It is a really cheesy hack, I know, but if you don't show us the code that gives you issues we can't help you as effectively.

        There is no module called "Zip::Archive" on cpan c_biswanath :)
Re: Question on Zip::Archive
by marinersk (Priest) on Jun 28, 2010 at 23:36 UTC
    As documented, use the second parameter on the addFile() method to force a different name, which (not documented) can include a name with no path. I leave it as an exercise for the reader to figure out how to determine the base filename without the path for any given file. Sample attached.
    #!/usr/bin/perl use Archive::Zip; use strict; my $TRUE = 1; my $FALSE = 0; foreach my $addtyp (qw /yespath nopath/) { # Hard-code test values my $os_nam = $^O; # Operating system name my $unisep = '/'; # *nix directory separato +r my $winsep = "\\"; # Windows directory sepa +rator my $subdnm = "sub1"; # Subdirectory name my $shtfnm = 'test.dat'; # Filename with no path my $zipfnm = "test-$addtyp.zip"; # ZIP filename my $zipobj = ''; # ZIP object pointer my $newflg = $TRUE; # Was this a new ZIP fi +le? # Set up test environment my $dirsep = $unisep; # Assum *nix until proven + otherwise if ($os_nam =~ /windows|win\d+/i) { # Appears to be Windows $dirsep = $winsep; } unlink $zipfnm; # Do not care if nonext +ant mkdir $subdnm; # Do not care if extant my $addfnm = "$subdnm$dirsep$shtfnm"; # Filename with path to a +dd open ADDFIL, ">$addfnm"; print ADDFIL "This is a test.\n"; close ADDFIL; # Perform test $zipobj = Archive::Zip->new(); if ($addtyp =~ /yespath/) { my $addret = $zipobj->addFile($addfnm); } else { my $addret = $zipobj->addFile($addfnm, $shtfnm); } my $wriret = $zipobj->writeToFileNamed($zipfnm); } exit;