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

Using PerlIO::gzip

by heigold1 (Acolyte)
on Jun 25, 2004 at 18:20 UTC ( [id://369701]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I have been trying to find examples of how to use PerlIO::gzip to make a gzip file out of a non-text file (i.e. a PDF or image file), and all the examples just have reading in GZIP files as input.

Does anyone know how to use PerlIO::gzip to make a UNIX gzip file (like a PDF) from a Windows Perl script?

Thanks in advance,

Brent.

Replies are listed 'Best First'.
Re: Using PerlIO::gzip
by PodMaster (Abbot) on Jun 25, 2004 at 18:46 UTC
    `perldoc PerlIO'
    I don't know if it'll work, but try something like this (untested):
    use strict; use warnings; my $infile = 'hi.txt'; my $outfile = 'hi.gz'; open my $in, '<:raw', $infile or die $!; open my $out, '>:gzip', $outfile or die $!; while(defined( my $read = read $in, my $s, 2048 ) ){ die "Error reading 2048: $!" if $read != 2048; print {$out} $s; } close $in; close $out;

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Hi, thank you so much for your response.

      The reason why I used PDF as an example was that I just wanted to create a GZIP file out of anything that wasn't a straight text or .dat file.

      So if you could provide an example of creating a GZIP file without using a text file that would be greatly appreciated.

      Brent.
        replace hi.txt with WHATEVER.WHATEVER or WHATEVER.pdf and there you go (mindblowing isn't it)
Re: Using PerlIO::gzip
by duff (Parson) on Jun 25, 2004 at 18:41 UTC

    Just open the file for writing with the :gzip IO layer enabled and it'll create a gzip file. As to "a UNIX gzip file" I haven't any idea what you mean. A gzip file is a gzip file no matter the OS.

Re: Using PerlIO::gzip
by Zaxo (Archbishop) on Jun 25, 2004 at 19:06 UTC

    PodMaster's solution is correct. Here is a tested version from the command line:

    $ ls -l T*.pdf -rw-rw-r-- 1 zaxo zaxo 362870 Sep 3 2002 The_Perl_Review_0_ +5.pdf -rw-rw-r-- 1 zaxo zaxo 352282 Jan 6 2003 The_Perl_Review_0_ +6.pdf -rw-rw-r-- 1 zaxo zaxo 263105 Jan 6 2003 The_Perl_Review_0_ +7.pdf $ perl -MPerlIO::gzip -e'for (@ARGV) {local $/ = \4096; open my $ih, " +<:raw", $_ or warn $! and next; open my $oh, ">:gzip", $_.".gz" or wa +rn $! and next; while (<$ih>) { print $oh $_ }}' T*.pdf $ ls -l T*.pdf.gz -rw-rw-r-- 1 zaxo zaxo 331242 Jun 25 14:57 The_Perl_Review_0_ +5.pdf.gz -rw-rw-r-- 1 zaxo zaxo 327834 Jun 25 14:57 The_Perl_Review_0_ +6.pdf.gz -rw-rw-r-- 1 zaxo zaxo 235150 Jun 25 14:57 The_Perl_Review_0_ +7.pdf.gz $ file T*.pdf.gz The_Perl_Review_0_5.pdf.gz: gzip compressed data, deflated, last modif +ied: Fri Jun 25 14:57:55 2004, os: Unix The_Perl_Review_0_6.pdf.gz: gzip compressed data, deflated, last modif +ied: Fri Jun 25 14:57:56 2004, os: Unix The_Perl_Review_0_7.pdf.gz: gzip compressed data, deflated, last modif +ied: Fri Jun 25 14:57:56 2004, os: Unix $
    The local $/ = \4096; statement makes perl read the input files in chunks of that size. That speeds things up quite a bit.

    Evidently PDF files are not too redundant to begin with. They don't gzip all that much tighter.

    After Compline,
    Zaxo

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-03-28 22:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found