-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir2cbz
executable file
·75 lines (66 loc) · 2.14 KB
/
dir2cbz
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env bash
# Converts every given directory to a CBZ-file.
# Can read a directory list from stdin or parameters.
# The script will never delete the original directories.
#
# EXAMPLE:
#
# If a directory structure is the following:
#
# Volume 01
# |- Page 01.jpg
# |- Page 02.jpg
# '- Page 03.jpg
# Volume 02
# |- Page 01.jpg
# |- Page 02.jpg
# '- Page 03.jpg
#
# ... and the following arguments are given:
#
# $ dir2cbz "/path/to/Volume 01" "/path/to/Volume 02"
#
# ... dir2cbz will convert directories Volume 01 and Volume 02
# into CBZ files "Volume 01.cbz" and "Volume 02.cbz".
# Resulting archive format, defaulting to zip. If rar or 7z are used, remember
# to update DEST_TYPE to match by setting it to cbr or cb7
ARCHIVE_TYPE=zip
# Resulting comic book archive format, must match with ARCHIVE_TYPE, eg.
# ARCHIVE_TYPE=rar -> DEST_TYPE=cbr.
DEST_TYPE=cbz
# 7z options used when archiving the directory, do not change '-t'. '-mx' can
# be changed to change compression ratio (0=no compression, 9=max compression).
OPTS=(-t$ARCHIVE_TYPE -mx=0)
convert_to_archive () {
dir="$(readlink -f "$1")"
# Check if the given argument is a directory
if [[ ! -d "$dir" ]]; then continue; fi
# If the destination file already exists, in part or cbz format,
# do not overwrite either and skip archival.
if [[ -f "$dir.$DEST_TYPE" ]]; then
echo "WARNING: file '$dir.$DEST_TYPE' already exists, skipping."
return
fi
if [[ -f "$dir.part" ]]; then
echo "WARNING: file '$dir.part' already exists, skipping."
return
fi
# Go inside directory tree so that the resulting archive has proper
# root directory, archive working directory files with '.' and move
# part-file to the destination type (cbz) if 7z succeeds.
cd "$dir" \
&& 7z a "${OPTS[@]}" "$dir.part" . > /dev/null 2>&1 \
&& mv "$dir.part" "$dir.$DEST_TYPE"
cd - > /dev/null
}
# If parameters are given to the script, cycle through them.
# Otherwise read stin.
if [[ ! -z "$1" ]]; then
for dir in "$@"; do
convert_to_archive "$dir"
done
else
while read dir; do
convert_to_archive "$dir"
done
fi