TechSomething

convert cbz file to epub

what is a cbz/cbr file: #

a .cbz file is a compressed archive with files inside

cbz are Comic Book Zip files
cbz are Comic Book Rar files

They usually contain a folder and the filenames of the pages:

comic_title.cbz

comic_title
|-- page1.jpg
|-- page2.jpg
|-- page3.jpg
'-- page4.jpg

cbz to epub conversion: #

conversion: #

to convert a cbz to epub (for max compatibility) I've triedto use Calibre but the results were underwhelming,
using cbr_to_epub (https://github.com/rafalcymerys/cbr_to_epub) is way better:

cbr_to_epub -i "comic_title.cbz" --tile "Comic Title" --author "The Author"

cleanup: #

then you will have 2 files:

comic_title.cbz
comic_title.cbz.epub

then you can bulk rename the converted files:

find . -name "*.epub" -execdir rename 's/.cbz//g' "{}" \;

title: #

The title of the epub is in the metadata and is what the programs reading your epub will cosider for the title.

My filename was something like this: "ComicName IssueNumber ComicTitle.cbz"

so I wanted the filename without the extension to be the title of my epub.

script: #

I am using a script to do that, recursively, in your folder:

#!/usr/bin/env bash

author="Your Author"

for i in *.cbz; do
title=$(echo $i | cut -d "." -f 1)
cbr_to_epub -i "$i" --title "$title" --author "$author"
done

find . -name "*.epub" -execdir rename 's/.cbz//g' "{}" \;

for i in *.cbr; do
title=$(echo $i | cut -d "." -f 1)
cbr_to_epub -i "$i" --title "$title" --author "$author"
done

find . -name "*.epub" -execdir rename 's/.cbr//g' "{}" \;


#using: https://github.com/rafalcymerys/cbr_to_epub

NB: the script won't take into account some cases, for example when a cbz file is not using zip or when we have errors due to filenames inside the archives
it was too much effort to also consider these cases in the script

issues: #

since a .cbz file is a collection of jpgs in a folder,
and the epub will be built following the filename order,
if we have a broken order, for example:

|-- Comic_Book
| |-- 000.jpg
| |-- 1-Title_One-(0).jpg
| |-- 1-Title_One-(1).jpg
| |-- 1-Title_One-(10).jpg
| |-- 1-Title_One-(11).jpg
| |-- 1-Title_One-(12).jpg
| |-- 1-Title_One-(2).jpg
| |-- 1-Title_One-(21).jpg
| |-- 1-Title_One-....jpg

the file order will be followed even if it's clearly wrong,
in thi case the pages will be mixed up.

000.jpg is the cover.

For this case I've created a script that will fix my issue with filenames,
it will work only on .cbz files since my issue was only on those:

#!/usr/bin/env bash

# 0: WARNING
#
read -p "This process is destructive, I will remove your .cbz files, are you sure you want to continue, do you have a backup? " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "OK, going on.."
else
echo "Exiting"
exit
fi

# 1: decompress everything
#
for i in *.cbz; do
title=$(echo $i | cut -d "." -f 1)
unzip "$i" -d "$title"
rm "$i"
done


# 2: fix filenames
#
find . -type f -name "*\(0\)*" -execdir rename 's/\(0\)/\(00\)/g' "{}" \;
find . -type f -name "*\(1\)*" -execdir rename 's/\(1\)/\(01\)/g' "{}" \;
find . -type f -name "*\(2\)*" -execdir rename 's/\(2\)/\(02\)/g' "{}" \;
find . -type f -name "*\(3\)*" -execdir rename 's/\(3\)/\(03\)/g' "{}" \;
find . -type f -name "*\(4\)*" -execdir rename 's/\(4\)/\(04\)/g' "{}" \;
find . -type f -name "*\(5\)*" -execdir rename 's/\(5\)/\(05\)/g' "{}" \;
find . -type f -name "*\(6\)*" -execdir rename 's/\(6\)/\(06\)/g' "{}" \;
find . -type f -name "*\(7\)*" -execdir rename 's/\(7\)/\(07\)/g' "{}" \;
find . -type f -name "*\(8\)*" -execdir rename 's/\(8\)/\(08\)/g' "{}" \;
find . -type f -name "*\(9\)*" -execdir rename 's/\(9\)/\(09\)/g' "{}" \;


# 3: recompress the folders and delete them
#

find . -type d -depth 1 > ./dir_list.txt

cat ./dir_list.txt

while IFS= read -r line; do # Whitespace-safe EXCEPT newlines
zip -r "$line.cbz" "$line"
rm -rf "$line"
done < ./dir_list.txt

rm ./dir_list.txt


# 4: convert to epub and fix filename
#
author="Your Author"

for i in *.cbz; do
title=$(echo $i | cut -d "." -f 1)
cbr_to_epub -i "$i" --title "$title" --author "$author"
done

find . -name "*.epub" -execdir rename 's/.cbz//g' "{}" \;

problematic cases: #

case 1: broken chars in the archives #

our cbz file has non utf8 chars inside the archive

decompress fip file

unzip "comic_title.cbz" -d "comic_title"

if you are on macOS and have decompress errors due to illegal chars in the filename, es:

checkdir error:  cannot create comic_title
                 Illegal byte sequence
                 unable to process comic_title/page1_text??_text.jpg

use ditto to manage filename format errors:

ditto -V -x -k --sequesterRsrc --rsrc "comic_title.cbz" "comic_title"

then go into the folder and sanitize the filenames with detox:

detox -r -v .

at this point you can re-create your cbz from the fixed folder:

zip -r "comic_title.cbz" "comic_title"

case 2: cbz is not in zip format #

.rar files are not compatible with cbr_to_epub:

decompress your rar archive:

unrar x "comic_title.cbz"

then just recreate the cbz file using zip:

zip -r "comic_title.cbz" "comic_title"