Skip to content

Instantly share code, notes, and snippets.

@nerun
Last active July 31, 2025 13:48
Show Gist options
  • Save nerun/dacb269ac03d13085f7e1465674d4ff1 to your computer and use it in GitHub Desktop.
Save nerun/dacb269ac03d13085f7e1465674d4ff1 to your computer and use it in GitHub Desktop.
Universal Compressor (Archiver) and Decompressor (Extractor) for Linux shell.
#!/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
@nerun
Copy link
Author

nerun commented Jul 31, 2025

You can safely replace the ZSH shebang with the BASH shebang.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment