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

I have tried zipping a directory which contains sub directories and files and this is my code.

!/usr/bin/perl -w use strict; use List::Util qw(first); use Archive::Zip qw(:ERROR_CODES :CONSTANTS); my $input= 'D:\temp\sample'; my $outDir = 'D:\out\sample.zip'; my $obj = Archive::Zip->new(); $obj->addTree( $input ); # # Write the files to zip. if ($obj->writeToFileNamed($outDir) == AZ_OK) { # write to disk print "\n\nArchive created successfully!\n"; } else { print "Error while Zipping !"; }

When i execute this, the zip file ( sample.zip ) is created.

Always the unzipping is done manually selecting the option " Extract Here ".

When i unzip this , the directory bursts open putting all the subfolders and files at the same location( D:\out).

What i want is, i expect the output to be a unique folder ( D:\out\sample) So, what changes need to be done in my code. Pls help me.

Replies are listed 'Best First'.
Re: How to zip the directory in a required format
by Athanasius (Archbishop) on Oct 28, 2015 at 06:28 UTC

    Hello sanvin,

    Always the unzipping is done manually selecting the option " Extract Here ".

    When I run your script on my system (Windows 8.1), I get this zip file: ...\out\sample.zip. Then when I right click on it in Windows Explorer, and select the Extract All... option, it gives me:

    Select a Destination and Extract Files

    Files will be extracted to this folder:

    ...\out\sample

    Then when I click Extract, it extracts the contents of sample.zip into the out\sample subfolder, creating additional subfolders as needed, and unzips files into their correct locations.

    So, how are you extracting the zip file?

    Update: If you instead use the excellent 7-zip, choose the Extract to "sample\" option.

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      I use 7 Zip and always select the option " Extract Here". This can't be changed. So, i am looking into ways where the code can be modified
Re: How to zip the directory in a required format
by Corion (Patriarch) on Oct 28, 2015 at 09:50 UTC

    Please read the Archive::Zip documentation for addTree. It documents how to give an added directory another name.