Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Integrate Compress::Zlib seamlessly w/no compression?

Reply
Thread Tools

Integrate Compress::Zlib seamlessly w/no compression?

 
 
peter.j.torelli@gmail.com
Guest
Posts: n/a
 
      09-08-2006
Hello,

I use Compress::Zlib extensively for reading files, but I always run
into this problem when writing:

If the user does not want to write compressed output, I can't use
Compress::Zlib for writing. I tried all the Zlib options to turn off
compression, and it always inserts a binary header, even with no
compression.

Since the read/write/close methods for the object are not the same as
an uncompressed filehandle, I can't just use a different open method
for the same variable. Instead I end up having to use open() and a
pipe to gzip if compression is desired so that the same filehandle can
be used without a ton of conditionals in the code.

So the simple question is: how do I turn off all compression and
headers when I use gzopen() and just write a plain text file?

(Note: I already played with Compress::Zlib::Z_NO_COMPRESSION, and
-WindowBits=>-MAX_WBITS, as well as "w" vs. "wb".)

Thanks,
Peter

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      09-09-2006

A. Sinan Unur wrote:
> http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in news:1157745641.539097.116200
> @d34g2000cwd.googlegroups.com:
>
> > Since the read/write/close methods for the object are not the same as
> > an uncompressed filehandle, I can't just use a different open method
> > for the same variable. Instead I end up having to use open() and a
> > pipe to gzip if compression is desired so that the same filehandle can
> > be used without a ton of conditionals in the code.
> >
> > So the simple question is: how do I turn off all compression and
> > headers when I use gzopen() and just write a plain text file?

>
> But, that is the wrong question to ask. You can just write two dispatcher
> class that have the identical interface, and hide the different
> implementations of read/write/close methods behind these objects'
> interfaces.


Or write a very thin shim that presents an interface like
Compress::Zlib's read/write/close but just passes them strait through
and use one of these objects instead.

Of course you could just us IO::Zib which gives Compress::Zlib a more
filehandle-like interface.

There's also PerlIO::gzip but I don't know if this uses zlib or an
external gzip process.

 
Reply With Quote
 
 
 
 
Paul Marquess
Guest
Posts: n/a
 
      09-11-2006
Brian McCauley wrote:

>
> A. Sinan Unur wrote:
>> (E-Mail Removed) wrote in news:1157745641.539097.116200
>> @d34g2000cwd.googlegroups.com:
>>
>> > Since the read/write/close methods for the object are not the same as
>> > an uncompressed filehandle, I can't just use a different open method
>> > for the same variable. Instead I end up having to use open() and a
>> > pipe to gzip if compression is desired so that the same filehandle can
>> > be used without a ton of conditionals in the code.
>> >
>> > So the simple question is: how do I turn off all compression and
>> > headers when I use gzopen() and just write a plain text file?


There is no way to suppress the gzip header with the gzopen interface, even
when you use Z_NO_COMPRESSION.

>> But, that is the wrong question to ask. You can just write two dispatcher
>> class that have the identical interface, and hide the different
>> implementations of read/write/close methods behind these objects'
>> interfaces.

>
> Or write a very thin shim that presents an interface like
> Compress::Zlib's read/write/close but just passes them strait through
> and use one of these objects instead.
>
> Of course you could just us IO::Zib which gives Compress::Zlib a more
> filehandle-like interface.
>
> There's also PerlIO::gzip but I don't know if this uses zlib or an
> external gzip process.


IO::Zlib or PerlIO::gzip are the best approaches to use at the moment.

if (want compressed file) {
$fh = IO::Zlib->new($filename, "wb9");
}
else {
$fh = open ">$filename";
}

die "error ... error"
unless $fh;

If you want to get into manipulating the gzip header, you could use
IO::Compress::Gzip, but that's still beta code.

Paul

 
Reply With Quote
 
peter.j.torelli@gmail.com
Guest
Posts: n/a
 
      09-13-2006
Paul,

I really like your solution, it is very clean and fits with my style.
Thank you.

The idea of writing another abstraction layer to handle this simple
case (IMHO) is preposterous as I would have to tote this baggage around
indefinitely. This sol'n adds a few extra lines of code, but that's
way cleaner than supporting another object.

--Peter


Paul Marquess wrote:
> IO::Zlib or PerlIO::gzip are the best approaches to use at the moment.
>
> if (want compressed file) {
> $fh = IO::Zlib->new($filename, "wb9");
> }
> else {
> $fh = open ">$filename";
> }
>
> die "error ... error"
> unless $fh;
>
> If you want to get into manipulating the gzip header, you could use
> IO::Compress::Gzip, but that's still beta code.
>
> Paul


 
Reply With Quote
 
peter.j.torelli@gmail.com
Guest
Posts: n/a
 
      09-18-2006

I understand that it is a small amount of code, and amortized over many
calls using the conditional becomes even more cost-effective in terms
of typing. However in the environment I work in, the cost of managing
another module adds to the complexity of our release environment. And
since I'll only be calling it a few times in each monolithic perl
script (that's how we write; i know, i know...), the conditional suits
my needs. I should have defined "preposterous" to mean "preposterous
in MY particular environment".

Thanks, Sinan.

--P

A. Sinan Unur wrote:
> (E-Mail Removed) wrote in
> news:(E-Mail Removed) ups.com:
>
> [ Please do not top-post ]
>
> > Paul Marquess wrote:
> >> IO::Zlib or PerlIO::gzip are the best approaches to use at the
> >> moment.
> >>
> >> if (want compressed file) {
> >> $fh = IO::Zlib->new($filename, "wb9");
> >> }
> >> else {
> >> $fh = open ">$filename";
> >> }

>
> ...
>
> > I really like your solution, it is very clean and fits with my style.
> > Thank you.
> >
> > The idea of writing another abstraction layer to handle this simple
> > case (IMHO) is preposterous

>
> Hmmm ...
>
> #!/usr/bin/perl
>
> package My::Open;
>
> use strict;
> use warnings;
>
> sub open {
> my ($fn, $mode, $compress) = @_;
>
> if ( $compress ) {
> require IO::Zlib;
> return IO::Zlib->new($fn, $mode) if $compress;
> }
> else {
> open my ($fh), $mode, $fn;
> return $fh;
> }
> }
>
> package main;
>
> my $fh1 = My::Open:pen('test1.gz', 'wb', 1)
> or die $!;
>
> print $fh1 "This is a test\n" or die $!;
>
> close $fh1 or die $!;
>
> my $fh2 = My::Open:pen('test2.txt', '>')
> or die $!;
>
> print $fh2 "This is a test\n" or die $!;
>
> close $fh2 or die $!;
>
> __END__
>
> It all depends on how many times you need to write that conditional, or
> if other programs could use such functionality.
>
> --
> A. Sinan Unur <(E-Mail Removed)>
> (remove .invalid and reverse each component for email address)
>
> comp.lang.perl.misc guidelines on the WWW:
> http://augustmail.com/~tadmc/clpmisc...uidelines.html


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Integrate AdvaTel to Cisco CallManager MdV Cisco 1 08-29-2005 06:38 PM
Moving between webpages seamlessly using ajax? James Yong Java 0 08-23-2005 05:36 PM
Can Cisco Secure for UNIX integrate with Active Directory???! m Cisco 1 06-30-2005 03:25 AM
zip / unzip uploaded files seamlessly bradley ASP .Net 2 06-10-2005 01:26 PM
Can you properly integrate PHP and Perl? Sarah E. Green Perl 1 08-10-2003 03:18 AM



Advertisments