File size: 990 Bytes
ae7e5f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ARCHIVE_DIR="${ROOT_DIR}/archives"

for archive in "${ARCHIVE_DIR}"/*.zip; do
  [ -e "${archive}" ] || continue
  echo "[unpack] ${archive}"
  split_head="${archive%.zip}.z01"
  if [ -f "${split_head}" ]; then
    if command -v 7z >/dev/null 2>&1; then
      7z x "${archive}" -o"${ROOT_DIR}"
      continue
    fi
    if command -v zip >/dev/null 2>&1 && command -v unzip >/dev/null 2>&1; then
      merged_zip="${ROOT_DIR}/.$(basename "${archive%.zip}")_merged.zip"
      rm -f "${merged_zip}"
      zip "${archive}" -s 0 --out "${merged_zip}"
      unzip -oq "${merged_zip}" -d "${ROOT_DIR}"
      rm -f "${merged_zip}"
      continue
    fi
    echo "[skip] need 7z or zip+unzip for split archive ${archive}" >&2
    continue
  fi

  if command -v unzip >/dev/null 2>&1; then
    unzip -oq "${archive}" -d "${ROOT_DIR}"
  else
    python3 -m zipfile -e "${archive}" "${ROOT_DIR}"
  fi
done