Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Zipping Files

by VicBalta (Scribe)
on Sep 26, 2001 at 18:15 UTC ( #114815=perlquestion: print w/replies, xml ) Need Help??

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

Hi

My question that I have is about zipping files. I want to make a program that will zip a text file. I have read some things on it and I think that there is a zip module for perl. But I also read that you can use winzip to get this done through your code. I am working on windows 2000. I was wondering which one would be better to use and would do a better job or compressing the files. Also how about unzipping files also? If any one has some experience with this and knows a good way of getting this done your help would be greatly appreciated. any examples would be great

Thank You

Replies are listed 'Best First'.
Re: Zipping Files
by tachyon (Chancellor) on Sep 26, 2001 at 18:31 UTC
Re: Zipping Files
by Rhandom (Curate) on Sep 26, 2001 at 18:34 UTC
    For compressing files, I would have to recommend IO::Zlib. IO::Zlib provides a IO::Handle style interface for reading from and writing to zipped files. These files are compatible with gunzip on unix and are able to be decompressed by winzip as well.

    I have used IO::Zlib (which is actually a front end to Compress::Zlib) very successfully under unix. I do not know what it would take to get it to run under windows though, but in theory it should be possible.

    Additionally you will want to look at Archive::Tar. Archive::Tar will put all of the files into one common file, and if you have Compress::Zlib installed will compress them for you. If you save the files out with an extension of .tgz, winzip will be able to extract them (it can also do the .tar.gz extension but it is more of a hassle on win32 systems).

    Again, I know that the libz libraries can work on windows, but it may be a bit of work to get them compiled.

    my @a=qw(random brilliant braindead); print $a[rand(@a)];
      On NT I have sucessfully used Archive::Tar and Compress::Zlib to make a .tar.gz file. .tar.gz files can be read by most recent versions of WinZip, and most *nix systems without a problem.

      In general, but not always, they tend to be smaller than zip files. I don't know what the conditions are that make gzip more efficient, I assume someone does, but I use .tar.gz by default as I find it's better most of the time.

      I think that Block Sorting Compression BZip2 is more efficient than either zip or Gzip on text files, but there wasn't a PPD for it for NT last time I looked, and it won't build from CPAN unless you have a compiler, and the bzip pieces to hand.

      Though it's not strictly Perl, but RedHat CygWin provides command line versions of zip, unzip, tar, gzip, bzip2 and lots of other cool *nix tools (including Perl), already compiled for Windows, and these all work okay too.

Re: Zipping Files
by RhetTbull (Curate) on Sep 26, 2001 at 20:01 UTC
    Echoing what's already been said here, I agree that either Archive::Zip or Compress::Zlib and Archive::Tar are your best bets. If you need to compress multiple files into one zip archive that is readable on Windows, I would recommend Archive::Zip. However, if you only need to compress one file (and if that one file happens to be the output of your program) my recommendation is IO::Zlib. This is great module that lets you create an IO handle that reads or writes a gzip compressed file. Here are some snippets to get your started:

    Using Archive::Zip:

    #!/bin/perl -w use warnings; use strict; use Archive::Zip qw( :ERROR_CODES :CONSTANTS); #almost verbatim from the Archive::Zip docs: my $zip = Archive::Zip->new(); my $member = $zip->addFile('t.out'); die 'Error writing file' if $zip->writeToFileNamed('foo.zip') != AZ_OK +;
    using IO::Zlib:
    #!/bin/perl -w use warnings; use strict; require 5.004; #using TIEHANDLE interface use IO::Zlib; tie *FILE, 'IO::Zlib', "foo.txt.gz", "wb"; #do your stuff here #everything written to FOO gets compressed and stored in foo.gz print FILE "hello world\n";
    If you want to use Compress::Zlib and Archive::Tar then just read the docs but I doubt it's very difficult. The above two are certainly straightforward and easy.
Re: Zipping Files
by Anonymous Monk on Sep 26, 2001 at 22:39 UTC
    Currently I am using Winzip and Perl to automagically archive and move the large web logs I work with. Winzip has an excellent add-on utility called WzCline that allows you to run Winzip via the command line. The Perl then generates the commands, among other things, and then uses the system command to run the Winzip commands. The add-on has all the options regular Winzip has, and I have yet to have any problems (although by saying this I know I'm asking for it).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2021-04-23 06:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?