2013-02-05 74 views
3

我正在編寫一個perl腳本,它將壓縮來自給定父文件夾的一組文件並創建一個* .epub文件。這個過程是好的,我能打開Adobe Digital Editions中的EPUB,但我得到一個錯誤epubchecker:用perl創建* .epub Archive :: Zip - 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肯定是有的,我也做了一定的MIME類型是第一個不使用壓縮。下面是從日誌摘錄:

------------------------------------------------------------------------- 
    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命令的google搜索,但我沒有看到歸檔:: zip或贏得任何東西相關。

感謝, BP

回答

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

    } 
+0

做到了,謝謝!-bp – badperson

相關問題