[Namazu-devel-ja 1649] filter/ooo.pl を Archive::Zip に対応するパッチ

Tadamasa Teranishi yw3t-trns @ asahi-net.or.jp
2007年 11月 15日 (木) 19:22:51 JST


寺西です。

そう言えば、unzip コマンドを使うフィルタの多くが Archive::Zip に
対応していないことに気づきました。
とりあえず filter/ooo.pl を Archive::Zip に対応するパッチを書きま
した。(development-2-1)
-- 
=====================================================================
寺西 忠勝(TADAMASA TERANISHI)  yw3t-trns @ asahi-net.or.jp
http://www.asahi-net.or.jp/~yw3t-trns/index.htm
Key fingerprint =  474E 4D93 8E97 11F6 662D  8A42 17F5 52F4 10E7 D14E
-------------- next part --------------
Index: ooo.pl
===================================================================
RCS file: /storage/cvsroot/namazu/filter/ooo.pl,v
retrieving revision 1.16.2.2
diff -u -p -r1.16.2.2 ooo.pl
--- ooo.pl	28 Jan 2007 20:22:31 -0000	1.16.2.2
+++ ooo.pl	15 Nov 2007 09:57:24 -0000
@@ -32,6 +32,7 @@ require 'gfilter.pl';
 my $utfconvpath = undef;
 my $unzippath = undef;
 my @unzipopts;
+my $zread = undef;
 
 sub mediatype() {
     # http://framework.openoffice.org/documentation/mimetypes/mimetypes.html
@@ -46,8 +47,18 @@ sub mediatype() {
 }
 
 sub status() {
+    if (util::checklib('Compress/Zlib.pm') and
+    util::checklib('Archive/Zip.pm')) {
+        $zread = \&az_zread;
+        if ($English::PERL_VERSION >= 5.008) {
+            $utfconvpath = "none";
+            return 'yes';
+        }
+    }
+
     $unzippath = util::checkcmd('unzip');
     if (defined $unzippath){
+        $zread = \&unzip_zread;
         @unzipopts = ("-p");
         if (util::islang("ja")) {
            if ($English::PERL_VERSION >= 5.008) {
@@ -117,16 +128,7 @@ sub filter_metafile ($$$) {
         print $fh $$contref;
         util::fclose($fh);
     }
-    my @cmd = ($unzippath, @unzipopts, $tmpfile, $metafile);
-    my $status = util::syscmd(
-        command => \@cmd,
-        option => {
-            "stdout" => \$xml,
-            "stderr" => "/dev/null",
-            "mode_stdout" => "wt",
-            "mode_stderr" => "wt",
-        },
-    );
+    my $status = $zread->($tmpfile, $metafile, \$xml);
     unlink $tmpfile;
 
     my $authorname = ooo::get_author(\$xml);
@@ -170,16 +172,7 @@ sub filter_contentfile ($$$$$) {
         print $fh $$contref;
         util::fclose($fh);
     }
-    my @cmd = ($unzippath, @unzipopts, $tmpfile, $contentfile);
-    my $status = util::syscmd(
-        command => \@cmd,
-        option => {
-            "stdout" => \$xml,
-            "stderr" => "/dev/null",
-            "mode_stdout" => "wt",
-            "mode_stderr" => "wt",
-        },
-    );
+    my $status = $zread->($tmpfile, $contentfile, \$xml);
     unlink $tmpfile;
 
     ooo::remove_all_tag(\$xml);
@@ -286,4 +279,58 @@ sub decode_entity ($) {
     $$text =~ s/&nbsp[;\s]/ /g;
 }
 
+sub unzip_zread($$$)
+{
+    my ($zipfile, $filename, $conts) = @_;
+
+    my @cmd = ($unzippath, @unzipopts, $zipfile, $filename);
+    my $status = util::syscmd(
+        command => \@cmd,
+        option => {
+            "stdout" => $conts,
+            "stderr" => "/dev/null",
+            "mode_stdout" => "wt",
+            "mode_stderr" => "wt",
+        },
+    );
+    return $status;
+}
+
+use constant NMZ_AZ_OK           => 0;
+use constant NMZ_AZ_STREAM_END   => 1;
+use constant NMZ_AZ_ERROR        => 2;
+use constant NMZ_AZ_FORMAT_ERROR => 3;
+use constant NMZ_AZ_IO_ERROR     => 4;
+
+sub az_zread($$$)
+{
+    eval 'use Archive::Zip;';
+    my ($zipfile, $filename, $conts) = @_;
+
+    $$conts = '';
+
+    my $zip = Archive::Zip->new();
+    my $err = $zip->read($zipfile);
+    if ($err != NMZ_AZ_OK) {
+        util::dprint("Archive::Zip: there was a error");
+        return 1;
+    }
+
+    my $member = $zip->memberNamed($filename);
+    my $size = $member->uncompressedSize();
+    if ($size == 0) {
+        my $fname = $member->fileName();
+        util::dprint("$fname: filesize is 0");
+        return 1;
+    } elsif ($size > $conf::FILE_SIZE_MAX) {
+        my $fname = $member->fileName();
+        util::dprint("$fname: Too large ooo file");
+        return 1;
+    }
+
+    $$conts = $zip->contents($member);
+
+    return 0;
+}
+
 1;


Namazu-devel-ja メーリングリストの案内