Created
January 7, 2012 01:35
-
-
Save lieutar/1573408 to your computer and use it in GitHub Desktop.
日本語ファイル名を含むzipの解凍
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/perl | |
use 5.01; | |
use strict; | |
use warnings; | |
no warnings 'utf8'; | |
use utf8; | |
use Encode; | |
use Encode::Guess qw(shift-jis euc-jp 7bit-jis utf8); | |
use Path::Class; | |
use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); | |
use File::Path; | |
my $zip = shift; | |
my $azip = Archive::Zip->new; | |
$azip->read("$zip") == AZ_OK || die "read error:$zip"; | |
foreach my $m ($azip->members){ | |
my $xto = file( decode('Guess', $m->fileName ) ); | |
say $xto; | |
if($m->isa('Archive::Zip::DirectoryMember')){ | |
mkpath $xto, 0, 0755; | |
} else { | |
my $p = $xto->parent; | |
mkpath $p,0, 0755 unless -d $p; | |
my $out = $xto->openw; | |
my $status = $m->rewindData(); | |
die "error $status" unless $status == AZ_OK; | |
while ( ! $m->readIsDone() ) | |
{ | |
( my $bufferRef, $status ) = $m->readChunk(); | |
die "error $status" | |
if $status != AZ_OK && $status != AZ_STREAM_END; | |
$out->print( $$bufferRef ); | |
} | |
$m->endRead(); | |
close $out; | |
} | |
} | |
__END__; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment