http://www.perlmonks.org?node_id=424125

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

Wise monks,

I'm looking for a way to zip files with perl. I've checked out Tie::gzip, but the documentation isn't entirely clear to me (doing the stupidly obvious cut-and-paste from the POD, and then trying to write to a file, rewards me with a "cannot modify constant item in tie" error which leads me to believe I'm misunderstanding something completely).

I could give up and use a command-line utility from perl easily enough. Trouble is, I don't really want to rely on some 3rd-party utility. Also, I like learning things, and this seems like a good opportunity ;).

Okay, I'm going to post my first attempts at test code. Try not to laugh (well, you can if you want, just don't post laughter).

#!perl -w ## Windows XP box, perl 5.8.something use strict; require Tie::Gzip; open FIL, ">C:/test.zip"; tie FIL, 'Tie::Gzip'; print FIL "This is a zipped file";

Thanks,

SamCG

Replies are listed 'Best First'.
Re: Zip Enlightenment
by idsfa (Vicar) on Jan 21, 2005 at 21:44 UTC

    ZIP is not the same as gzip. If you want ZIP files, you should be using Archive::Zip:

    use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); my $zip = Archive::Zip->new(); my $member = $zip->addDirectory( 'dirname/' ); $member = $zip->addString( 'This is a test', 'stringMember.txt' ); $member = $zip->addFile( 'xyz.pl', 'AnotherName.pl' ); die 'error' unless $zip->writeToFileNamed('someZip.zip') == AZ_OK;

    The FAQ is helpful as well.


    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. -- Cyrus H. Gordon
Re: Zip Enlightenment
by graff (Chancellor) on Jan 22, 2005 at 08:28 UTC
    The first reply raises a good point -- your terminology seems a bit confused.

    If you really do want to use GNU gzip-style compression to read and/or write files with gzip-compressed data, the easiest way to do this (IMO) is with PerlIO::gzip -- which makes use of the Perl 5.8.x "IO-layers":

    #!/usr/bin/perl -w # (might as well make it "cross-platform") require 5.008 require PerlIO::gzip; open FIL, ">:gzip", "C:/test.gz" or die "oops: $!"; print FIL "This behaves like any other file handle, but output gets gz +ipped\n"; close FIL;
    Note that PerlIO::gzip is not (yet) part of the Perl 5.8.x standard distribution (I wish it was) -- you have to get it from CPAN.

    UPDATE: (2010-10-18) It seems that PerlIO::gzip should be viewed as superseded by PerlIO::via:gzip. (see PerlIO::gzip or PerlIO::via::gzip).

      Thanks...

      I was (kind of) aware that gzip was different from zip -- the request actually came from a third party, who has now said standard zipping (as opposed to gzipping) is fine.

        Actually Gzip is the standart, since ZIP was built over gzip.

        Graciliano M. P.
        "Creativity is the expression of liberty".