UTF8 is causing error when using Archive::Zip

Viewed 47
#!/usr/bin/env perl
use utf8; #this causes error
use strict;
use warnings;

use Archive::Zip qw( :ERROR_CODES :CONSTANTS );

my $zip = Archive::Zip->new();

my $my_string_with_utf8 = <<'END_UTF8_STRING';
Text with UTF8 open/close 201c/201d “hello”
END_UTF8_STRING

my $zip_pathname = 'myfiles/myfile.txt';
$zip->addString($my_string_with_utf8, $zip_pathname);
unless ( $zip->writeToFileNamed('myZip.zip') == AZ_OK ) {
   die 'write error';
}

Error: Wide character in Compress::Raw::Zlib::crc32

Why does utf8 cause error in these package? perl5/vendor_perl/Archive/Zip.pm line 303

1 Answers

$my_string_with_utf8 is not encoded using UTF-8 as the name suggests. It's a string of decoded text aka a string of Unicode Code Points.

Files can only contain bytes, so you need to encode those Code Points into bytes, say by using a character encoding such as UTF-8.

Related