Archive::StringToZip - Transforms a string to a zip


Archive-StringToZip documentation Contained in the Archive-StringToZip distribution.

Index


Code Index:

NAME

Top

Archive::StringToZip - Transforms a string to a zip

SYNOPSIS

Top

A simple wrapper around Archive::Zip for transforming a string to a compressed zip returned as a filehandle. Inherits all of Archive::Zip's methods.

This module operates in memory and avoids the use of temporary files. If you want to send the contents of a zip to standard output, for example, you might find this module useful.

USAGE

Top

OO-style:
 use Archive::StringToZip;

 my $stz = Archive::StringToZip->new();
 my $zip = $stz->zipString($string,$filename) 
            or die "Zipping string failed";

or

Procedural-style
 use Archive::StringToZip qw(zipString);

 my $zip = zipString($string,$filename) 
            or die "Zipping string failed";

or even

Classy-style
 use Archive::StringToZip;

 my $zip = Archive::StringToZip::zipString($string,$filename) 
            or die "Zipping string failed";

Then return a zip file to a browser download
 print qq~Content-Type: application/x-zip\nContent-Disposition: attachment; Encoding: base64; filename="${filename}.zip"\n\n~;
 print $zip;

METHODS

Top

new
 my $stz = Archive::StringToZip->new();

Constructs a new string zipping object

zipString
 my $zip_content = $stz->zipString($string, $filename);

or

 my $zip_content = zipString($string, $filename);

First argument is compulsory; second is optional. The default filename is file.txt if the second argument is undefined.

Converts a string ($string) into a file ($filename) in a compressed zip file format which is returned as a string.

Returns a false value on failure.

Sets binmode STDOUT by default to help prevent Win32 systems being confused about your output.

DEPENDENCIES

Top

Archive::Zip and IO::String.

MOTIVATION

Top

We had users wanting to carry bulk exports of data from a database representing their online store's order history. After slurping the data out and converting it, the resulting CSV was pretty big, so compression was the way forward, et voila! Archive::StringToZip was born.

AUTHORS

Top

Robbie Bow and Tom Hukins sometime in spring 2006.

BUGS

Top

Please report any bugs or feature requests to

bug-archive-stringtozip at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Archive-StringToZip. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

ALTERNATIVES

Top

IO::Compress::Zip, unstable at the time of writing.

LICENSE

Top

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


Archive-StringToZip documentation Contained in the Archive-StringToZip distribution.

package Archive::StringToZip;

use strict;
use vars qw(@ISA @EXPORT_OK $VERSION);

use Carp qw(croak);
use IO::String ();
use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
use base qw(Archive::Zip);

require Exporter;
@ISA = qw(Exporter Archive::Zip::Archive Archive::Zip);
@EXPORT_OK = qw(zipString);


$VERSION = '1.03';

sub zipString {
    my $self = ref($_[0]) ? shift : __PACKAGE__->new();
    my ($string, $filename) = @_;

    croak 'Cannot archive an undefined string' unless defined $string;
    $filename = 'file.txt' unless $filename;

    my $SH = IO::String->new();
    
    my $member = $self->addString($string, $filename);
    $member->desiredCompressionMethod(COMPRESSION_DEFLATED);
    $member->desiredCompressionLevel( 9 );

    my $status = $self->writeToFileHandle($SH);
    die $! if $status != AZ_OK;

    binmode STDOUT;     # necessary for Win32 and does no harm on *nix
    $SH->setpos(0);
    return do { local $/ = undef; $SH->getline };
}

1;

__END__