Search the web
Sign In
New User? Sign Up
perl-python
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want to share photos of your group with the world? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
gzip a file   Message List  
Reply | Forward Message #99 of 127 |
Re: [perl-python] gzip a file

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


Fri Sep 2, 2005 10:07 am

orionrobots@...
Send Email Send Email

Forward
Message #99 of 127 |
Expand Messages Author Sort by Date

20050830 Here is a example of how to decompress a gzip file using Python. # -*- coding: utf-8 -*- # Python import gzip inF =...
xah lee
p0lyglut
Offline Send Email
Sep 2, 2005
6:25 am

... 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,...
danny staple
orionrobots@...
Send Email
Sep 2, 2005
3:55 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help