使用perl Archive :: Zip - epubchecker错误创建* .epub

时间:2013-02-05 01:10:14

标签: perl archive epub

我正在编写一个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的

1 个答案:

答案 0 :(得分: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 );

    }