Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Zip Enlightenment

by SamCG (Hermit)
on Jan 21, 2005 at 20:38 UTC ( #424125=perlquestion: print w/replies, xml ) Need Help??
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:/"; tie FIL, 'Tie::Gzip'; print FIL "This is a zipped file";



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( '', '' ); die 'error' unless $zip->writeToFileNamed('') == 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).


      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".

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://424125]
Approved by Limbic~Region
[atcroft]: Lady_Aleena: So you are shoving the disparate data into a structure and returning the structure, so you can run it through a (probably simpler) routine that displays only?
[Lady_Aleena]: atcroft, that is the goal.
[Lady_Aleena]: atcroft, this is the last of the printing modules I am going to make stop printing, I think.
[Lady_Aleena]: It will leave me with 8 out of 138 modules that print something.

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2017-05-29 02:37 GMT
Find Nodes?
    Voting Booth?