2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PerlのArchive::ZipやArchive::Tarを試すワンライナー

Posted at

概要

PerlのArchive::ZipArchive::Tarモジュールを試用するためのワンライナー。これらのモジュールの動作や出力確認をしたい時用にメモしてます。

詳細

Archive::Zip

zipファイルの作成

./dataディレクトリを圧縮したtemp.zipファイルを作成する。

perl -MArchive::Zip -e "$zip=Archive::Zip->new; $zip->addTree('./data','data');$zip->writeToFileNamed('temp.zip')"

メソッドaddTreeに渡している第一引数./dataは圧縮対象ディレクトリ。第二引数dataは、圧縮ファイル内でのこのディレクトリの名称。同じにしておけば混乱が少なそう。あえて変えることもできる(日次バックアップとして"元のディレクトリ名+年月日"とするなど)。

zipファイルの内容確認

temp.zipファイルの内容を表示する。

perl -MArchive::Zip -e "$zip=Archive::Zip->new('./temp.zip');print qq($_\n) foreach ($zip->memberNames)"

zipファイルの展開

temp.zipファイルを展開する。

perl -MArchive::Zip -e "$zip=Archive::Zip->new('./temp.zip'; $zip->extractTree();"

展開対象を指定したいときはextractMemberメソッド。

perl -MArchive::Zip -e "$zip=Archive::Zip->new('./temp.zip');$zip->extractMember($_) foreach ($zip->memberNames)"

Archive::Tar

tarファイルの作成

./temp.txtファイルを格納したtemp.tarファイルを作成する。add_filesには、一度に複数のファイルを指定できる。

perl -MArchive::Tar -e "$tar=Archive::Tar->new; $tar->add_files('temp.txt'); $tar->write('temp.tar')"

./temp.txtディレクトリを格納、gzip圧縮したtemp.tgzファイルを作成する。

perl -MArchive::Tar -e "$tar=Archive::Tar->new; $tar->add_files('temp.txt'); $tar->write('temp.tgz', COMPRESS_GZIP )"

オブジェクトを生成せずクラスメソッドを使うことも可能。第三引数以降で、複数のファイルを指定できる。

perl -MArchive::Tar -e "Archive::Tar->create_archive('temp.tgz', COMPRESS_GZIP, 'temp.txt');
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?