Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I gzip compress a string in Perl

I have a string that I would like to compress using GZip before inserting into a database as a BLOB. However, it seems that the built in GZip Perl modules are all designed to compress files, not inline strings.

In PHP I would accomplish this with $compressed = gzcompress($string)

What is a Perl equivalent of PHP's gzcompress()?

like image 722
Dave Avatar asked Jan 25 '11 05:01

Dave


3 Answers

There is a gzip function in IO::Compress::Gzip that should do what you need. Something like this:

gzip \$input => \$output

would be more or less equivalent to your PHP. Note the use of references to scalars if you want to do everything in memory buffers, if you use plain scalars then gzip will think they're filenames.

like image 193
mu is too short Avatar answered Nov 02 '22 21:11

mu is too short


Perhaps http://perldoc.perl.org/IO/Compress/Gzip.html ?

like image 39
Tony Delroy Avatar answered Nov 02 '22 23:11

Tony Delroy


use Compress::Zlib;
#then $out = compress($in) or $out = uncompress($in)
like image 1
Joyer Avatar answered Nov 02 '22 21:11

Joyer