#!/usr/bin/perl use warnings; use strict; use CGI::Simple; use IO::Compress::Zip qw/$ZipError/; use Encode qw/encode/; my @lines = (qw/ Hello World Foo Bar /, "\N{U+1F42A}"); my $CRLF = "\r\n"; my $encoding = "UTF-8"; # or maybe "CP1252" for Windows my $zipdata; my $z = IO::Compress::Zip->new(\$zipdata, Name => "Filename.txt" ) or die "zip failed: $ZipError\n"; for my $line (@lines) { $z->print( encode($encoding, $line.$CRLF, Encode::FB_CROAK|Encode::LEAVE_SRC) ); } $z->close(); my $q = CGI::Simple->new(); print $q->header( -type => 'application/zip', -Content_Disposition => qq{attachment; filename="Download.zip"}, -Content_Length => length($zipdata) ); binmode STDOUT; # just to play it safe print $zipdata;