On 02/09/05, xah lee <xah@...> wrote:
> 20050830
>
> Here is a example of how to decompress a gzip file using Python.
>
> # -*- coding: utf-8 -*-
> # Python
>
> import gzip
> inF = gzip.GzipFile("/Users/t/access_log.1.gz", 'rb');
> s=inF.read()
> inF.close()
>
> outF = file("/Users/t/access_log.1", 'wb');
> outF.write(s)
> outF.close()
>
>
> Here is a example of compressing a gzip file using Python.
>
> # -*- coding: utf-8 -*-
> # Python
>
> import gzip
>
> inF = file("x.txt", 'rb');
> s=inF.read()
> inF.close()
>
> outF = gzip.GzipFile("x.txt.gz", 'wb');
> outF.write(s)
> outF.close()
>
>
> For more detail, see
> http://python.org/doc/2.4.1/lib/module-gzip.html
>
> Perl does not come with a gzip module bundled, but one can easily call
> the unix gzip with qx. (assuming you are on unix)
>
> # perl
>
> qx(gzip x.txt);
> qx(gzip -d x.txt.gz);
>
>
> Several module are available online to do compression. IO::Zlib,
> PerlIO::gzip.
>
> ----------
> this post is archived at:
> http://xahlee.org/perl-python/gzip.html
>
> ☄
>
You could shorten the Perl unix calls by using the backtick operator:
`gzip x.txt`
`gzip -d x.txt.gz`
And for the same behaviour as the Python module, where you actually
have the decompressed stream and not a temporary file, you would want
to use (when compressing or decompressing) the -c switch with gzip.
This instructs it to put its output onto the console (on std::out)
which in this case you would be capturing by simply assigning the
return from the backtick.
my $compressedFile = `gzip -c x.txt`;
my $decompressedFile = `gzip -dc x.txt`;
I would probably however prefer to use a real Perl module for the sake
of portability though. After all - you are also importing a module
into Python. Python has the advantage here of a more comprehensive
core module library, where in Perl you would need to install it.
Orion
--
http://orionrobots.co.uk - Build Robots