#!/usr/bin/env bash set -euo pipefail usage() { echo "Использование: $0 input.tar[.gz|.zst]|input.zip output.zip|output.tar[.gz] [--force]" >&2 } if [[ $# -lt 2 ]]; then usage exit 2 fi input=$1 output=$2 shift 2 force=0 for arg in "$@"; do case "$arg" in --force) force=1 ;; *) echo "Неизвестный аргумент: $arg" >&2 usage exit 2 ;; esac done if [[ ! -f "$input" ]]; then echo "Входной файл не найден: $input" >&2 exit 1 fi if [[ -e "$output" && $force -eq 0 ]]; then echo "Выходной файл уже существует: $output. Используйте --force" >&2 exit 1 fi command -v tar >/dev/null 2>&1 || { echo "Нужен tar" >&2; exit 1; } mkdir -p -- "$(dirname -- "$output")" shopt -s nocasematch case "$input" in *.zip) input_format=zip command -v 7z >/dev/null 2>&1 || { echo "Нужен 7z из 7-Zip" >&2; exit 1; } ;; *.tar|*.tar.gz|*.tgz|*.tar.zst|*.tzst) input_format=tar command -v gzip >/dev/null 2>&1 || { echo "Нужен gzip" >&2; exit 1; } ;; *) echo "Вход должен быть zip или tar" >&2 exit 2 ;; esac case "$output" in *.zip) output_format=zip ;; *.tar|*.tar.gz|*.tgz) output_format=tar ;; *) echo "Выход должен быть zip, tar или tar.gz" >&2 exit 2 ;; esac if [[ "$input_format" == "$output_format" ]]; then echo "Форматы входа и выхода совпадают" >&2 exit 2 fi tmp=$(mktemp -d) trap 'rm -rf -- "$tmp"' EXIT if [[ "$input_format" == "tar" ]]; then list_plain() { case "$input" in *.tar.zst|*.tzst) command -v zstd >/dev/null 2>&1 || { echo "Нужен zstd" >&2; exit 1; } zstd -dc -- "$input" | tar -tf - ;; *) tar -tf "$input" ;; esac } list_verbose() { case "$input" in *.tar.zst|*.tzst) zstd -dc -- "$input" | tar -tvf - ;; *) tar -tvf "$input" ;; esac } bad=0 while IFS= read -r name; do case "$name" in /*|..|../*|*/../*|*/..) echo "Небезопасный путь: $name" >&2 bad=1 ;; esac done < <(list_plain) while IFS= read -r line; do case "${line:0:1}" in l|h) echo "Ссылка в архиве: $line" >&2 bad=1 ;; esac done < <(list_verbose) if [[ $bad -ne 0 ]]; then echo "Конвертация отменена." >&2 exit 2 fi case "$input" in *.tar.zst|*.tzst) zstd -dc -- "$input" | tar -xf - -C "$tmp" ;; *) tar -xf "$input" -C "$tmp" ;; esac else bad=0 while IFS= read -r name; do case "$name" in /*|..|../*|*/../*|*/..) echo "Небезопасный путь: $name" >&2 bad=1 ;; esac done < <(7z l -slt -- "$input" | awk '/^Path = / { print substr($0, 8) }') if [[ $bad -ne 0 ]]; then echo "Конвертация отменена." >&2 exit 2 fi 7z x -y -o"$tmp" -- "$input" >/dev/null fi link_found=0 while IFS= read -r link; do link_found=1 break done < <(find "$tmp" -type l -print) if [[ $link_found -ne 0 ]]; then echo "В архиве есть символические ссылки, конвертация отменена." >&2 exit 2 fi output_abs=$(cd -- "$(dirname -- "$output")" && pwd -P)/$(basename -- "$output") if [[ "$output_format" == "zip" ]]; then command -v zip >/dev/null 2>&1 || { echo "Нужен zip" >&2; exit 1; } (cd "$tmp" && zip -r -q "$output_abs" .) else case "$output" in *.tar.gz|*.tgz) tar -czf "$output_abs" -C "$tmp" . ;; *) tar -cf "$output_abs" -C "$tmp" . ;; esac fi rm -rf -- "$tmp" trap - EXIT echo "Готово: $output"