Skip to content

Commit

Permalink
Partial fix issue wcember#24 - invalid epub
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
clach04 committed Jul 28, 2023
1 parent f040f38 commit 8736984
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions pypub/epub.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import shutil
import tempfile
import time
from zipfile import ZipFile, ZIP_DEFLATED

import jinja2
import requests
Expand Down Expand Up @@ -264,8 +265,40 @@ def create_zip_archive(epub_name):
os.remove(os.path.join(epub_name_with_path, '.zip'))
except OSError:
pass
shutil.make_archive(epub_name_with_path, 'zip', self.EPUB_DIR)
return epub_name_with_path + '.zip'
# perform zip operation
# TODO cleanup chdir code
# TODO refactor/simplify walk code
# TODO compression - debug Stored for now
save_cwd = os.getcwd()
os.chdir(self.EPUB_DIR)
archname = epub_name_with_path + '.zip'
paths = ['.']
flist = None
if os.path.exists(archname):
os.unlink(archname)
if not flist:
flist = ['mimetype']
for path in paths:
for root, dirs, files in os.walk(path):
for fname in files:
if fname == 'mimetype':
continue
fname = os.path.join(root, fname)
flist.append(fname)
os.chdir(save_cwd)
arch = ZipFile(archname, 'w')#, ZIP_DEFLATED) # FIXME
os.chdir(self.EPUB_DIR)
for fname in flist:
# . is bad for py24 under win,
# py 2.5 generates more sane entries for:
# './filename' and '.\\filename'
print(fname) # FIXME DEBUG
fname = os.path.normpath(fname)
arch.write(fname)
arch.close()
os.chdir(save_cwd)

return archname

def turn_zip_into_epub(zip_archive):
epub_full_name = zip_archive.strip('.zip') + '.epub'
Expand Down

0 comments on commit 8736984

Please sign in to comment.