如何将文件移动到zip存档的根目录?

时间:2018-01-17 23:14:39

标签: python zipfile epub

所以我终于找到了我的GenEpub.py程序的主要问题以及为什么它会产生zip -tzipinfo所证明的缩小的有效zip文件,但它未能通过{{}}的最终测试{1}}。

问题如下,下面的脚本会写入在这种情况下调用的整个文件夹epubcheck,然后按照说明将其拉上并将zip文件命名为IdealogicalEcho。但IdealogicalEcho.epub无法查看此文件夹,然后报告无法找到三个核心ePub文件epubcheck

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

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

以下是mimetype META-INF OEBPS的当前版本:

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

1 个答案:

答案 0 :(得分:0)

>>> 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.

对于zip文件,.write有第二个参数,指示zip文件中存储文件的位置。因此,对于您的示例,zf.write(data["fileName"] + '/mimetype', 'mimetype')应该这样做。