Net::SSH::Perl::Comp::Zlib - Wrapper for SSH Zlib Compression


Net-SSH-Perl documentation Contained in the Net-SSH-Perl distribution.

Index


Code Index:

NAME

Top

Net::SSH::Perl::Comp::Zlib - Wrapper for SSH Zlib Compression

SYNOPSIS

Top

    use Net::SSH::Perl::Comp;
    my $comp = Net::SSH::Perl::Comp->new('Zlib');
    print $comp->compress($data);

DESCRIPTION

Top

Net::SSH::Perl::Comp::Zlib subclasses Net::SSH::Perl::Comp to provide Zlib compression support for Net::SSH::Perl. To do so it wraps around Compress::Zlib, an XS hook into the zlib library.

Read through the Net::SSH::Perl::Comp docs for usage information.

AUTHOR & COPYRIGHTS

Top

Please see the Net::SSH::Perl manpage for author, copyright, and license information.


Net-SSH-Perl documentation Contained in the Net-SSH-Perl distribution.

# $Id: Zlib.pm,v 1.3 2008/10/02 20:46:17 turnstep Exp $

package Net::SSH::Perl::Comp::Zlib;

use strict;
use Carp qw( croak );
require Compress::Zlib;

use Net::SSH::Perl::Comp;
use base qw( Net::SSH::Perl::Comp );

sub init {
    my $comp = shift;
    my($level) = @_;
    my($err);

    ($comp->{d}, $err) = Compress::Zlib::deflateInit({ Level => $level });
    croak "Can't create outgoing compression stream"
        unless $err == Compress::Zlib::Z_OK();

    ($comp->{i}, $err) = Compress::Zlib::inflateInit();
    croak "Can't create incoming compression stream"
        unless $err == Compress::Zlib::Z_OK();
}

sub compress {
    my $comp = shift;
    my($data) = @_;
    my $d = $comp->{d};
    my($compressed, $err);
    {
        my($output, $out);
        ($output, $err) = $d->deflate($data);
        last unless $err == Compress::Zlib::Z_OK();
        ($out, $err) = $d->flush(Compress::Zlib::Z_PARTIAL_FLUSH());
        last unless $err == Compress::Zlib::Z_OK();

        $compressed = $output . $out;
    }
    croak "Error while compressing: $err" unless defined $compressed;
    $compressed;
}

sub uncompress {
    my $comp = shift;
    my($data) = @_;
    my $i = $comp->{i};
    my($inflated, $err);
    {
        my($out);
        ($out, $err) = $i->inflate($data);
        last unless $err == Compress::Zlib::Z_OK();

        $inflated = $out;
    }
    croak "Error while inflating: $err"
        unless defined $inflated;
    $inflated;
}

1;
__END__