4 Feb 2011 AlanHorkan   » (Master)

Comics: Convert CBR to CBZ

Comics are great. I'd gotten out of the habit but I still enjoy picking up a good Batman story now and again, but other than that it is hard to know what to pick. The painted comics of Alex Ross (Kingdom Come) are beautiful, the stories of Warren Ellis (Transmetropolitan) are compelling.

Can you judge a book by it's cover? If it is a graphic design book, or art book I think you can and with comics I often do choose on that basis (and all too often get caught out when the interior art is entirely different from the cover). In the long run though it is stories and the writers that keep me coming back and if I particularly like the style of the artist that is a bonus.

Chatting with the local equivalent of Comic Book Guy is one way to get ideas of what to try but picking a book off a shelf and having a look is usually the best way. The internet has provided us with an even bigger bookshelf to flick through, and there are groups who scan the latest comic books and package them up for easy sampling, usually a series of JPEG image files packaged together as a single convenient archive. Very often this archive they are packaged in use RAR compression CBR (Comic Book RAR archive), but other times the container uses Zip compression CBZ (Comic Book Zip archive). The essential difference is that Zip is free and RAR is proprietary.

I wanted to convert these CBR files to CBZ. I could do that by unpacking and repacking the files on a case by case basis but that is repetative and uninteresting. Instead I decided to make things more complicated and come up with a more general automated solution. That solution might be of use to others, especially those who do not know how to write MSDOS batch files, so I'm sharing it. On the internet someone else there is almost always someone else who has the same idea as you, and I was able to find something very close: Andrew Armstrong had already written a Mass Zip, RAR to 7Zip Recompression Batch File and all I needed to do was make some small adjustments so that it would to convert RAR files and CBR files into CBZ files. If you want details of what exactly is happening you should read his detailed description. It took me far less time to make my changes than it to write them up and explain them, the hard work was already done.

The script requires the excellent free compression program 7-zip and you must have it installed. To keep things simple we assume 7-zip is installed in the default location C:\Program Files\7-zip\7z and we do not try to check. If for example you were using a German version of Microsoft Windows 7-zip might be installed in a program called "Programme" rather than "Program Files" or if you wanted to use a portable flash drive and use Portable 7-zip you would also need to change the program before it would work. (We could try to avoid the first problem by checking to see if %ProgramFiles% is C:\Program Files as expected.

Another known issue the sometimes causes the script to fail is when file path names are too long. Microsoft Windows limits file names to 260 characters in length. The script creates a temporary folder based on the name of the CBR and if there is a subfolder inside the CBR the filename can sometimes be too long.

As this script is intended to be used to unpack not just any generic RAR file but a Comic Book Archive there are other changes we can include. Creators of Comic Book archives compress a whole folder instead of picking out just the picture files they want, and all sorts of other things get included by mistake. Microsoft Windows includes a hidden file called Thumbs.db which stores thumbnail images of your files. We can safely delete this, Windows will recreate it if the comic book archive is ever unpacked and it is needed. To do this I added the line DEL /F /S /Q Thumbs.db (which is short for DELete by Force, in all Subdirectories, and do it Quietly without reporting any errors).

The scanners who create Comic Book archives often like to include a banner image saying who they are and often with a message such as reminding readers that if they like the comic they should buy it too. These files are often given a file name starting with 'Z' so that it will appear last in the sequence so I added another line to delete any file that starts with the letter 'Z' DEL /F /S /Q z*.*. If the banner image has a different name this won't work but it is an example of what you can do to fit the generic scripts to better fit that task.

One change I have made might seem surprising. I have set the Zip compression to level zero, which is no compression at all (-mx=0). I could save a small amount of space by using the maximum amount of compression but this would take longer to pack the files, and more importantly it would make it slower each and every time you want to unpack and read the files. The Zip archive convenient container to make things easier to organize, and JPEG files are already quite heavily compressed.

If you want to try it out for your self, copy the commands below into a text file, change the name to cbr2cbz.bat or similar, put it in a folder with some CBR or RAR files and away you go.



@ECHO OFF
REM Based on 
REM Mass Zip, RAR to 7Zip Recompression Batch File by Andrew Armstrong
REM http://aarmstrong.org/tutorials/mass-zip-rar-to-7zip-recompression-batch-file
REM
REM Find all .cbr and .rar files in the current directory, recompress them into .cbz and .zip files - but do not delete the originals.

ECHO Searching for CBR and RAR files to recompress into CBZ (Comic Book Zip) archives.

for %%F in (*.cbr *.rar) do (
ECHO Found %%F to recompress...
REM Extract...
"C:\Program Files\7-zip\7z" x "%%F" -o"%%F contents"
REM Does the directory exist? has 7zip created it correctly?
IF EXIST "%%F contents" (
REM Change directory, create zip of contents of directory...
CD "%%F contents"
REM remove banners 
DEL /F /S /Q z*.*
REM Delete Thumbnail files. 
DEL /F /S /Q Thumbs.db
REM compression level is 0, store only. 
"C:\Program Files\7-zip\7z" a -tzip "../%%~nF.cbz" * -mx=0
CD ..
REM Delete the temporary extraction folder
RMDIR "%%F contents" /S /Q
ECHO Recompressed %%F to %%~nF.zip
)
)
ECHO Search ended.

PAUSE
CLS
EXIT

Syndicated 2011-02-01 01:57:35 from Alan Horkan

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!