35. Compressor / Descompressor universal para terminal!
Existe o FileRoller, é claro: um excelente arquivador universal para o Nautilus/GNOME, mas para o terminal não existe. Você tem que usar uma miríade de comandos diversos. Por isso desenvolvi um script universal.
Coloquei o script no GitHub Gist, só copiar, colar, salvar como zap.sh, embora a extensão não seja de todo necessária, podendo salvar apenas como zap mesmo.
Eu salvei esse conteúdo dentro do meu .bash_aliases, assim eu posso usar os comandos zap/unzap sem ter que dar load no script. Se você fez isso, digite zap ou unzap pra obter ajuda sobre o uso do script.
Para poder usá-lo, instale todos os arquivadores possíveis:
$ sudo apt install arj bzip2 cabextract gzip lzip lzma lzop ncompress p7zip p7zip-full p7zip-rar rar tar unace unace-nonfree unar unrar unzip zip zpaq zstd pigz pbzip2 plzip nulib2
Formatos suportados:
- 7ZIP (7z, tar.7z)
- ARJ
- Binary II and ShrinkIt (bny, bqy, bse, bxy, sdk, sea, shk)
- BROTLI (br) -- descompressão apenas
- BZIP2 (bz, bz2, tar.bz2, tb2, tbz, tbz2)
- CABINET (cab, exe) -- descompressão apenas
- GZIP (gz, tar.gz, tgz)
- IMAGEM (iso, bin, mdf, nrg, cdi) -- descompressão apenas
- INCOMUNS (ace, arc, cpio, lha, lzh, lzx, pak, pit, sit, sitx) -- descompressão apenas
- LZIP (lz, lzip, tar.lz, tar.lzip, tlz)
- LZMA (lzma, tar.lzma)
- LZOP (lzo, lzop, tar.lzo, tar.lzop)
- RAR
- TAR
- Unix GNUzip (z)
- XZIP (tar.xz, xz, txz)
- ZIP
- ZPAQ
- Zstandard (tar.zst, tar.zstd, tzst, zst, zstd)
This file contains hidden or 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/zsh | |
################################### | |
# ZAP - Compress/Decompress Wrapper | |
# By Daniel Dias Rodrigues | |
################################### | |
function show_usage() { | |
cat << EOF | |
Usage: zap [option] [archive] [files/folders...] | |
Options: | |
c, -c Compress | |
x, -x, d, -d Extract / Decompress | |
Supported formats: | |
7z, tar.7z, arj, bny, bqy, bse, bxy, sdk, sea, shk, | |
br, bz, bz2, tar.bz2, tbz, tbz2, tb2, | |
cab, exe, | |
gz, tar.gz, tgz, | |
iso, bin, mdf, nrg, cdi, | |
ace, arc, cpio, lha, lzh, lzx, pak, pit, sit, sitx, | |
lz, tar.lz, tlz, | |
lzma, tar.lzma, | |
lzo, tar.lzo, tzo, | |
rar, tar, tar.xz, txz, | |
z, zst, tar.zst, tzst, | |
zip, zpaq | |
Notes: | |
- This is a simple wrapper: only verbose and recursion options enabled. | |
- No compression level or update options supported. | |
- Multithreading used where possible. | |
EOF | |
} | |
function compress() { | |
local archive="$1" | |
shift | |
local files=("$@") | |
local lc_archive="${archive:l}" | |
case $lc_archive in | |
*.tar.bz2|*.tbz2|*.tbz|*.tb2) | |
tar -I pbzip2 -cvf "$archive" "${files[@]}" ;; | |
*.tar.gz|*.tgz) | |
tar -I pigz -cvf "$archive" "${files[@]}" ;; | |
*.tar.lz|*.tlz) | |
tar -I plzip -cvf "$archive" "${files[@]}" ;; | |
*.tar.lzo|*.tzo) | |
tar --lzop -cvf "$archive" "${files[@]}" ;; | |
*.tar.zst|*.tzst) | |
tar -I "zstd -T0" -cvf "$archive" "${files[@]}" ;; | |
*.tar.lzma) | |
tar -I lzma -cvf "$archive" "${files[@]}" ;; | |
*.tar) | |
tar -cvf "$archive" "${files[@]}" ;; | |
*.tar.xz|*.txz) | |
tar -I "xz -vv -T0" -cvf "$archive" "${files[@]}" ;; | |
*.tar.7z) | |
tar cf - "${files[@]}" | 7z a -si "$archive" ;; | |
*.7z) | |
7z a -r "$archive" "${files[@]}" ;; | |
*.arj) | |
arj a -r "$archive" "${files[@]}" ;; | |
*.rar) | |
rar a -r0 "$archive" "${files[@]}" ;; | |
*.zip) | |
zip -r "$archive" "${files[@]}" ;; | |
*.zpaq) | |
zpaq a "$archive" "${files[@]}" ;; | |
# Single-file compressors: loop each file | |
*.bz2|*.bz) | |
for f in "${files[@]}"; do | |
pbzip2 -kv "$f" | |
done ;; | |
*.gz) | |
for f in "${files[@]}"; do | |
pigz -kv "$f" | |
done ;; | |
*.lzma) | |
for f in "${files[@]}"; do | |
lzma -kv "$f" | |
done ;; | |
*.lzip|*.lz) | |
for f in "${files[@]}"; do | |
plzip -vo "$f" | |
done ;; | |
*.lzop|*.lzo) | |
for f in "${files[@]}"; do | |
lzop -Pvo "$f" | |
done ;; | |
*.xz) | |
for f in "${files[@]}"; do | |
xz -kv -T0 "$f" | |
done ;; | |
*.z) | |
for f in "${files[@]}"; do | |
compress -vr "$f" | |
done ;; | |
*.zstd|*.zst) | |
for f in "${files[@]}"; do | |
zstd -vo "$f" | |
done ;; | |
*.shk|*.sdk) | |
for f in "${files[@]}"; do | |
nulib2 -aee "$f" | |
done ;; | |
*) | |
echo "ERROR: Unsupported compression format: '$archive'" | |
return 1 ;; | |
esac | |
} | |
function decompress() { | |
local archive="$1" | |
shift | |
local files=("$@") | |
if [[ ! -f "$archive" ]]; then | |
echo "ERROR: File does not exist: $archive" | |
return 1 | |
fi | |
local lc_archive="${archive:l}" | |
case $lc_archive in | |
*.tar) | |
tar -xvpf "$archive" ;; | |
*.tar.bz2|*.tbz2|*.tbz|*.tb2) | |
tar -I pbzip2 -xvpf "$archive" ;; | |
*.tar.gz|*.tgz) | |
tar -I pigz -xvpf "$archive" ;; | |
*.tar.lz|*.tlz) | |
tar -I plzip -xvpf "$archive" ;; | |
*.tar.xz|*.txz) | |
tar -I "xz -d -vv -T0" -xvpf "$archive" ;; | |
*.tar.lzma|*.tar.lzo|*.tzo|*.tar.zst|*.tzst) | |
tar -xvpaf "$archive" ;; | |
*.tar.7z) | |
7z x "$archive" && tar -xvpf "${archive%.*}" ;; | |
*.7z) | |
7z x "$archive" ;; | |
*.arj) | |
arj x -y "$archive" ;; | |
*.br) | |
brotli -d "$archive" ;; | |
*.bz2|*.bz) | |
bunzip2 "$archive" ;; | |
*.cab|*.exe) | |
cabextract "$archive" ;; | |
*.gz) | |
gunzip "$archive" ;; | |
*.lzma) | |
unlzma "$archive" ;; | |
*.lz) | |
plzip -kvd "$archive" ;; | |
*.lzo) | |
lzop -vPd "$archive" ;; | |
*.rar) | |
unrar x -ad "$archive" ;; | |
*.xz) | |
unxz -v -T0 "$archive" ;; | |
*.zip) | |
unzip "$archive" ;; | |
*.zpaq) | |
zpaq x "$archive" ;; | |
*.zst) | |
zstd -vd "$archive" ;; | |
*.z) | |
uncompress "$archive" ;; | |
*.bny|*.bqy|*.bse|*.bxy|*.sdk|*.sea|*.shk) | |
nulib2 -xee "$archive" "${files[@]}" ;; | |
*.iso|*.bin|*.mdf|*.nrg|*.cdi) | |
unar "$archive" ;; | |
*.sit|*.sitx|*.ace|*.lha|*.lzh|*.lzx|*.cpio|*.pit|*.arc|*.pak) | |
unar "$archive" ;; | |
*) | |
echo "ERROR: Unsupported extraction format: '$archive'" | |
return 1 ;; | |
esac | |
} | |
if [[ $# -lt 2 ]]; then | |
show_usage | |
exit 1 | |
fi | |
case "$1" in | |
-c|c) | |
compress "$2" "${@:3}" ;; | |
-d|d|-x|x) | |
decompress "$2" "${@:3}" ;; | |
*) | |
show_usage | |
exit 1 ;; | |
esac |
Comentários
Postar um comentário