From: http://hi.baidu.com/jorling/blog/item/13db7bec1c38a1d32e2e21d3.html
File compression and Archiving
Sometimes, we need to store a group of files into one for backup or transmission to another directory or even another computer. Sometimes, we need to compress files into one, so they only use a small amount of disk space and can be downloaded over the Internet faster.
UnderstandingArchive file)AndCompressed file)The differences between them are very important for users. An archive object is a collection of objects and directories stored in a file. The archive file is not compressed-the disk space it uses is the sum of all the files and directories. A compressed file is also a collection of files and directories, and the collection is also stored in a file,HoweverIt stores less disk space than all its files and directories. If you have insufficient disk space on your computer, you can compress files that are not commonly used or that are no longer in use but that you want to keep. You can even create an archive file and compress it to save disk space.
|
Note |
|
Archive files are not compressed files, but compressed files can be archive files. |
Use a package
Red Hat Linux includes a graphical compression tool named "file package ". It can compress, decompress, and archive files and directories. The package supports common UNIX and Linux File compression and archiving formats, and its interface is simple and rich in documents. It is also integrated into the desktop environment and graphical file manager, making it easier to process archive files.
To start the file package, click "Main Menu" => "attachment" => "file package 」. You can also enterFile-RollerTo start the package. Figure 14-1 shows the file package in progress.
|
Tips |
|
If you are using a File Manager (such as Nautilus), you can double-click the file you want to unarchive or decompress to start the package. The browsing window of the file package will appear. The folder contains the files you want to decompress Or unarchive, so that you can extract or browse them. |
Figure 14-1. Running file package
Decompress or decompress the archive using the package.
To cancel archiving or decompress the file, click open on the toolbar. A file menu will pop up, allowing you to select the archive you want to operate. For example, if you haveFoo.tar.gzFile to highlight the file, and then click "OK 」. The file appears as a folder in the main browser window of the package. You can double-click the folder icon to read the content. The file package keeps all the original directories and sub-directory structures, so that you can find a specific file in the archive. You can click the extract button, select the directory of the files you want to save, and click OK to extract a file or the entire archive.
Create an archive file using a package
If you need to free up some hard disk space, or send multiple files or all files in a directory to another user, the package allows you to create files and archive directories. To create a new archive, click "new" on the toolbar. A file browser will pop up, allowing you to specify the archive name and compression technology. For example, you can select the tar (tar.gz) format compressed by gzip from the drop-down menu and enter the name of the archive file you want to create. Click "OK" to add files and directories to the new archive. To add a file to your new archive, click "add". A preview window is displayed (Figure 14-2). You can find the file or directory to be archived. Click OK and close to close the archive.
Figure 14-2. Create an archive with a package
|
Tips |
|
The tasks that the file package can execute are not limited to the ones mentioned here. Please read the document package instructions (click "help" => "Manual") to obtain detailed information. |
Compress the file at the shell prompt
Compressed files use less disk space and are faster than uncompressed large files. In Red Hat Linux, you can use the following file compression tools:Gzip,Bzip2, AndZip.
We recommend that you useBzip2Compression tool because it provides maximum compression and can be found on most Unix-like operating systems.GzipCompression tools can also be found on Unix-like operating systems. If you need to transfer files between Linux and other operating systems such as MS windows, you should useZipBecause the command is most compatible with the compression tool on Windows.
Compression Tool |
File Extension |
Decompression tool |
Gzip |
. GZ |
Gunzip |
Bzip2 |
. Bz2 |
Bunzip2 |
Zip |
. Zip |
Unzip |
Table 14-1. compression tools
According to conventions, useGzipThe extension of the file to be compressed is. GZ; UseBzip2The extension of the file to be compressed is. Bz2; UseZipThe extension of the compressed file is. Zip.
UseGzipCompressed files can be usedGunzipTo decompress the package.Bzip2Compressed files can be usedBunzip2To decompress the package.ZipCompressed files can be usedUnzipTo decompress the package.
Bzip2 and bunzip2
To useBzip2To compress the file, enter the following command at the shell prompt:
Files are compressed and savedFilename.bz2.
To expand the compressed file, type the following command:
Filename.bz2The file will be deleted, followedFilename.
You can useBzip2Command to process multiple files and directories at the same time by listing them one by one and using the space interval:
bzip2 filename.bz2 file1 file2 file3 /usr/work/school |
The above commandFile1,File2,File3, And/Usr/work/SchoolThe contents of the directory (assuming this directory exists) are compressed and then putFilename.bz2File.
|
Tips |
|
To obtain detailed information about the two commands, enterMan Bzip2AndMan bunzip2To readBzip2AndBunzip2Command manual page. |
Gzip and gunzip
To useGzipTo compress the file, enter the following command at the shell prompt:
Files are compressed and savedFilename.gz.
To expand the compressed file, type the following command:
Filename.gzWill be deleted, followedFilename.
You can useGzipCommand to process multiple files and directories at the same time by listing them one by one and using the space interval:
gzip -r filename.gz file1 file2 file3 /usr/work/school |
The above commandFile1,File2,File3, And/Usr/work/SchoolThe contents of the directory (assuming this directory exists) are compressed and then putFilename.gzFile.
|
Tips |
|
To obtain detailed information about the two commands, enterMan GzipAndMan gunzipTo readGzipAndGunzipCommand manual page. |
Zip and unzip
To useZipIn the shell prompt, enter the following command:
zip -r filename.zip filesdir |
In this example,Filename.zipIndicates the file you created,FilesdirIndicates the directory where you want to place the new zip file.-ROption to specifyRecursive (recursively)Including allFilesdirDirectory.
To extractZipFile Content, type the following command:
You can useZipCommand to process multiple files and directories at the same time by listing them one by one and using the space interval:
zip -r filename.zip file1 file2 file3 /usr/work/school |
The above commandFile1,File2,File3, And/Usr/work/SchoolThe contents of the directory (assuming this directory exists) are compressed and then putFilename.zipFile.
|
Tips |
|
To obtain detailed information about the two commands, enterMan zipAndMan unzipTo readZipAndUnzipCommand manual page. |
File archiving at the shell prompt
TarA file is a collection of several files and/or directories in one file. This is an excellent path for creating backups and archives.
TarThe following options are used:
-C-Create a new archive.
-F-When-CTheTarFile Name specified by this option;-XIf this option is used together, the archive specified by this option is removed.
-T-Display included inTarFile List.
-V-Displays the file archiving progress.
-X-Extract files from the archive.
-Z-UseGzipTo CompressTarFile.
-J-UseBzip2To CompressTarFile.
To create a tar file, type:
tar -cvf filename.tar directory/file |
In the above example,Filename.tarIndicates the file you created,Directory/FileIndicates the files and directories you want to put into the archive file.
You can useTarCommand to process multiple files and directories at the same time by listing them one by one and using the space interval:
tar -cvf filename.tar /home/mine/work /home/mine/school |
The above command/Home/MineDirectoryWorkAndSchoolAll the files in the subdirectory are put in the current directory namedFilename.tar.
To list the contents of the TAR file, type:
To extract the content of the TAR file, type:
This command will not be deletedTarFile, but it will copy the Unarchived content to the current working directory and keep any directory structure used by the archive file. For example, if the tar file contains a file namedBar.txtAnd this file is included inFoo/Directory, archive files will be extracted and created in your current working directoryFoo/Directory, which containsBar.txtFile.
Remember,TarBy default, files are not compressed. To create a file that uses tar and bzip to archive and compress, use-JOption:
tar -cjvf filename.tbz file |
According to conventions, useBzip2CompressedTarFile has. TBZExtension. HoweverTar.bz2To archive their files.
The preceding command creates an archive file and then compresses itFilename. TBZFile. If you useBunzip2Command isFilename. TBZDecompress the file,Filename. TBZThe file will be deleted, followedFilename.tarFile.
You can also use a command to expand and unarchiveBzip TarFile:
To create a file archived and compressed with tar and gzip, use-ZOption:
tar -czvf filename.tgz file |
According to conventions, useGzipTo CompressTarFile has. TgzExtension.
This command creates an archive fileFilename.tarAnd then compress itFilename. tgzFile (FileFilename.tarNot retained ). If you useGunzipCommandFilename. tgzDecompress the file,Filename. tgzThe file will be deleted and replacedFilename.tar.
You can use a single command to expandGzip TarFile:
tar -xzvf filename.tgz