Packing and compression

Archiving, also known as packaging, referring to the collection of a file or directory, and this collection is stored in a file. Archive file is not compressed, so it takes up space which is the sum of all files and directories.

And similar archive, a collection of compressed files is a file and directory, and this collection is also stored in a file, but they differ in that the compressed file with a different storage methods, so the amount of disk space the sum of all the file size is smaller than the collection.

Is the use of compression algorithms for processing the file, the file has reached the maximum retention of information, and make the file size smaller purposes. The basic principle is, by looking for repeated bytes within the file, create a same byte dictionary file, and is represented by a code. For example, in a compressed file, has appeared in more than a " C language Chinese network," then, in the compressed file, the word will be denoted by a code and write the dictionary file, so that you can achieve reduced file size purpose.

Since the binary information is represented in the form of a computer process, therefore, it is to compress the binary information in the software the same special character string tag, as long as reasonable mathematical calculation, the file size can be greatly compressed. The one or more files compressed with compression software, forming a compressed file, storage space can be saved, there is conveniently transmitted on the network.

If you can understand the basic principles of file compression, so it is easy to think of the file is compressed, it may damage the contents of the file, therefore, it can be divided into lossy compression and lossless compression. Lossless compression is well understood, referring to the compressed data must be accurate; lossy compression means that even if the loss of individual data files will not cause much impact. Lossy compression is widely used in animation, sound and image files, a typical representative of DVD file format mpeg, mp3 music file format and image file format jpg.

A compressed file compression tool, the resulting compressed file is called, the volume of the files typically only half of the original document even smaller. It should be noted that the data packet compression can not be used directly before use need to use compression tool to restore data files, this process is also known as decompression.

Linux  case, there are two common archive command, tar and dd are (relatively speaking, more extensive use of tar);

There are many commonly used commands compression, such as gzip, zip, bzip2 and so on.

1, takes

The most common archive (packaged) command is tar, the command saves many files together into a single tape or disk archive. Not only that, the command can also restore a file from the archive, which is anti-packaged process, called solution package.

Packaging operations

When the tar command for packaging operations, the basic format of this command is: tar [options] source file or directory

Options:

  • -c: multiple files or directories to package.
  • -A: Append tar files to an archive file.
  • -f package name: Specify the file name of the package ( suffix, usually .tar ). Extension package is used to identify the format of the administrator, so be sure to specify the correct extension;
  • -v: display package file process;

When you specify options using the tar command can not options previously entered "-." For example, as the use of "cvf" option and the "-cvf" play a role .

Example One: packaged files and directories

tar -cvf Anaconda-ks.cfg. tar     anaconda- ks.cfg 
# to anacondehks.cfg packaged as anacondehks.cfg.tar file

Options "-cvf" generally idioms, you need to specify the file name after the package Remember package, and use the ".tar" as the extension .

Example II: compression packing

Compress command can not be directly compress the directory, you must first directory packaged with tar command before you can be compressed packed files using gzip or bzip2 command command .

gzip the Test. tar 
#gzip command will test.tar compressed into the Test. tar .gz

Unpacking operation

When the tar command packet is used to make tar solution packaging operation, the basic format of the command is as follows: tar [Option] archive

When packaged solution for when you want to use the options:

  • -x: tar package to do the de-packaging operations.
  • -f: Specifies the package name you want to extract the tar package.
  • -t: Only see which files or directories have tar package, tar package do not de-packaging operations.
  • -C directory: Packaging Solutions designated location.
  • -v: Display specific process solutions packaged.

Packaging and packaging solution compared to just pack option "-cvf" replace "-xvf" .

the tar -xvf Anaconda-the ks.cfg. the tar 
# solution package to the current directory at 

the tar -xvf Test. the tar -C / tmp 
# test.tar solution package to package to / tmp / directory 

the tar -tvf Test. the tar 
# will Details test.tar package file for display in the long format

Compression packing (packing decompresses) Operation

When the tar command while doing packing compression operation, the basic format is as follows: tar [Option] source file or directory archive

Commonly used herein have the following two options, namely:

  • -z: compress and decompress ".tar.gz" format;
  • -j: compression and decompression ".tar.bz2" format.
 tar -zcvf tmp. tar .gz / tmp / 
# the / temp / directory as direct compression package " .tar.gz " format, by " the -z " to identify the format, " -cvf " and packaging options consistent 

tar -zxvf tmp . tar .gz 
# decompress the solution package " .tar.gz " format

"-C" specifies the decompression position, "- t" archive for viewing content, the same applies here.

 

tar -jcvf tmp. tar .bz2 / tmp / 
# package is compression " .tar.bz2 " format, note the name of the archive file 

tar -jxvf tmp. tar .bz2 
# decompress the package solution " .tar.bz2 " format

The compressed files directly into the ".tar.gz" and ".tar.bz2" format, Linux is the most commonly used compression method, which is the compression and decompression method we must master.

2、zip

".Zip" file format is Windows and  Linux  systems are common compressed file types, belonging to several mainstream compression formats (zip, rar, etc.) one, is a fairly simple compressed separately for each file storage format.

The basic format is as follows: ZIP [options] source file compression package name or source directory listing

Options:

  • -r: recursive compression directory, and all files and subdirectories in the development of the entire directory compression.
  • -m: After the file compression, delete the original file, move the file to the equivalent of a compressed file.
  • -v: Displays detailed information of the compression process.
  • -q: does not display during the execution of the command in compressed time.
  • - compression level: compression level is a number from 1 to 9, -1 compress faster -9 compress better representatives.
  • -u: update compressed files that add new files to the archive.
ZIP Test. ZIP  the install .log the install .log.syslog 
# while compressing the plurality of files in the archive to test.zip

 

Use zip compression directory command, use the "-r" option:

ZIP -r dir1. ZIP dir1 
# compression directory

2、unzip

unzip command to view and decompress zip file. The basic format of the command is as follows:  the unzip [Options] archive name

Options:

  • -d directory name will extract the compressed files to the specified directory.
  • Do not overwrite existing files when extracting -n.
  • When unzip -o overwrite an existing file, and without user confirmation.
  • -v view details of compressed files, including compressed file contains the file size, file name and compression ratio, but does not do decompression operation.
  • -t test compressed file for damage, but not unpacked.
  • -x file list extracting file, but does not contain the specified file in the file list.
the unzip dir1. ZIP 
# either file archive, archive or catalog, can be decompressed directly

 

Use the -d option to specify a manual decompression position;

the unzip -d / tmp / ANA. ZIP 
# archive into the specified location

3、gzip

gzip is  Linux  systems are often used to compress and decompress the file command, this command new file compression was obtained, and extension is usually labeled as ".gz".

gzip command can only be used to compress the file , directory can not be compressed, even if the specified directory, and can only compress all the files in the directory.

The basic format of gzip command is as follows: gzip [options] source file

Source file command, when the compression operation, means that the average document; when the decompression operation, referring to the compressed file.

Options:

  • The compressed data output to the standard output, and keep the source file: -c.
  • -d: the compressed file to decompress.
  • -r: Recursive compress all the files in the specified directory and subdirectories.
  • -v: For each compression and decompression of files, display the file name and compression ratio.
  • -l: For each compressed file, it displays the following fields:
    • The compressed file size;
    • Uncompressed file size;
    • Compression ratio;
    • Name of the file uncompressed.
  • - Digital: used to specify the compression level, -1 lowest level of compression, the compression ratio of the worst; -9 highest compression ratio. The default compression ratio -6.

Example a: basic compression

gzip compression command is very simple, even after compression package name does not need to specify the compression, simply specify the source file name.

gzip  install .log 
# instal.log compressed file 
# compressed file generation, but the source file is gone

 

Example Two: reserved Source file compression

gzip -c Anaconda-ks.cfg> anaconda- ks.cfg.gz 
# use the -c option, but not to compress the output data to the screen, but is redirected to a compressed file, so you can shrink a file without deleting the source file

 

Example Three: Compression directory

gzip -r test / 
# but test directory still exists, and does not become compressed files

gzip command does not pack the directory , but to all the sub-directory under the file compression, respectively

In Linux, packing and compression are treated separately. The command gzip compression only, not package, thus giving rise to no packing catalog, but only the files in the directory of the compression .

4、gunzip

gunzip is a widely-used decompression command, which is used to decompress compressed gzip file. (extension .gz). The basic format for the gunzip command: gunzip [options] file

For decompression is gzip compressed files, you can also use gzip own, namely gzip -d archive.

Options:

  • -r recursive processing, unzip all the files in the specified directory and subdirectories.
  • -c outputs the decompressed file to the standard output device.
  • -f Force unzip the file, the file is ignored and so already exists.
  • -l List the contents of compressed files.
  • -v show command execution process.
  • Whether -t test compressed files properly, but it does not do decompression operation.

Example One: Unzip the file

gunzip install.log.gz

"Gunzip -r" still will only decompress files in the directory, but not packaged solution. To decompress ".gz" format, you can also use "gzip -d" command, for example:

gzip -d anaconda-ks.cfg.gz

 

Example Two: Unzip the contents of the directory, you need to use the "-r" option, such as:

gunzip -r test/

 

If we compress is a plain text file, you can directly use the zcat command to view the contents of this text file in the case of not decompress. E.g:

zcat anaconda-ks.cfg.gz

5、bzip2

Similar to the command bzip2 gzip command only for file compression (or decompression), the directory can only be compressed (or uncompressed) files in the directory and all subdirectories. When the task is completed to perform compression, to generate a ".bz2" suffix to the archive.

".bz2" format is  Linux  another compression format, in theory, algorithm format more advanced, better compression ratio; and ".gz" Time Format relatively faster "bz2.".

The basic format of bzip2 command is as follows: bzip2 [options] source file

Options:

  • -d performs decompression, when the source file should be labeled with the option suffix .bz2 compressed file.
  • -k bzip2 compression or decompression after the task is completed, it will delete the original file, to preserve the original file, you can use this option.
  • When -f bzip2 compression or decompression, if the output file with the same name as an existing file, the default will not overwrite an existing file, if using this option, it will force the overwrite an existing file.
  • -t test compressed package file integrity.
  • When -v compressed or uncompressed files, it displays detailed information.
  • - The role of digital parameters and the like gzip command to specify the compression level, -1 lowest level of compression, the compression ratio of the worst; -9 highest compression ratio

gzip package directory just will not, but if you use the "-r" option, you can compress each file in the directory, respectively; and bzip2 command does not support the compression directory , there is no "-r" option.

6、bunzip2

To unzip ".bz2" compressed file formats, in addition to using the "bzip2 -d archive name" command, you can also use bunzip2 command. The basic format bunzip2 command is: bunzip2 [options] source file

And use gunzip command bunzip2 command about the same, bunzip2 command can only be used to extract the files, even if the extracted directory, the directory also unzip all files and subdirectories contained.

Options:

  • -K after decompression, the default will delete the original compressed files. To retain the compressed file, use this parameter.
  • When -f decompress, if the output file the same name as an existing file, the default will not overwrite existing files. To override, use this option.
  • -v show command execution process.
  • -L Lists compressed file contents.

And ".gz" format, like, ". Bz2" compression format of plain text files can also be compressed directly view not understand the command using bzcat. E.g:

bzcat install.log.syslog.bz2

 

Guess you like

Origin www.cnblogs.com/pacino12134/p/11482409.html