Help us understand the problem. What is going on with this article?

IO::Compress::Zipでzipファイルを作る

Perl Advent Calendar 2019 の10日目です!!
昨日はskajiさんでした。


この間、IO::Compress::Zipで、zipファイルを作成したのですが、記事が少なそうでしたので、簡単な使い方紹介をしようと思います。

IO::Compress::Zipとは

https://metacpan.org/pod/IO::Compress::Zip

IO::Compress::Zip - Write zip files/buffers

IO::Compress::Zipはfileやbufferからzip fileを書き出してくれる君です。

実行環境

MacBook Pro
OS: macOS Mojave
Perl v5.28.2

使い方

zip fileを開いて、zipを作る。

IO::Compress::Zipの第一引数にpath、もしくはfilehandleを渡すと、そのファイルを開いて、zipファイルを作ってくれます。
ちなみに、渡したfilehandleは、IO::Compress::Zipのclose時に、内部でcloseしてくれるようです。

use strict;
use warnings;

use IO::Compress::Zip qw/$ZipError/;

my $out = 'test.zip';
my $z = IO::Compress::Zip->new($out, NAME => 'test.text') or die "zip failed: $ZipError\n";
$z->write("hello zip\n");
$z->write("hello perl\n");

$z->close();

もしくは、

use strict;
use warnings;

use IO::Compress::Zip qw/$ZipError/;

my $out = 'test.zip';
my $fh;
open($fh, ">", $out) or die("error :$!");

my $z = IO::Compress::Zip->new($fh, NAME => 'test.text') or die "gzip failed: $ZipError\n";
$z->write("hello zip\n");
$z->write("hello perl\n");

$z->close();
$ perl test_zip_file.pl
$ unzip -l test.zip
Archive:  test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       21  12-04-2019 14:36   test.text
---------                     -------
       21                     1 file

bufferにzipを作る。

IO::Compress::Zipにscalarのリファレンスを渡すと、そこに書き込んでくれます。

use strict;
use warnings;

use IO::Compress::Zip qw/$ZipError/;

my $out = '';
my $z = IO::Compress::Zip->new(\$out, NAME => 'test.text') or die "zip failed: $ZipError\n";;
$z->write("hello zip\n");
$z->write("hello perl\n");
$z->close();

print $z;

my $fh;
open($fh, ">", "data.zip") or die("error :$!");
print $fh $out;
close $fh;

bufferに作成したzipファイルは、そのままfileに出力することができます。

$ perl test_zip_file.pl
IO::Compress::Zip=GLOB(0x7fcf48005908)
$ unzip -l data.zip
Archive:  data.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       21  12-06-2019 16:34   test.text
---------                     -------
       21                     1 file

ちょっとハマったこと

zipファイルを作る際、NAME option(NAME => 'test.text') を入れてあげないと、unzipできなくなるので注意が必要です。
https://metacpan.org/pod/IO::Compress::Zip#Name-=%3E-$string
https://stackoverflow.com/questions/55043193/zip-archive-contain-files-without-names-how-to-unzip-it

$ unzip -l test.zip
Archive:  test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       21  12-04-2019 14:41
---------                     -------
       21                     1 file

$ unzip test.zip
Archive:  test.zip
mapname:  conversion of  failed

以上になります。

明日は、codehexさんです。
良いクリスマスを!

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away