I unzipped an epub
file to make some minor modifications. How can I repack it properly to be a valid epub
file from command line on Linux?
4 Answers
zip -rX ../my.epub mimetype META-INF/ OEBPS/
Without -X
you could get the following when validating it with EpubCheck:
ERROR: my.epub: Mimetype entry must not have an extra field in its ZIP header
If mimetype
is not the first in the epub
file EpubCheck prints the following:
ERROR: my.epub: Mimetype entry missing or not the first in archive
zip -X0 "my.epub" "mimetype"
zip -Xr "my.epub" "META-INF/" "OEBPS/"
As palacsint already pointed out, without the -X flag, one would run into the epubcheck error PKG_005
"The mimetype file has an extra field of length %1$s. The use of the extra field feature of the ZIP format is not permitted for the mimetype file."
because the mimetype file is being used as magical number within the compressed zip file, and the zip extra field feature (where zip programs and operating systems can place non-standardized data) would destroy the magical number mechanism (see https://github.com/IDPF/epubcheck/pull/497). The -0 flag makes sure that the mimetype file doesn't get compressed, which is also necessary for the mimetype file to work as a magical number, otherwise the data at the position where the mimetype file is expected could be compressed, scrambled gibberish.
A one-liner based on the previous answers, which will take the current dirname as the epub name, and the contents of the current dir as the epub: zip -rX "../$(basename "$(realpath .)").epub" mimetype $(ls|xargs echo|sed 's/mimetype//g')
-
2On a Mac,
realpath
is not available by default and you'll want to exclude the invisible.DS_Store
files… Making the command:zip -rX "../myprecious.epub" mimetype $(ls|xargs echo|sed 's/mimetype//g') -x *.DS_Store
. Commented Feb 21, 2019 at 20:24 -
To exclude any DS_Store in any directory use this pattern with the answer from @FabienSnauwaert
**/.DS_Store
– ow3nCommented Mar 4, 2023 at 16:46
I'm using this python script to batch repackage the files.
import os
import shutil
import zipfile
# Enter the paths
path_to_files = "/Users/matteo/Desktop/ebooks" # Path to directory containing epub packages
path_to_new_files = "/Users/matteo/Desktop/ebooks_conv" # Path to store new epub files in
# Function to store all filenames in a list
def extract_filename(path_to_files):
filenames = []
for f in os.listdir(path_to_files):
f_name, f_ext = os.path.splitext(f)
if f_ext == ".epub":
filenames.append(f_name)
filenames.sort()
return filenames
# Function to generate new epub files
def create_epub(filenames, path_to_files, path_to_new_files):
total_files = len(filenames)
for i, filename in enumerate(filenames, 1):
epub_path = os.path.join(path_to_files, filename + ".epub")
# Remove iTunesMetadata.plist if exists
plist_path = os.path.join(path_to_files, "iTunesMetadata.plist")
if os.path.exists(plist_path):
os.remove(plist_path)
# Copy the EPUB files to a temporary directory to avoid zipping recursively
temp_dir = os.path.join(path_to_new_files, filename)
shutil.copytree(epub_path, temp_dir)
# Create the new EPUB archive
with zipfile.ZipFile(os.path.join(path_to_new_files, filename + ".epub"), 'w') as new_epub:
for root, dirs, files in os.walk(temp_dir):
for file in files:
file_path = os.path.join(root, file)
# Write each file to the archive
new_epub.write(file_path, os.path.relpath(file_path, temp_dir))
# Clean up the temporary directory
shutil.rmtree(temp_dir)
print("File #", i, " has been processed successfully. Remaining files:", total_files - i)
filenames = extract_filename(path_to_files)
create_epub(filenames, path_to_files, path_to_new_files)