gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:48:59 27 4
gpt4 key购买 nike

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

最佳答案

从您的日志记录看来,您正在使用绝对路径在 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 );

}

关于perl - 使用 perl Archive::Zip 创建 *.epub -- epubchecker 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14698579/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com