Created
June 19, 2011 08:21
-
-
Save poomon/1033956 to your computer and use it in GitHub Desktop.
convert an archive to another (simply unpack and pack again)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# This script is in public domain. | |
die() { echo $*; exit 1; } | |
archive() { | |
local action="$1" | |
local archive="$2" | |
local dir="$3" | |
local compressor | |
cd "$dir" || die "cd '$dir' failed" | |
case "$(echo "$archive" | tr '[A-Z]' '[a-z]')" in | |
*.tar.*) | |
case "$archive" in | |
*.gz) compressor=gzip ;; | |
*.bz2) compressor=bzip2 ;; | |
*.xz) compressor=xz ;; | |
*) die "unknown compressed file: '${archive}'" ;; | |
esac | |
pack_cmd="tar cpf - . | ${compressor} > '${archive}'" | |
unpack_cmd="${compressor} -dc '${archive}' | tar xpf -" | |
;; | |
*.tar) | |
pack_cmd="tar cpf '${archive}' ." | |
unpack_cmd="tar xpf '${archive}'" | |
;; | |
*.zip) | |
pack_cmd="zip -r '${archive}' ." | |
unpack_cmd="unzip '${archive}'" | |
;; | |
*.7z) | |
pack_cmd="7z a '${archive}' ." | |
unpack_cmd="7z x '${archive}'" | |
;; | |
*.rar) | |
pack_cmd="rar a -r -ow '${archive}' ." | |
unpack_cmd="unrar x -ow '${archive}'" | |
;; | |
*) | |
die "unknown archive '${archive}'" | |
;; | |
esac | |
case $action in | |
pack) eval "$pack_cmd" ;; | |
unpack) eval "$unpack_cmd" ;; | |
*) die ;; | |
esac | |
} | |
on_exit() { | |
rm -rf "$workdir" | |
} | |
trap on_exit EXIT INT TERM | |
from="$(readlink -f "$1")" | |
to="$(readlink -f "$2")" | |
workdir="$(mktemp -d --tmpdir "repack-${USER}.XXXXXXXXXX")" | |
[ -z "$workdir" ] && die "mktemp -d failed" | |
[ $# -ne 2 ] && die "Usage: ${0##*/} FROM_ARCHIVE TO_ARCHIVE" | |
archive unpack "$from" "$workdir" | |
archive pack "$to" "$workdir" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment