【问题标题】:Perl script not writing to extracted zip filePerl 脚本未写入提取的 zip 文件
【发布时间】:2015-04-07 19:46:47
【问题描述】:

我正在编写一个 perl 脚本以从 zip 文件中提取一个特定命名的文件并写入该提取的文件(脚本中还有更多内容,但这是我卡住的部分)。下面的代码是 perl 脚本的一部分。

我不知道为什么它没有写入文件!

my $zipName = "f:\\data\\archive.zip";
my $destPath = "f:\\data\\dataFile.DAT";
my $tempZip = Archive::Zip->new();
my $dataNum = " 0 0";

        unless ($tempZip->read($zipName) == AZ_OK ) 
        {
            die 'read error';
        }

    my @dataFileMatches = $tempZip->membersMatching( 'dataFile.*\.DAT' );

    my $dataFile;

    if($#dataFileMatches> -1)
    {
        $dataFile = $dataFileMatches[0];

        my $fileContents = $tempZip->contents($dataFile);

        my $newContents = substr $fileContents, 0, 10;
        $newContents = $newContents.$dataNum;

        my $dataFilename = $dataFile ->fileName();
        open(my $fh, '>', $dataFilename) or die "Could not open file '$dataFilename' $!";

        $tempZip->contents($dataFile, $newContents);

        $fileContents = $tempZip->contents($dataFile);

        print "New FileContents -  $fileContents\n";

        #print $fh $newContents;
        #copy($fh, $destPath);
        close $fh;
    }

这里是没有被执行的代码:

use strict;
use warnings;
use File::Copy qw(copy);
use Archive::Zip qw/ :ERROR_CODES :CONSTANTS /;

my $filename = 'dataFile.DAT';
open(my $fh, '>', $filename) or die "Could not open file";

print $fh "test\n";

close $fh;

但是如果我在这个提取的 DAT 文件所在的完全相同的目录中创建一个虚拟 TEXT 文件,它就可以工作。

【问题讨论】:

  • print "New FileContents - $fileContents\n"; 写信给STDOUT;您在终端中看到任何输出了吗?
  • 是的,它确实可以正确打印到终端,但是当我查看文件时它并没有改变。
  • "当我查看文件时它没有改变。"我假设您正在运行的脚本没有注释掉 print $fh $newContents; 行。实际上,您问题中的代码仅写入STDOUT
  • 这里有一个如何解压文件的例子:api.metacpan.org/source/PHRED/Archive-Zip-1.46/examples/… 最终,你不必写文件,而是调用 extractMember 方法。
  • print $fh, $newContents; 不正确,应该是print $fh $newContents; 没有逗号。将某些内容分配给$fileContents 不会向您的文件句柄写入任何内容,因此将print 行切换为$fileContents = $tempZip->contents($dataFile); 不会解决您的问题。如果要将数据写入文件,则必须在文件句柄 $fh 上打印一些内容。

标签: perl zipfile


【解决方案1】:

您必须使用 Archive::Zip 的 extractMemberWithoutPaths() 方法。

以下是提取文件的方法:

#!perl

use strict;
use warnings;
use Data::Dumper qw/Dumper/;
use FindBin qw/$Bin/;
use File::Spec;
use Archive::Zip qw/AZ_OK/;

my $zipName = File::Spec->catfile($Bin, '/archive.zip');
my $destPath = File::Spec->catfile($Bin, 'dataFile.DAT');
my $tempZip = Archive::Zip->new();
my $dataNum = " 0 0";

unlink $destPath if -e $destPath;

unless ($tempZip->read($zipName) == AZ_OK ) {
    die 'read error';
}

my @dataFileMatches = $tempZip->membersMatching( {regex => 'dataFile.*\.DAT'} );

$tempZip->extractMemberWithoutPaths($dataFileMatches[0]); # extract $fileContents to current working directory

if ( -e $destPath ) {
    print "file was extracted\n";
}else{
    print "File was not extracted :(\n";
}

【讨论】:

  • 我已经更改了提取它的方式,但我想要在文件中的新文本仍未写入文件中。我已经分解了这个脚本,发现即使是简单的打开并打印到文件也不起作用,但是如果我将文件切换到文本文件,它就可以工作。
  • 你能发布你的分解代码sn-ps吗?也许这是某种错字。请随意使用 gist 或类似的东西 (gist.github.com)。
猜你喜欢
  • 2012-06-15
  • 1970-01-01
  • 1970-01-01
  • 2014-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多