在swift中解压缩Epub

Eth*_*Kay 0 zip epub ios swift ssziparchive

我需要在swift中解压缩.epub文件以完全自己读取数据.我知道如何解析ePub的输出如果我能得到它(我在python中编写了一个工作示例),但SSZipArchive显然不会解压缩.epubs.但是,它确实可以在伪.zip文件上正常工作; 只有.epub是个问题.据我所知,毫无疑问,如果只是将人们指向那些在Objective-c中为你做的项目,并且有很多开销(我不明白或不需要) )这违背了我需要做的目的.以下是我目前的尝试.请注意,有问题的epub可以在以下链接(项目gutenberg)http://www.gutenberg.org/ebooks/158.epub.noimages找到,当我运行它时,print语句会发出:"true,true, true,false"(即文件和路径都存在,但不会解压缩):

import Foundation

class EpubExtractor: NSObject, SSZipArchiveDelegate {
    init(fileName: String) {
        fName = fileName
    }

    func getEpubInfo() {
        var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
        let documentsDir = paths[0]
        let zipPath =  documentsDir.stringByAppendingString("/MyZipFiles") // My folder name in document directory
        let fileManager = NSFileManager.defaultManager()
        let success1 = fileManager.fileExistsAtPath(zipPath) as Bool
        if success1 == false {
            print("no directory")
            do {
                try! fileManager.createDirectoryAtPath(zipPath, withIntermediateDirectories: true, attributes: nil)
            }
        }
        let archivePath = zipPath.stringByAppendingString("/emma.epub") // Sample folder is going to zip with name Demo.zip
        let success2 = fileManager.fileExistsAtPath(archivePath) as Bool
        let destPath = zipPath.stringByAppendingString("/Hello")
        let success3 = fileManager.fileExistsAtPath(destPath) as Bool
        let worked = SSZipArchive.unzipFileAtPath(archivePath, toDestination: destPath, delegate:self)
        print(success1, success2, success3, worked)

    }
}
Run Code Online (Sandbox Code Playgroud)

编辑

下面是用python编写的概念代码的证明,其中我可以获得相同的epub被识别为zip文件并读取其容器内容:

import zipfile
dir = "sampleData/epubs/"
fileName = "emma.epub"
print zipfile.is_zipfile(dir+fileName) # Check whether file is zip (this returns true, though in swift it fails)

zip = zipfile.ZipFile(dir+fileName)
txt = zip.read('META-INF/container.xml')  # Print contents of container (this is what I need swift to be able to do)
print txt # This successfully prints the container content text
Run Code Online (Sandbox Code Playgroud)

Eth*_*Kay 5

经过许多小时的阅读后,我想出来了.事实证明,解决方案非常简单,如果不明显的话.

需要将"fileName.epub"文件重命名为"fileName.zip".而已!

之后,SSZipArchive或Zip会将文件解压缩到名为"fileName"的文件夹中的META-Inf,mimetype和OEBPS文件中(至少作为默认名称).

希望这有助于任何挣扎于此的人.当然,如果还有其他方法可以做到这一点,请在评论中告诉我.