-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixCRCerror.sh
More file actions
executable file
·52 lines (41 loc) · 1.61 KB
/
fixCRCerror.sh
File metadata and controls
executable file
·52 lines (41 loc) · 1.61 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
#!/bin/bash
BIN7Z="7z"
# Check if 7-Zip is installed
if ! type "$BIN7Z" > /dev/null; then
printf "error: you have to install 7z!\n" 1>&2
exit 1
fi
# Check if the corrupted archive is provided
if [ -z "$1" ]; then
printf "usage: %s CORRUPTED_DFIR_ORC\n" "$0" 1>&2
exit 1
fi
# Check if the given argument is a file
if [ ! -f "$1" ]; then
printf "%s: %s: No such file or directory\n" "$0" "$1" 1>&2
exit 1
fi
# Get paths of the given file and its parent directory
file_path=$(realpath "$1")
file_name=$(basename "$file_path")
root_path=$(dirname "$file_path")
# Build paths for output
tmp_dir=${root_path}/${file_name/.7z/.tmp}
fixed_file_path=${root_path}/${file_name/.7z/.fixed.7z}
fixed_file_name=$(basename "$fixed_file_path")
corrupted_file_path=${root_path}/${file_name/.7z/.corrupted.7z}
corrupted_file_name=$(basename "$corrupted_file_path")
# Extracting the corrupted archive into the temporary folder
mv "${file_path}" "${corrupted_file_path}"
printf "[*] Corrupted file: %s (%s)\n" "${corrupted_file_name}" \
"$(du -sh "${corrupted_file_path}" | cut -f1)"
printf "[*] Extracting '%s' using '%s'\n" "${corrupted_file_name}" "${BIN7Z}"
${BIN7Z} x "${corrupted_file_path}" -O"${tmp_dir}" > /dev/null
# Recreating a fresh archive
printf "[*] Recreating '%s' using '%s'\n" "${fixed_file_name}" "${BIN7Z}"
cd "${tmp_dir}" && ${BIN7Z} a "${fixed_file_path}" * >/dev/null
printf "[*] Fixed file: %s (%s)\n" "${fixed_file_name}" \
"$(du -sh "${fixed_file_path}" | cut -f1)"
mv "${fixed_file_path}" "${file_path}"
printf "[*] Cleaning extracted files (%s)\n" "$(basename "${tmp_dir}")"
rm -r "${tmp_dir}"