【问题标题】:How to move files into the root of a zip archive?如何将文件移动到 zip 存档的根目录中?
【发布时间】:2018-01-17 23:14:38
【问题描述】:

所以我终于弄清楚了我的 GenEpub.py 程序的主要问题,以及为什么它会产生如 zip -tzipinfo 所证明的压缩的、有效的 zip 文件,但它未能通过 epubcheck 的最终测试。

问题如下,下面的脚本写入了本例中称为IdealogicalEcho的整个文件夹,然后按照说明将其压缩并将压缩文件本身命名为IdealogicalEcho.epubepubcheck 但是看不到这个文件夹,然后报告三个核心 ePub 文件都找不到 mimetype META-INF OEBPS

有没有办法将这些文件移动到 zip 目录的根目录?压缩前还是压缩后?

https://github.com/inferno986return/Damore-essay-ebook

这是EpubGen.py的当前版本:

#!/usr/bin/env python

#GenEpub.py - Generates an .epub file from the data provided.
#Ideally with no errors or warnings from epubcheck (needs to be implemented, maybe with the Python wrapper).

import os
import json
import zipfile

with open('metadata.json') as json_file:
   data = json.load(json_file)

#The ePub standard requires deflated compression and a compression order.
zf = zipfile.ZipFile(data["fileName"] + '.epub', mode='w', compression=zipfile.ZIP_STORED)

zf.write(data["fileName"] + '/mimetype')

for dirname, subdirs, files in os.walk(data["fileName"] + '/META-INF'):
    zf.write(dirname)
    for filename in files:
        zf.write(os.path.join(dirname, filename))

for dirname, subdirs, files in os.walk(data["fileName"] + '/OEBPS'):
    zf.write(dirname)
    for filename in files:
        zf.write(os.path.join(dirname, filename))

zf.close()

#zipfile has a built-in validator for debugging
with open(data["fileName"] + '.epub','r') as f:
    if zipfile.is_zipfile(f) is True:
    print("ZIP file is valid.")

#Extra debugging information
#print(getinfo.compress_type(zf))
#print(getinfo.compress_size(zf))
#print(getinfo.file_size(zf))

还有metadata.json

{
        "comment1": "Metadata.json - Insert the e-book's metadata here. WIP",

        "comment2": "Technical metadata - This is the where the cover image is specified. Recommended to use ePub V2.0.1 over 3.0 for epubVersion and Reflowable rather than Fixed for textPresentation (unless doing a project that requires a specific layout). mobiCover and generateKindle are currently unused but added for futureproofing.",
        "epubCover": "cover.jpg",
        "mobiCover": "cover.jpg",
        "fileName": "IdealogicalEcho",
        "epubVersion": "2.0.1",
        "textPresentation": "Reflowable",
        "generateKindle": "no",

        "comment3": "Book metadata - Information about the e-book itself. Language is specified with ISO 639-1. Rights can be worldwide, country specific or under a permissable license such as Creative-Commons SA",
        "title": "Google's Idealogical Echochamber: How bias clouds our thinking about diversity and inclusion",
        "creator": "James Damore",
        "subject": "Academic",
        "publisher": "Hal Motley",
        "ISBN": "-",
        "language": "en",
        "rights": "Creative Commons BY-SA 4.0",

        "comment4": "This is the page order that the e-book has. The first number before the colon is the page order, the second is the indentation, third is the page name and fourth is file itself.",
        "pages": [
                {
                    "pageNumber": "0",
                    "indentation": "0",
                    "pageName": "Cover",
                    "fileName": "bookcover.xhtml"
                },
                {
                    "pageNumber": "1",
                    "indentation": "0",
                    "pageName": "Title",
                    "fileName": "title.xhtml"
                },
                {
                    "pageNumber": "2",
                    "indentation": "0",
                    "pageName": "Indicia",
                    "fileName": "indicia.xhtml"
                },
                {
                    "pageNumber": "3",
                    "indentation": "0",
                    "pageName": "License",
                    "fileName": "license.xhtml"
                },
                {
                    "pageNumber": "4",
                    "indentation": "0",
                    "pageName": "Contents",
                    "fileName": "toc.xhtml"
                },
                {
                    "pageNumber": "5",
                    "indentation": "0",
                    "pageName": "Foreword",
                    "fileName": "foreword.xhtml"
                },
                {
                    "pageNumber": "6",
                    "indentation": "0",
                    "pageName": "Article",
                    "fileName": "article.xhtml"
                }
            ]
}

【问题讨论】:

    标签: python zipfile epub


    【解决方案1】:
    >>> z = ZipFile('test', 'w')
    >>> help(z.write)
    Help on method write in module zipfile:
    
    write(self, filename, arcname=None, compress_type=None) method of zipfile.ZipFile instance
        Put the bytes from filename into the archive under the name
        arcname.
    

    .write 的第二个参数用于 zip 文件,它指示在 zip 中存储文件的位置的名称。因此,对于您的示例,zf.write(data["fileName"] + '/mimetype', 'mimetype') 应该可以解决问题。

    【讨论】:

    • 它确实适用于 mimetype,我觉得我已经解决了这个问题的三分之一。我对 META-INFOEBPS 两个目录感到困惑。
    猜你喜欢
    • 2013-05-09
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 2022-01-06
    • 2019-01-02
    • 1970-01-01
    相关资源
    最近更新 更多