Skip to content

Instantly share code, notes, and snippets.

@hugobuddel
Created May 3, 2016 07:58
Show Gist options
  • Save hugobuddel/64b6c865cf4eeafe72e42e2b7352662b to your computer and use it in GitHub Desktop.
Save hugobuddel/64b6c865cf4eeafe72e42e2b7352662b to your computer and use it in GitHub Desktop.
Fixes broken epub zip files.
import zipfile
import os
"""
This file was released on the tenth International Day Against DRM,
May 3th 2016, to help others read the epub books they bought in the
way they like.
Many ebook distributors add Digital Restrictions Management (DRM)
to their epub files. This DRM can often be removed easily by
various tools, so it is safe to buy those books.
However, some distributors create broken zip files (epub files are
zip files) that confuse most zip file extractors (including tools
to remove DRM). Fix those broken epub files with this script!
Note that this script does not remove or circumvent any DRM. It
only fixes broken zipfiles. Use at your own risk.
"""
"""
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org>
"""
def fix_broken_drm_epub_zip(filename, filename_fixed=None):
"""
Reads in a zip archive, tries to decompress all files and
recompresses the correct files.
The order of the files in the archive is unchanged. This is
essential for zipfiles.
"""
fnin = filename
if filename_fixed:
fnout = filename_fixed
elif ".epub" in fnin:
fnout = fnin.replace(".epub", ".fixed.epub")
else:
fnout = fnin + ".fixed"
if os.path.exists(fnout):
return fnout
if not zipfile.is_zipfile(fnin):
return filename
files = []
broken_zip = False
with zipfile.ZipFile(fnin) as zin:
for item in zin.infolist():
try:
filedata = zin.read(item.filename)
files.append((item, filedata))
except zipfile.BadZipFile:
broken_zip = True
if broken_zip:
with zipfile.ZipFile(fnout, 'w') as zout:
for (item, filedata) in files:
zout.writestr(item, filedata)
return fnout
else:
return filename
def fix_all_broken_drm_epub_zips(dirname=".", quiet=False):
"""
Fixes all broken epub files
"""
filenames_tofix = [fn for fn in os.listdir(dirname) if fn.endswith(".epub") and not fn.endswith(".fixed.epub")]
filenames_fixed = [fix_broken_drm_epub_zip(fn) for fn in filenames_tofix]
for (fnin, fnout) in zip(filenames_tofix, filenames_fixed):
if fnin != fnout:
print("Fixed %s to %s." % (fnin, fnout))
if __name__ == '__main__':
fix_all_broken_drm_epub_zips()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment