Using zip commands to compress and decompress files in Linux __linux

Source: Internet
Author: User
Tags syslog password protection
using zip commands to compress and decompress files in LinuxTransferred from http://www.itivy.com/linux/archive/2012/3/22/linux-zip.html

In Windows, we use the most of the decompression software is winrar, this software is very convenient for us to achieve decompression. However, if you are in a Linux system, we can also try to use the command to achieve the decompression of the file, this article mainly introduces the zip command in Linux compression and decompression files. The zip command is very powerful, so let's take a step-by-step introduction.

1, Zip command base

How to compress multiple files with zip .

Syntax: Zip {. zipfile-name} {File-names}

#zip var-log-files.zip/var/log/*
Adding:var/log/acpid (deflated 81%)
Adding:var/log/anaconda.log (deflated 79%)
Adding:var/log/anaconda.syslog (deflated 73%)
Adding:var/log/anaconda.xlog (deflated 82%)
adding:var/log/audit/(stored 0) adding:var/log/boot.log (stored 0%)
Adding:var/log/boot.log.1 (deflated 40%)
Adding:var/log/boot.log.2 (deflated 42%)
Adding:var/log/boot.log.3 (deflated 40%)
Adding:var/log/boot.log.4 (deflated 40%)

How to recursively compress a directory and a file in a directory.

#zip-R var-log-dir.zip/var/log/
updating:var/log/(stored 0%)
Adding:var/log/wtmp (deflated 78%)
Adding:var/log/scrollkeeper.log (deflated 94%)
Adding:var/log/rpmpkgs.3 (deflated 68%)
Adding:var/log/spooler (stored 0%)
Adding:var/log/cron.2 (deflated 90%)
Adding:var/log/spooler.1 (stored 0%)
ADDING:VAR/LOG/SPOOLER.4 (stored 0%)
adding:var/log/httpd/(stored 0%)
Adding:var/log/rpmpkgs.1 (deflated 68%)
Adding:var/log/anaconda.log (deflated 79%)
ADDING:VAR/LOG/SECURE.2 (deflated 93%)

How to extract the a*.zip compressed package.

# Unzip Var-log.zip
Archive:var-log.zip
Inflating:var/log/acpid

Inflating:var/log/anaconda.log
Inflating:var/log/anaconda.syslog
Inflating:var/log/anaconda.xlog
creating:var/log/audit/

Use parameter V to see the details of the decompression process:

# unzip-v Var-log.zip
Archive:var-log.zip
Length method Size Ratio Date time CRC-32 Name
------     ------    ----- ----- ---- ----     ------ ----
1916 Defl:n 369 81% 02-08-0814:27 e2ffdc0cvar/log/acpid
13546 defl:n 2900 79% 02-02-07 14:25 34cc03a1 var/log/anaconda.log
Skip..
7680 defl:n 411 12-30-08 10:55 fe876ee9 VAR/LOG/WTMP.1
40981 defl:n 7395 82% 02-08-08 14:28 6386a95e var/log/xorg.0.log
--------   -------    ---    ---    ----
41406991 2809229 93% Files

How to understand the pressure of a compressed package to see the file inside it.

#unzip-l Var-log.zip
Archive:var-log.zip
Length Date Time Name
--------   ----       ----   ----
1916 02-08-08 14:27 Var/log/acpid
13546 02-02-07 14:25 Var/log/anaconda.log
.. Skip..
40981 02-08-08 14:28 Var/log/xorg.0.log
40981 02-08-07 14:56 Var/log/xorg.0.log.old
--------                    -------
41406991 files

2, Zip command advanced usage

The ZIP command provides 10 compression levels:

O Grade 0 is the lowest level, only do archiving, not compression
o Grade 1 Low compression rate, but fast
O Level 6 is the default compression level
O Level 9 is the highest compression rate, but it takes a lot of time, in addition to large files, we generally recommend to use Level 9

In the following example, I use Level 0, Level 6, Level 9 to compress the same directory, to see what size they compress individually:

# Zip var-log-files-default.zip/var/log/*
# zip-0 var-log-files-0.zip/var/log/*
# zip-9 var-log-files-9.zip/var/log/*
# ls-ltr
-rw-r--r--1 root root 2817248 1 13:05
Var-log-files-default.zip
-rw-r--r--1 root root 41415301 1 13:05
Var-log-files-0.zip
-rw-r--r--1 root root 2582610 1 13:06
Var-log-files-9.zip

3, the zip file password protection

Encrypt a zip file using the P option of the zip command

#zip-P mysecurepwd var-log-protected.zip/var/log/*

It is a good choice to use the above options in your shell script to do background work. However, when you use an interactive command line, you generally do not want the password to be visible in the history. Therefore, this time will use the following E option to set the password.

# ZIP-E var-log-protected.zip/var/log/*
Enter Password:
Verify Password:
Updating:var/log/acpid (deflated 81%)
Updating:var/log/anaconda.log (deflated 79%)

When you want to unzip a password-protected compressed file, you will be asked to enter a password.

# Unzip Var-log-protected.zip
Archive:var-log-protected.zip
[Var-log-protected.zip] var/log/acpid Password:

4, check the integrity of the zip file

Sometimes you want to check the integrity of the zip file and don't want to unzip it. You can then use the T option as described below

# unzip-t Var-log.zip
Archive:var-log.zip
Testing:var/log/acpid OK
Testing:var/log/anaconda.log OK
Testing:var/log/anaconda.syslog OK
Skip ...
Testing:var/log/wtmp OK
TESTING:VAR/LOG/WTMP.1 OK
Testing:var/log/xorg.0.log OK
No errors detected in compressed data of var-log.zip.

For Linux under the ZIP command compressed file method is introduced, for Linux, there is also a tar command can also achieve the compression and decompression of files, I intend to introduce the next article.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.