bash compression commands gzip bzip zip - ghdrako/doc_snipets GitHub Wiki

Verify compresed archive

Files in gzip format contain the length of the compressed data and the length of uncompressed data. However, this is an ancient format and the length fields only have 32 bits, so nowadays they're interpreted as being the length modulo 2^32 (i.e. 4 GiB). Before decompression, gzip checks that the checksum of the compressed data is correct. After decompression, gzip checks that the checksum of the decompressed data is correct, and that the size of the decompressed data is correct modulo 2^32.

As a consequence, gzip is guaranteed to detect a truncated input if the size of the compressed data (or the size of the decompressed data) is less than 4 GiB.

gzip -t arch.gz
bzip -t arch.bz

if ! gzip -t "$plik" > /dev/null 2>&1; then
  echo "Błąd: Plik '$plik' nie jest poprawnym plikiem .gz."
  exit 1
fi

It returned not only the exit code but also the unexpected end of file error message to stdout. Note `command -t' is time-consuming

If you just want to test whether or not the file is compressed use gzip --list (redirect errors if you want) and check $?

    apack log.log.apack.gz log.log
    gzip -k log.log
    zstd log.log
    brotli -3 log.log

bzip

Set of utilities for bz files includes bzcat, bzcmp, bzdiff, bzegrep, bzfgrep, bzgrep, bzless, and bzmore.

zip

zip file.zip file1 file2 file3 
zip sample2.zip *.txt 
#Alternatively, you can use the Java jar command:
jar cvf sample3.zip *.txt 

# The unzip command will unzip the contents of a zip file, as shown here:
unzip sample1.zip 
# Alternatively, you can use the Java jar command to unzip a zip file:
jar xvf sample3.zip The

The zip command has useful options (such as –x for excluding files). There are various commands for handling zip files, including zdiff, zcmp, zmore, zless, zcat, zipgrep, zipsplit, zipinfo, zgrep, zfgrep, and zegrep.

gzip

  • compress
gzip -k file # compres and  keep the input (original) file
gzip -c filename > filename.gz # as above
gzip -r directory # recursively traverse through the whole directory structure and compress all the files in the directory and it’s subdirectories
  • decompress
gzip -d filename.gz
gunzip filename.gz
gzip -dk filename.gz # Keep the compressed file
gzip -d file1.gz file2.gz file3.gz
gzip -dr directory