File size: 4,128 Bytes
6225c63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
set -euo pipefail

usage() {
    echo "Usage: $0 <tar_root_dir> <max_processes> <delete_tar_file>"
    echo "  tar_root_dir   Root directory to recursively search for .tar files"
    echo "  max_processes  Number of parallel decompression processes, must be a positive integer"
    echo "  delete_tar_file  Whether to delete tar files after decompression: 0=Keep 1=Delete"
}

if [ "$#" -ne 3 ]; then
    usage
    exit 1
fi

TAR_ROOT_DIR="$1"
MAX_PROCESSES="$2"
DELETE_TAR_FILE="$3"

if [ ! -d "${TAR_ROOT_DIR}" ]; then
    echo "Error: tar_root_dir does not exist or is not a directory: ${TAR_ROOT_DIR}" >&2
    exit 1
fi

if ! [[ "${MAX_PROCESSES}" =~ ^[1-9][0-9]*$ ]]; then
    echo "Error: max_processes must be a positive integer: ${MAX_PROCESSES}" >&2
    exit 1
fi

if ! [[ "${DELETE_TAR_FILE}" =~ ^[01]$ ]]; then
    echo "Error: delete_tar_file must be 0 or 1: ${DELETE_TAR_FILE}" >&2
    exit 1
fi

# Define global statistical variables
total_tar_num=0
finished_tar_num=0
fail_tar_num=0

# ===================== Core Decompression Function =====================
unpack_tar() {
    local tar_file="$1"
    local tar_dir=$(dirname "${tar_file}")
    local tar_name=$(basename "${tar_file}")

    # Core logic: cd to the directory where the tar package is located to perform decompression and ensure correct path restoration
    cd "${tar_dir}" && tar xvf "${tar_name}"
    local unpack_status=$?

    if [ ${unpack_status} -eq 0 ]; then
        # Count +1 for successful decompression
        finished_tar_num=$((finished_tar_num+1))
        local progress_percent=$(( finished_tar_num * 100 / total_tar_num ))
        echo -e "\rDecompression progress: [${finished_tar_num}/${total_tar_num}] ${progress_percent}% | Success: ${tar_file}"
        
        # Delete tar package after successful decompression if configured
        if [ ${DELETE_TAR_FILE} -eq 1 ]; then
            rm -f "${tar_file}"
        fi
    else
        # Count +1 for failed decompression
        fail_tar_num=$((fail_tar_num+1))
        echo -e "\nDecompression failed: ${tar_file}"
    fi
}

# ===================== Step 1: Recursively find all tar packages and pre-count =====================
echo -e "Start recursively searching for all .tar files under [${TAR_ROOT_DIR}]..."
# Recursively find all .tar files, sort by path, compatible with all path formats
tar_files=()
while IFS= read -r file; do
    tar_files+=("$file")
done < <(find "${TAR_ROOT_DIR}" -type f -name "*.tar" | sort)

# Count total number
total_tar_num=${#tar_files[@]}
if [ ${total_tar_num} -eq 0 ]; then
    echo -e "No .tar files found, script exit!"
    exit 0
fi

echo -e "Search completed, total [${total_tar_num}] tar files found"
echo -e "Start multi-process parallel decompression, process count: ${MAX_PROCESSES} | Delete tar files after decompression: $( [ ${DELETE_TAR_FILE} -eq 1 ] && echo "Yes" || echo "No" )"
echo -e "==================================================\n"

# ===================== Step 2: Multi-process parallel decompression + process limit =====================
for tar_file in "${tar_files[@]}"; do
    # Execute decompression function in background parallel
    unpack_tar "${tar_file}" &

    # Core rate limit: Wait for any background process to complete when the process count reaches the upper limit
    while (( $(jobs -p | wc -l) >= MAX_PROCESSES )); do
        wait -n
    done
done

# ===================== Final step: Wait for all background decompression processes to complete =====================
echo -e "\n\nWaiting for all remaining decompression processes to complete... (Remaining: $(jobs -p | wc -l) )"
wait

# ===================== Final decompression completion summary =====================
echo -e "\n=================================================="
echo -e "All decompression tasks completed"
echo -e "Decompression statistics: Total=${total_tar_num} | Success=${finished_tar_num} | Failed=${fail_tar_num}"
echo -e "Decompressed files have been restored according to the original directory structure, no manual path adjustment required!"
echo -e "=================================================="