Skip to content

Commit 8736984

Browse files
committed
Partial fix issue wcember#24 - invalid epub
Use own zip code to walk directory and place mimetype file first. Fix EPUBCheck v5.1.0 ERROR(PKG-006): Mimetype wrong location ERROR(PKG-006): My First Epub.epub//...../My%20First%20Epub.epub(-1,-1): Mimetype file entry is missing or is not the first file in the archive.
1 parent f040f38 commit 8736984

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

pypub/epub.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import shutil
66
import tempfile
77
import time
8+
from zipfile import ZipFile, ZIP_DEFLATED
89

910
import jinja2
1011
import requests
@@ -264,8 +265,40 @@ def create_zip_archive(epub_name):
264265
os.remove(os.path.join(epub_name_with_path, '.zip'))
265266
except OSError:
266267
pass
267-
shutil.make_archive(epub_name_with_path, 'zip', self.EPUB_DIR)
268-
return epub_name_with_path + '.zip'
268+
# perform zip operation
269+
# TODO cleanup chdir code
270+
# TODO refactor/simplify walk code
271+
# TODO compression - debug Stored for now
272+
save_cwd = os.getcwd()
273+
os.chdir(self.EPUB_DIR)
274+
archname = epub_name_with_path + '.zip'
275+
paths = ['.']
276+
flist = None
277+
if os.path.exists(archname):
278+
os.unlink(archname)
279+
if not flist:
280+
flist = ['mimetype']
281+
for path in paths:
282+
for root, dirs, files in os.walk(path):
283+
for fname in files:
284+
if fname == 'mimetype':
285+
continue
286+
fname = os.path.join(root, fname)
287+
flist.append(fname)
288+
os.chdir(save_cwd)
289+
arch = ZipFile(archname, 'w')#, ZIP_DEFLATED) # FIXME
290+
os.chdir(self.EPUB_DIR)
291+
for fname in flist:
292+
# . is bad for py24 under win,
293+
# py 2.5 generates more sane entries for:
294+
# './filename' and '.\\filename'
295+
print(fname) # FIXME DEBUG
296+
fname = os.path.normpath(fname)
297+
arch.write(fname)
298+
arch.close()
299+
os.chdir(save_cwd)
300+
301+
return archname
269302

270303
def turn_zip_into_epub(zip_archive):
271304
epub_full_name = zip_archive.strip('.zip') + '.epub'

0 commit comments

Comments
 (0)