3

我正在编写一个 perl 脚本,它将压缩给定父文件夹中的一组文件并创建一个 *.epub 文件。该过程正常,我可以在 adobe 数字版中打开 epub,但我收到 epubchecker 错误:

Required MTA-INF/container.xml resource is missing

当我手动压缩文件时(我在 winxp 机器上)没有问题,但是 perl 创建的文件会引发错误。以下是相关代码:

#------------------------------------------------------------------------------- 
# name      :   createEpub
# purpose   :   create an epub from a given parent folder
# args      :   [0] parent folder [1] name of new zip file [2] log object
# example   :   &createEpub( $zipLoc, 'newzip', $log);
# notes     :   it is assumed that mimetype, meta-inf and oebs are all child folders
#               of the given parent folder
# author:   :   jw 2/4/13
#-------------------------------------------------------------------------------


sub createEpub(){
my ($parentFolder, $zipName, $log) = @_;
my $newZipLoc;
$parentFolder =~ s#\\#/#g;

    my $newZip = Archive::Zip->new();

    # add mimetype first with no compression
    my $mimetype = "$parentFolder/mimetype";
    my $mimetypeMember = $newZip->addFile( $mimetype, 'mimetype');
    $mimetypeMember->desiredCompressionMethod( COMPRESSION_STORED );

    ## add web-inf
    my $metaINF = $parentFolder . '/META-INF';
    &addFilesToZip( $metaINF, $parentFolder, $newZip, $log);

    ## add OEBPS
    my $oebps = $parentFolder . '/OEBPS';
    &addFilesToZip( $oebps, $parentFolder, $newZip, $log );

    # maybe break this out in its own func...ok for current epub script purposes
    $newZipLoc = $1 if $parentFolder =~ m/(.*)\//;
    $newZipLoc = $newZipLoc . '/' . $zipName;
    if( $newZipLoc !~ m/\.zip/){
        $newZipLoc = $newZipLoc . '.epub';
    }

    $log->info("writing new zip file to $newZipLoc");
    $newZip->writeToFileNamed( $newZipLoc );

    ## not sure if this is the write thing to do...returning actual file name, not zip     extract object
    return $newZipLoc;

}


sub addFilesToZip(){
my ($file, $origParent, $zip, $log) = @_;

    if( -d $file ){
        my @children = grep{ $_ !~ m/mimetype/} glob("$file/*") or warn "can't add     $file to zip! $!\n";
            foreach my $child( @children ){
                &addFilesToZip( $child, $origParent, $zip, $log);
            }
        } elsif (-f $file){
            my $memPath = $file; $memPath =~ s/\Q$origParent\E//;
            $log->info("adding member $memPath");
            my $newMember = $zip->addFile( $file, $memPath );

        }



}

当我在 winzip 中打开生成的 epub 文件时,container.xml 肯定在那里,我还确保 mimetype 是第一个没有压缩的。这是日志的摘录:

-------------------------------------------------------------------------
    creating zip file from recently unzipped files
-------------------------------------------------------------------------

[ok]:     adding member /META-INF/container.xml
[ok]:     adding member /META-INF/stylesheet.css.kindle
[ok]:     adding member /META-INF/toc.ncx.kindle
[ok]:     adding member /OEBPS/content.opf
[ok]:     adding member /OEBPS/coverpage.html

在谷歌搜索中,我看到人们在他们的 linux shell 命令中做了一些细微的改动,但我没有看到任何与 archive::zip 或 win 相关的内容。

谢谢,bp

4

1 回答 1

3

从您的日志记录看来,您正在使用绝对路径在 zip 文件中创建条目。

[ok]:     adding member /META-INF/container.xml

我相信 epub 文件需要是相对路径 - 尝试从要写入 zip 文件的路径中删除前导“/”。类似的东西(未经测试)

    } elsif (-f $file){
        my $memPath = $file; $memPath =~ s/\Q$origParent\E//;
        # remove leading "/"
        $memPath =~ s#^/+##;
        $log->info("adding member $memPath");
        my $newMember = $zip->addFile( $file, $memPath );

    }
于 2013-02-05T11:41:07.380 回答