【问题标题】:Unzipping Epub in swift快速解压 Epub
【发布时间】:2016-04-15 00:14:29
【问题描述】:

我需要快速解压缩一个 .epub 文件才能自己完全读取数据。如果可以的话,我知道如何解析 ePub 的输出(我在 python 中编写了一个工作示例),但 SSZipArchive 显然不会解压缩 .epubs。但是,它在虚拟 .zip 文件上运行良好;只有 .epub 是个问题。据我所知,毫无疑问,如何在 S.O. 上手动执行此操作。除了简单地将人们指向在objective-c中为你做的项目,而且有很多开销(我不理解或不需要),这违背了我需要做的事情的目的。以下是我目前的尝试。请注意,有问题的 epub 可以在以下链接(项目古腾堡)http://www.gutenberg.org/ebooks/158.epub.noimages 中找到,并且当我运行此打印语句时会发出:“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)

    }
}

编辑

下面是用 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

【问题讨论】:

    标签: ios swift zip epub ssziparchive


    【解决方案1】:

    经过许多小时的阅读,我明白了。事实证明,如果不明显,解决方案非常简单。

    “fileName.epub”文件需要重命名为“fileName.zip”。就是这样!

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

    希望这可以帮助任何为此苦苦挣扎的人。当然,如果有其他方法可以做到这一点,请在 cmets 中告诉我。

    【讨论】:

      猜你喜欢
      • 2022-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-22
      • 1970-01-01
      相关资源
      最近更新 更多