Skip to content

Instantly share code, notes, and snippets.

@xdevs23
Last active November 24, 2016 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xdevs23/d334ee2272e092982dfeb03600c4a008 to your computer and use it in GitHub Desktop.
Save xdevs23/d334ee2272e092982dfeb03600c4a008 to your computer and use it in GitHub Desktop.
Easy-to-use folder compression and decompression
# XZ (LZMA2 implementation) is a very strong and one of the most used compression utils
# Compress folder
# Specify 'noabrt' to prevent cancelling the process when an error occurs
# Specify 'nodel' to keep input files
# Specify 'force' to force compression and avoid some weird errors
function compressfolder() {
addnbrt=""
[[ " $@ " == *" nodel "* ]] && addnbrt="--keep"
[[ " $@ " == *" force "* ]] && addnbrt="$addnbrt --force"
while read ffcc; do
echo -e "\e[1mCompressing $ffcc\e[0m"
xz -z9ev -T 4 --lzma2=nice=273 -Q $addnbrt "$ffcc"
if [ $? -ne 0 ]; then
echo "Compression failed."
if [[ " $@ " != *" noabrt "* ]]; then
return 1
fi
fi
done < <(find . -type f)
unset addnbrt
}
# Decompress folder
# Specify 'noabrt' to prevent cancelling the process when an error occurs
# Specify 'nodel' to keep input files
# Specify 'force' to force decompression and avoid some weird errors
function decompressfolder() {
addnbrt=""
[[ " $@ " == *" nodel "* ]] && addnbrt="--keep"
[[ " $@ " == *" force "* ]] && addnbrt="$addnbrt --force"
while read ffcc; do
echo -e "\e[1mDecompressing $ffcc\e[0m"
xz -dv -T 4 -Q $addnbrt "$ffcc"
if [ $? -ne 0 ]; then
echo "Decompression failed."
if [[ " $@ " != *" noabrt "* ]]; then
return 1
fi
fi
done < <(find . -name "*.xz" -type f)
unset addnbrt
}
function compressfile() {
addnbrt=""
[[ " $@ " == *" nodel "* ]] && addnbrt="--keep"
[[ " $@ " == *" force "* ]] && addnbrt="$addnbrt --force"
xz -z9ev -T 4 --lzma2=nice=273 -Q $addnbrt "$1"
unset addnbrt
}
function decompressfile() {
addnbrt=""
[[ " $@ " == *" nodel "* ]] && addnbrt="--keep"
[[ " $@ " == *" force "* ]] && addnbrt="$addnbrt --force"
xz -dv -T 4 -Q $addnbrt "$1"
unset addnbrt
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment