ebooklib icon indicating copy to clipboard operation
ebooklib copied to clipboard

How to handle Epub read encountering "Bad Zip file" error?

Open actualizeai opened this issue 3 years ago • 8 comments

Hi!

I am using ebooklib to read epub.images files from project gutenberg. For most .epub files, i am able to open & read the files fines.

But intermittently, i am getting error: "Bad Zip file". The trace is as under: File "/Users/jaideepadhvaryu/.pyenv/versions/3.9.2/lib/python3.9/site-packages/ebooklib/epub.py", line 1686, in _load self.zf = zipfile.ZipFile(self.file_name, 'r', compression=zipfile.ZIP_DEFLATED, allowZip64=True) File "/Users/jaideepadhvaryu/.pyenv/versions/3.9.2/lib/python3.9/zipfile.py", line 1257, in init self._RealGetContents() File "/Users/jaideepadhvaryu/.pyenv/versions/3.9.2/lib/python3.9/zipfile.py", line 1324, in _RealGetContents raise BadZipFile("File is not a zip file") zipfile.BadZipFile: File is not a zip file

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/Users/jaideepadhvaryu/Documents/PracticeCodes/readepub/epubmeta.py", line 69, in ebook = epub.read_epub(epubname) File "/Users/jaideepadhvaryu/.pyenv/versions/3.9.2/lib/python3.9/site-packages/ebooklib/epub.py", line 1739, in read_epub book = reader.load() File "/Users/jaideepadhvaryu/.pyenv/versions/3.9.2/lib/python3.9/site-packages/ebooklib/epub.py", line 1397, in load self._load() File "/Users/jaideepadhvaryu/.pyenv/versions/3.9.2/lib/python3.9/site-packages/ebooklib/epub.py", line 1688, in _load raise EpubException(0, 'Bad Zip file') ebooklib.epub.EpubException: 'Bad Zip file'

I can understand that there can be problems with epub files.

I am keen to get guidance on how to handle such errors in opening epub files?

Thank you.

actualizeai avatar Mar 31 '21 10:03 actualizeai

Hard to say but this is the error you would get if you try to load non epub files. Can you unzip that file? Just rename the extension to .zip and try to extract it or just unzip it from the command line. If you get error than it is not valid ZIP file (Epub files are really ZIP files).

aerkalov avatar Jun 18 '22 22:06 aerkalov

I also encountered this error The book I used is here

arealhorse avatar Jul 15 '22 15:07 arealhorse

I have the feeling this might not be an issue with the epub file.

I started an interactive session and I was able to create a zipfile object of the epub file which proved difficult (I used the same args as ebooklib, just to be sure).

Python 3.10.6 (main, Nov  2 2022, 18:53:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import zipfile
>>> f = zipfile.ZipFile("file.epub", 'r', compression=zipfile.ZIP_DEFLATED, allowZip64=True)
>>> f
<zipfile.ZipFile filename='file.epub' mode='r'>

I tried this both inside and outside the virtual enviroment where I have ebooklib installed (just to narrow down possible dependency issues); same result.

Epubs created with epublib are read just great.

Do you guys have any thoughts about this?

lb803 avatar Nov 23 '22 18:11 lb803

I have the feeling this might not be an issue with the epub file.

I started an interactive session and I was able to create a zipfile object of the epub file which proved difficult (I used the same args as ebooklib, just to be sure).

Python 3.10.6 (main, Nov  2 2022, 18:53:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import zipfile
>>> f = zipfile.ZipFile("file.epub", 'r', compression=zipfile.ZIP_DEFLATED, allowZip64=True)
>>> f
<zipfile.ZipFile filename='file.epub' mode='r'>

I tried this both inside and outside the virtual enviroment where I have ebooklib installed (just to narrow down possible dependency issues); same result.

Epubs created with epublib are read just great.

Do you guys have any thoughts about this?

OK, I found the solution to my own issue (hopefully this helps others):

The epub.read_epub() method accepts a file path (string) as argument, whereas I was trying to feed it a file object.

lb803 avatar Nov 23 '22 18:11 lb803

I have the feeling this might not be an issue with the epub file. I started an interactive session and I was able to create a zipfile object of the epub file which proved difficult (I used the same args as ebooklib, just to be sure).

Python 3.10.6 (main, Nov  2 2022, 18:53:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import zipfile
>>> f = zipfile.ZipFile("file.epub", 'r', compression=zipfile.ZIP_DEFLATED, allowZip64=True)
>>> f
<zipfile.ZipFile filename='file.epub' mode='r'>

I tried this both inside and outside the virtual enviroment where I have ebooklib installed (just to narrow down possible dependency issues); same result. Epubs created with epublib are read just great. Do you guys have any thoughts about this?

OK, I found the solution to my own issue (hopefully this helps others):

The epub.read_epub() method accepts a file path (string) as argument, whereas I was trying to feed it a file object.

Hello, please consider to take a look on this issue, i will be very thankful, maybe you can help.

https://www.reddit.com/r/Calibre/comments/10axudh/epub_to_pdf_pleas_help/

paweltylman avatar Jan 20 '23 00:01 paweltylman

   您好!您的来信我已接受,我会尽快回复您。

c1924959470 avatar Jan 20 '23 00:01 c1924959470

I have the feeling this might not be an issue with the epub file. I started an interactive session and I was able to create a zipfile object of the epub file which proved difficult (I used the same args as ebooklib, just to be sure).

Python 3.10.6 (main, Nov  2 2022, 18:53:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import zipfile
>>> f = zipfile.ZipFile("file.epub", 'r', compression=zipfile.ZIP_DEFLATED, allowZip64=True)
>>> f
<zipfile.ZipFile filename='file.epub' mode='r'>

I tried this both inside and outside the virtual enviroment where I have ebooklib installed (just to narrow down possible dependency issues); same result. Epubs created with epublib are read just great. Do you guys have any thoughts about this?

OK, I found the solution to my own issue (hopefully this helps others):

The epub.read_epub() method accepts a file path (string) as argument, whereas I was trying to feed it a file object.

In my case, the problem was in file paths having escape sequences in them, i.e. \ with spaces. Dodged it by renaming the files temporarily to have underbars and then reverting them back. Edit: Also faulty epub file can cause this. Check with your sources!

boolYikes avatar Dec 21 '23 06:12 boolYikes