mirror of
https://github.com/JimmXinu/FanFicFare.git
synced 2026-01-09 01:22:57 +01:00
Fix to force mimetype file to be uncompressed and first in epub. Tested on Python 2.5.2.
This commit is contained in:
parent
466d5f4ee3
commit
f8701a9e8d
1 changed files with 31 additions and 9 deletions
40
zipdir.py
40
zipdir.py
|
|
@ -44,6 +44,32 @@ def addFolderToZip(zippedHelp,folder,fpath):
|
|||
def inMemoryZip(files):
|
||||
# files have a structure of {'path/to/file' => content} dictionary
|
||||
io = StringIO.StringIO()
|
||||
|
||||
# This fixes the uncompressed mimetype-first issue by opening
|
||||
# the in memory file as STORE, putting in the mimetype, then
|
||||
# closing and re-opening with DEFLATED. while it is often
|
||||
# true that mimetype is the first file, we can't assume it,
|
||||
# because the dict object is defined as unordered.
|
||||
path='mimetype'
|
||||
memzip = zipfile.ZipFile(io, 'a', compression=zipfile.ZIP_STORED)
|
||||
memzip.debug = 3
|
||||
if type(files[path]) != type('str'):
|
||||
data = files[path].getvalue()
|
||||
else:
|
||||
data = files[path]
|
||||
|
||||
logging.debug("Writing ZIP path %s" % path)
|
||||
try:
|
||||
memzip.writestr(path, data.encode('utf-8'))
|
||||
except UnicodeDecodeError, e:
|
||||
memzip.writestr(path.encode('utf-8'), data.encode('utf-8'))
|
||||
|
||||
memzip.close()
|
||||
|
||||
# remove it from the files dict.
|
||||
del(files['mimetype'])
|
||||
|
||||
# open in 'a' append mode.
|
||||
memzip = zipfile.ZipFile(io, 'a', compression=zipfile.ZIP_DEFLATED)
|
||||
memzip.debug = 3
|
||||
|
||||
|
|
@ -55,16 +81,12 @@ def inMemoryZip(files):
|
|||
|
||||
# logging.debug(data)
|
||||
logging.debug("Writing ZIP path %s" % path)
|
||||
# epub standard requires mimetype to be uncompressed and first file.
|
||||
if path == 'mimetype':
|
||||
compress=zipfile.ZIP_STORED
|
||||
else:
|
||||
compress=zipfile.ZIP_DEFLATED
|
||||
try:
|
||||
memzip.writestr(path, data.encode('utf-8'), compress_type=compress)
|
||||
memzip.writestr(path, data.encode('utf-8'))
|
||||
except UnicodeDecodeError, e:
|
||||
memzip.writestr(path.encode('utf-8'), data.encode('utf-8'), compress_type=compress)
|
||||
|
||||
memzip.writestr(path.encode('utf-8'), data.encode('utf-8'))
|
||||
|
||||
# declares all the files created by Windows.
|
||||
for zf in memzip.filelist:
|
||||
zf.create_system = 0
|
||||
|
||||
|
|
@ -79,4 +101,4 @@ if __name__ == '__main__':
|
|||
data = inMemoryZip(files)
|
||||
f = open('res.zip', 'w')
|
||||
f.write(data)
|
||||
f.close()
|
||||
f.close()
|
||||
|
|
|
|||
Loading…
Reference in a new issue