Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
more useful options
 
PerlMonks  

Zipping the files

by nesanhere (Initiate)
on Aug 19, 2005 at 06:27 UTC ( [id://485047]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

I want to zip files (which has a specific extension eg: .pdf) from a folder; the files with same extension inside the subfolders should not be zipped

Replies are listed 'Best First'.
Re: Zipping the files
by reneeb (Chaplain) on Aug 19, 2005 at 06:34 UTC
    opendir(DIR,$dir) or die $!; my @files = map{$dir.'/'.$_}grep{$_ =~ /\.$extension$/}readdir(DIR); closedir DIR;

    or
    my @files = glob($dir.'/*.'.$extension);


    To zip files you can use Archive::Zip or Archive::Tar...
Re: Zipping the files
by Joost (Canon) on Aug 19, 2005 at 06:42 UTC
Re: Zipping the files
by Samy_rio (Vicar) on Aug 19, 2005 at 07:49 UTC

    Hi, Your question is not clear, but I have palced code for zip the files in the given path (as argument).

    use Archive::Zip; $path = $ARGV[0]; chdir ($path); my $zip = Archive::Zip->new(); #create object my $file = $1 if($path =~ /\\([^\\]+)$/); $zip->addTree( '.', "$file" ); $zip->writeToFileNamed( "$path.zip" ); #archive & compress print "Zip Process Over";

    Check whether it helps you.

    Regards,
    Velusamy R.

Re: Zipping the files
by Roger (Parson) on Aug 19, 2005 at 08:40 UTC
    I'd say that `zip filename.zip dir/*.pdf` will do the trick. Note that if you do not specify the -r switch, the zip program will not recurse into subdirectories. Ok, you do have zip installed, don't you? :-)

Re: Zipping the files
by radiantmatrix (Parson) on Aug 19, 2005 at 13:14 UTC

    You can use the command-line versions of zip or 7zip easily enough. On the off chance that this is part of a larger Perl-driven process, you have two approaches:

    system('zip',$zipfile_name,'*.pdf'); ## don't forget to check $? for errors

    Or, you can use something like:

    use Archive::Zip; my $zip = new Archive::Zip; for (glob '*.pdf') { my $member = $zip->addFile($_); $member->desiredCompressionMethod( COMPRESSION_DEFLATED ); } die "Can't write zip file" if $zip->writeToFileNamed('pdfs.zip') != AZ +_OK;

    This is untested, of course, but it should be basically correct.

    <-radiant.matrix->
    Larry Wall is Yoda: there is no try{} (ok, except in Perl6; way to ruin a joke, Larry! ;P)
    The Code that can be seen is not the true Code
    "In any sufficiently large group of people, most are idiots" - Kaa's Law

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://485047]
Approved by Nevtlathiel
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.