Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Zipping the files

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

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 Joost (Canon) on Aug 19, 2005 at 10:42 UTC
Re: Zipping the files
by reneeb (Chaplain) on Aug 19, 2005 at 10: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 Samy_rio (Vicar) on Aug 19, 2005 at 11: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 radiantmatrix (Parson) on Aug 19, 2005 at 17: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
Re: Zipping the files
by Roger (Parson) on Aug 19, 2005 at 12: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? :-)

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
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2025-01-15 18:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (49 votes). Check out past polls.