-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepacktotar.sh
More file actions
64 lines (53 loc) · 1.69 KB
/
repacktotar.sh
File metadata and controls
64 lines (53 loc) · 1.69 KB
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
#!/bin/bash
#sudo apt install p7zip-full p7zip-rar libarchive-tools
# GNU tar takes longer because it is scanning the entire original archive before appending.
# The bsdtar command from libarchive just immediately appends the new data.
# https://superuser.com/questions/1456587/why-does-each-subsequent-append-to-a-tar-archive-take-longer
controlc() {
exit 1
}
trap controlc SIGINT
declare -a archivefiles
IFS=$'\n'
for line in $(find . -type f \( -name "*.7z" -o -name "*.rar" -o -name "*.zip" \) 2>/dev/null | sort -n); do
archivefiles+=("$line")
done
for ((archno = 0; archno < ${#archivefiles[@]}; archno++)); do
archivefile="${archivefiles[$archno]}"
echo -e "\nProcessing $archivefile"
echo -n "testing $archivefile..."
7z t "$archivefile" &>/dev/null
if [ ! $? -eq 0 ]; then
echo "original ${archivefiles[$archno]} is damaged, status: $?"
exit 1
fi
echo "OK"
tarfile="${archivefile%.*}.tar"
if [ -f "$tarfile" ]; then
rm -f "$tarfile"
fi
# extract to a temp directory
mkdir -p ./temp
tempdir="./temp"
echo "extracting $archivefile to $tempdir"
7z x "$archivefile" -o"$tempdir"
if [ ! $? -eq 0 ]; then
echo "failed to extract $archivefile, status: $?"
exit 1
fi
# create a tar archive
echo "creating $tarfile"
tar cf "$tarfile" -C "$tempdir" .
if [ ! $? -eq 0 ]; then
echo "failed to create $tarfile, status: $?"
exit 1
fi
echo -ne "\ntesting $tarfile..."
if ! tar -tf "$tarfile" &>/dev/null; then
echo "created $tarfile is damaged, status: $?"
exit 1
fi
echo "OK"
done
# delete the temp directory on exit
trap "rm -rf $tempdir" EXIT