#============================================================ # MyArchive #============================================================ package MyArchive; #use Exporter; use Common; @ISA = qw(Common); #公開したいサブルーチン #@EXPORT = qw(DelSpace Reduce01 MakePath RegExpQuote); use strict; use Cwd; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); use Utils; our $zipPath = "/usr/bin/zip"; our $unzipPath = "/usr/bin/unzip"; if($^O eq 'MSWin32') { $zipPath = "c:/cygwin/bin/zip"; $unzipPath = "c:/cygwin/bin/unzip"; } sub new { my ($module) = @_; my $this = {}; bless $this; return $this; } sub DESTROY { my $this = shift; $this->Close(); } sub MakePasswordedZIP { my ($this, $OutputPath, $BaseDir, $pInputPaths, $passwd, $option, $FileSystemCharCode, $ZIPFileCharCode, $App) = @_; return undef if(@$pInputPaths == 0); $App->print("O[$OutputPath]\n"); my $Kara = '〜'; #'〜'; my $KaraZIPCharCode = $Kara; Jcode::convert(\$Kara, $FileSystemCharCode, 'utf-8');#$pParams->{ScriptCharCode}); Jcode::convert(\$KaraZIPCharCode, $ZIPFileCharCode, 'utf-8'); my $BaseDirRegExp = Utils::RegExpQuote($BaseDir); my $cwd = cwd(); #$App->print("cwd: [$cwd]\n"); if(chdir($BaseDir)) { $App->print("Change directory to [$BaseDir].\n") if($App); } else { $App->H3("Error: Change directory to [$BaseDir] failed.\n") if($App); return undef; } if(-e $OutputPath) { if(unlink($OutputPath)) { $App->H3("Existing file [$OutputPath] has been deleted.\n") if($App); } else { $App->H3("Error: Existing file [$OutputPath] could not be deleted.\n") if($App); return undef; } } # $OutputPath =~ s/([\(\)\{\}\|\>\<])/\\$1/g; my $cmd = "\"$zipPath\" $option -P $passwd \"$OutputPath\""; my $cmd2 = "\"$zipPath\" $option -P ***** \"$OutputPath\""; my @CopiedFiles; for(my $i = 0 ; $i < @$pInputPaths ; $i++) { my $SourcePath = $pInputPaths->[$i]; next if($SourcePath eq ''); $SourcePath =~ s/\\/\//g; # $SourcePath =~ s/([\(\)\{\}\|\>\<])/\\$1/g; $SourcePath =~ s/^$BaseDirRegExp\/?//i; #$App->print("$i: [$path]\n"); #Jcode::convert(\$path, 'utf8'); my $SourcePathZIPCharCode = $SourcePath; Jcode::convert(\$SourcePathZIPCharCode, $ZIPFileCharCode, $FileSystemCharCode); #$App->print("K:$KaraZIPCharCode\n"); #$SourcePathZIPCharCode =~ s/$KaraZIPCharCode/-/g if($^O ne 'MSWin32' and $KaraZIPCharCode and $KaraZIPCharCode ne '?'); $SourcePathZIPCharCode =~ s/\?/-/g; my $ArchivePath = $SourcePath; if($SourcePathZIPCharCode ne $SourcePath) { $App->PrintRawHTML("Try to use encoded filename [$FileSystemCharCode to $ZIPFileCharCode].
\n"); my $SourceFullPath = $SourcePath; #Utils::MakePath($BaseDir, $SourcePath, '/', 0); my $TargetFullPath = $SourcePathZIPCharCode;# Utils::MakePath($BaseDir, $SourcePathZIPCharCode, '/', 0); if(-e $TargetFullPath) { $App->PrintRawHTML("[$TargetFullPath] exists. Use the original file.
\n"); } elsif(Utils::CopyFile($SourceFullPath, $TargetFullPath) > 0) { $ArchivePath = $SourcePathZIPCharCode; push(@CopiedFiles, $TargetFullPath); $App->PrintRawHTML("Copy [$SourceFullPath] to [$TargetFullPath] succeeded.
\n"); } else { $App->PrintRawHTML("Copy [$SourceFullPath] to [$TargetFullPath] failed.
Use the original file.
\n"); } } $cmd .= " \"$ArchivePath\""; $cmd2 .= " \"$ArchivePath\""; } $cmd .= " |"; $cmd2 .= " |"; $App->print("cmd [$cmd2]\n") if($App); #return; my $fh; if(open($fh, $cmd)) { } else { $App->H3("Error: Can't open [$cmd]:$!.\n") if($App); return undef; } my @lines = <$fh>; if($App) { $App->print(join("\n", @lines)); $App->print("\n"); } if(close($fh)) { } else { $App->H3("Error: Can't close [$cmd]:$!.\n") if($App); return undef; } # my $ret = system($cmd); for(my $i = 0 ; $i < @CopiedFiles ; $i++) { my $TargetFullPath = $CopiedFiles[$i]; if(Utils::DeleteFile($TargetFullPath) > 0) { $App->PrintRawHTML("[$TargetFullPath] has been deleted.
\n"); } else { $App->PrintRawHTML("Delete [$TargetFullPath] failed.
\n"); } } if(chdir($cwd)) { $App->print("Change directory to [$cwd].\n") if($App); } else { $App->H3("Warning: Change directory to [$cwd] failed.\n") if($App); } # return undef if($ret != 0); return $OutputPath; } sub BurstZIP { my ($this, $InputPath, $BaseDir, $option, $App) = @_; my $cwd = cwd(); #$App->print("cwd: [$cwd]\n"); if(chdir($BaseDir)) { $App->print("Change directory to [$BaseDir].\n") if($App); } else { $App->H3("Error: Change directory to [$BaseDir] failed.\n") if($App); return undef; } my $cmd = "$unzipPath $option $InputPath |"; my $cmd2 = "$unzipPath $option $InputPath |"; $App->print("cmd [$cmd2]\n") if($App); my $fh; if(open($fh, $cmd)) { } else { $App->H3("Error: Can't open [$cmd]:$!.\n") if($App); return undef; } my @lines = <$fh>; if($App) { for(my $i = 0 ; $i < @lines ; $i++) { $App->print("$lines[$i]\n"); } $App->print("\n"); } if(close($fh)) { } else { $App->H3("Error: Can't close [$cmd]:$!.\n") if($App); return undef; } # my $ret = system($cmd); if(chdir($cwd)) { $App->print("Change directory to [$cwd].\n") if($App); } else { $App->H3("Warning: Change directory to [$cwd] failed.\n") if($App); } # return undef if($ret != 0); return @lines; } # 暗号化zipを展開(-P)し、パイプ(-p)で渡す # my $cmd = "$unzipPath -p -P $passwd $InputPath |"; # open my $fh, $cmd or die "can't open $cmd :$!"; # my @lines = <$fh>; # close $fh or die "can't close $cmd:$!"; # return \@lines; sub MakeZIP { my ($this, $OutputPath, $BaseDir, $pFiles) = @_; $BaseDir =~ s/\\/\//g; $BaseDir = "$BaseDir/" if($BaseDir !~ /\/$/); $BaseDir = Utils::RegExpQuote($BaseDir); unlink($OutputPath); my $zip = Archive::Zip->new(); foreach my $fmask (@$pFiles) { my @files = glob($fmask); #print "fmask: [$fmask]\n"; foreach my $f (@files) { next if(-d $f); my $fname = $f; $fname =~ s/^$BaseDir//; #print "$f [$fname]\n"; $zip->addFile($f, $fname); # $zip->addTree($FilePath, $Hit{AbstractFile}); } } my $ret = $zip->writeToFileNamed($OutputPath); if($ret == AZ_OK) { } else { return (undef, "Archive file [$OutputPath] could not been saved."); } return ($OutputPath, $ret, "") if(-f $OutputPath); return (undef, 1, "Archive file [$OutputPath] could not been created"); } sub MakePDF { my ($this, $OutputPath, $pFiles, $pdftkpath) = @_; unlink($OutputPath); my $cmd = "$pdftkpath "; foreach my $fmask (@$pFiles) { #print "fm: $fmask\n"; my @files = glob($fmask); foreach my $f (@files) { #print "f: $f\n"; $cmd .= "$f "; } } $cmd .= " cat output $OutputPath"; #print "cmd: $cmd\n"; my $str = `$cmd 2>&1`; #print "\ns: [$str]\n\n"; if(!defined $str) { return (undef, 2, "Can not open [$cmd]"); } # my $str = ''; # if(!open(IN, "$cmd|")) { # return (undef, 2, "Can not open [$cmd]"); # } # while() { # $str .= $_; # } # close(IN); return ($OutputPath, 0, "", $str) if(-f $OutputPath); return (undef, 1, "Archive file [$OutputPath] could not been created", $str); } 1;