#!/usr/bin/env bash set -euo pipefail usage() { echo "Использование: $0 input.zip|input.7z output.zip|output.7z [--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 7z >/dev/null 2>&1 || { echo "Нужен 7z из 7-Zip" >&2; exit 1; } mkdir -p -- "$(dirname -- "$output")" shopt -s nocasematch case "$input" in *.zip) input_format=zip ;; *.7z) input_format=7z ;; *) echo "Вход должен быть zip или 7z" >&2 exit 2 ;; esac case "$output" in *.zip) output_format=zip ;; *.7z) output_format=7z ;; *) echo "Выход должен быть zip или 7z" >&2 exit 2 ;; esac if [[ "$input_format" == "$output_format" ]]; then echo "Форматы входа и выхода совпадают" >&2 exit 2 fi if [[ "$output_format" == "7z" ]]; then 7z a -t7z -mx=9 -- "$output" "$input" else tmp=$(mktemp -d) trap 'rm -rf -- "$tmp"' EXIT 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 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") (cd "$tmp" && 7z a -tzip -mx=9 -- "$output_abs" .) rm -rf -- "$tmp" trap - EXIT fi echo "Готово: $output"