File size: 2,406 Bytes
a37b18d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import zipfile
import os
import shutil
import re
def extract_epubs_from_zip(zip_path, extract_to):
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
for file_info in zip_ref.infolist():
if file_info.filename.endswith('.epub'):
# Extract the EPUB file directly to the target directory
zip_ref.extract(file_info, extract_to)
# Move the EPUB file to the target directory
extracted_path = os.path.join(extract_to, file_info.filename)
shutil.move(extracted_path, os.path.join(extract_to, os.path.basename(file_info.filename)))
def unpack_zips_and_extract_epubs(root_dir, epubs_dir):
if not os.path.exists(epubs_dir):
os.makedirs(epubs_dir)
for root, dirs, files in os.walk(root_dir):
for file in files:
if file.endswith('.zip'):
zip_path = os.path.join(root, file)
temp_extract_dir = os.path.join(root, 'temp_extract')
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(temp_extract_dir)
for temp_root, temp_dirs, temp_files in os.walk(temp_extract_dir):
for temp_file in temp_files:
if temp_file.endswith('.epub'):
epub_path = os.path.join(temp_root, temp_file)
shutil.move(epub_path, os.path.join(epubs_dir, os.path.basename(epub_path)))
elif temp_file.endswith('.zip'):
nested_zip_path = os.path.join(temp_root, temp_file)
if zipfile.is_zipfile(nested_zip_path):
extract_epubs_from_zip(nested_zip_path, epubs_dir)
# Clean up the temporary extraction directory
shutil.rmtree(temp_extract_dir)
# Remove any empty directories in the epubs directory
for root, dirs, files in os.walk(epubs_dir, topdown=False):
for dir in dirs:
dir_path = os.path.join(root, dir)
if not os.listdir(dir_path):
os.rmdir(dir_path)
if __name__ == "__main__":
root_directory = '.' # Change this to the directory containing your ZIP files
epubs_directory = './epubs'
unpack_zips_and_extract_epubs(root_directory, epubs_directory) |