快速解压 Epub [英] Unzipping Epub in swift

查看:102
本文介绍了快速解压 Epub的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to unzip a .epub file in swift to read the data myself entirely. I know how to parse the output of an ePub if I can get it (I've written a working example in python), but SSZipArchive apparently will not unzip .epubs. It does, however, works fine on a dummy .zip file; only .epub is a problem. So far as I can tell, there has been no question asking how to actually do this by hand on S.O. beyond simply pointing people to projects that do it for you in objective-c with lots of overhead (which I don't understand or need) that defeats the purpose of what I need to do. Below is my current attempt. Note that the epub in question can be found at the following link (project gutenberg) http://www.gutenberg.org/ebooks/158.epub.noimages and that when I run this the print statement emits: "true, true, true, false" (that is, the files and paths all exist, but won't unzip):

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 文件并读取其容器内容:

Below is proof of concept code written in python in which I CAN get the very same epub to be recognized as a zip file and read its container content:

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

推荐答案

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

I figured it out after many many hours of reading. Turns out the solution is extremely simple if non-obvious.

fileName.epub"文件需要重命名为fileName.zip".就这样!

The "fileName.epub" file needs to be renamed to "fileName.zip". That's it!

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

After that either SSZipArchive or Zip will unzip the file into its META-Inf, mimetype, and OEBPS files in a folder called "fileName" (at least as the default name).

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

Hope this helps anyone struggling with this. Of course if there is another way to do this please let me know in comments.

这篇关于快速解压 Epub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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