使用 perl Archive::Zip 创建 *.epub -- epubchecker 错误 [英] creating *.epub with perl Archive::Zip -- epubchecker error

查看:27
本文介绍了使用 perl Archive::Zip 创建 *.epub -- epubchecker 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing a perl script that will zip up a group of files from a given parent folder and create a *.epub file. The process works ok, and I am able to open the epub in adobe digital editions, but I get an epubchecker error:

Required MTA-INF/container.xml resource is missing

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

When I zip up the files manually (I'm on a winxp machine) there are no problems, but the perl created file throws the error. Here is the relevant code:

#------------------------------------------------------------------------------- 
# 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 是第一个没有压缩的.以下是日志的摘录:

when I open the resulting epub file in winzip, the container.xml is definitely there, I also made sure the mimetype is first with no compression. Here's an excerpt from the log:

-------------------------------------------------------------------------
    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 相关的东西.

In the googling I've seen there is a slight alteration people make in their linux shell commands, but I didn't see anything related to archive::zip or win.

谢谢,bp

推荐答案

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

From your logging it looks like you are creating entries in the zip file with absolute paths.

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

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

I believe epub files need to be relative paths - try removing the leading "/" from the path that is going to be written to the zip file. Something like ths (untested)

    } 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 );

    }

这篇关于使用 perl Archive::Zip 创建 *.epub -- epubchecker 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆