Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Zip/compress files and directories

by g_speran (Scribe)
on Jan 31, 2009 at 02:34 UTC ( [id://740370]=perlquestion: print w/replies, xml ) Need Help??

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

Hello All,
Please forgive my ignorance in asking this question. I have never made a zip or a compressed file using perl. I am seek perl wisdom on sample code that would zip / compress files and directories.


I Have written a script that creates many files, 46, to be exact. These files are created in the same directory that the script is ran from. The files will look like AAA_file1, AAA_file2, AAA_file3, etc. All beginning with AAA. In addition, the scrip also creates a directory with two sub directories and files located in the directory and the sub directories which also need to be added to the zip / compress file. There may be other files and directories in the current location but will not be needed in the zip / compressed file. The directory name that will be created is called "support"

Any offering / assistance is greatly appreciated.

Gary

Replies are listed 'Best First'.
Re: Zip/compress files and directories
by mr_mischief (Monsignor) on Jan 31, 2009 at 05:53 UTC
    Take a look at the docs for Archive::Zip (or Archive::Tar) if you want to make the archive from Perl. If you'd rather wrap an existing tool, then system is a good option.
Re: Zip/compress files and directories
by jethro (Monsignor) on Jan 31, 2009 at 03:37 UTC

    It makes no sense replicating the complex functionality of a pack program within perl. Just use the perl builtin system() to call whatever zip utility you have on your computer.

    If you are on windows you might have to search for a packer where you can give all parameters on the command line instead of interactively

Re: Zip/compress files and directories
by sathiya.sw (Monk) on Jan 31, 2009 at 12:14 UTC
Re: Zip/compress files and directories
by Plankton (Vicar) on Jan 31, 2009 at 08:09 UTC
    have you tried ...
    `find . -name "AAA*" -print | zip yourzipfile.zip -\@`;
Re: Zip/compress files and directories
by ikegami (Patriarch) on Jan 31, 2009 at 03:18 UTC
    What have you tried? What problem are you having?
Re: Zip/compress files and directories
by g_speran (Scribe) on Jan 31, 2009 at 15:31 UTC
    OK. So I hav made headway. I am now using Archive::Zip. I have been able to add the files in the local directoy to the archive, but have not been able to add a subdirectoy in the current directory to the archive. Here is the code I have. Any assistance?
    sub compress { @add_array=@{ $_[0] }; $obj = Archive::Zip->new(); # new instance foreach (@add_array) { $obj->addFile($_); # add files } if ($obj->writeToFileNamed("$ZIP") != AZ_OK) { # write to disk print "Error in archive creation!"; } else { print "Archive created successfully!"; } } opendir(DIR, "."); @files = grep(/^AAA_/,readdir(DIR)); closedir(DIR); push(@files,"./support"); # <== Seems not to be working compress(\@files);

      You may find the documentation helpful. It suggests to me that you might need the addTree() method.

      Cheers,

      JohnGG

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-29 07:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found