Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of the Linux basic command gzip

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "the usage of the Linux basic command gzip". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Gzip

Gzip uses the Lempel-ziv algorithm to compress the file. When compressing, it retains the owner, permissions and modification time of each file. For symbolic links, gzip will ignore it.

If the compressed file name is too long for its file system, gzip truncates it. Gzip attempts to truncate only those parts of the file name that are more than 3 characters long. (part is separated by dots.) If the name consists only of widgets, the longest part is truncated. For example, if the file name is limited to 14 characters, "gzip.msdos.exe" is compressed to "gzi.msd.exe.gz". In systems where there is no limit on the length of file names, names are not truncated.

By default, gzip saves the original file name and timestamp in a compressed file. These are used when extracting files using the "- N" option. This is useful when the compressed file name is truncated or there is no timestamp retained after the file transfer. Compressed files can be restored to their original form using "gzip-d" or "gunzip" or "zcat". If the original name saved in the compressed file is not appropriate for its file system, a new name is constructed from the original file to make it legal.

Gunzip takes a list of files on its command line and replaces each file whose name ends with .gz,-z,-z, _ z, or .z, which starts with the correct magic number, and the uncompressed file of the file does not have the original extension. Gunzip also recognizes the special extensions .tgz and .taz as abbreviations for .tar.gz and .tar.z, respectively. When compressing, gzip uses the .tgz extension if necessary, rather than truncating files with the .tar extension.

Gunzip can currently extract files created by gzip,zip,compress-H or Pack. The detection of input format is automatic. When using the first two formats, gunzip checks 32-bit CRC. For bags, the gun zipper checks the uncompressed length. The standard compression format is not designed to allow consistency checking. However, gunzip can sometimes detect a bad .z file. If there is an error when unzipping the .z file, do not think the .Z file is correct just because the standard decompression does not complain. This usually means that standard decompression does not check its input and happily generates garbage output. The Shanghai Cooperation Organization Compression-H format (lzh compression method) does not include a CRC, but also allows some consistency checks.

Files created by zip can only be unzipped by GZIP if the individual members are compressed using the "deflation" method. This feature is only used to help convert tar.zip files to tar.gz format. To extract the zip file using a single member, you can use something such as "gunzip"

< foo.zip"或"gunzip -S .zip foo.zip"之类的命令。要提取包含多个成员的zip文件,请使用解压缩而不是gunzip。 Zcat和"gunzip -c"是一样的。(在某些系统上,可以将zcat安装为gzcat,以保留原始链接以进行压缩。)zcat解压缩命令行上的文件列表或其标准输入,并将未压缩的数据写入标准输出。无论是否有.gz后缀,zcat都会解压缩具有正确魔术号的文件。 Gzip使用用于zip和PKZIP的Lempel-Ziv算法。获得的压缩量取决于输入的大小和常用子字符串的分布。通常,像源代码或英语这样的文本会减少60-70%.压缩通常比LZW(用于压缩)、Huffman编码(用于Pack)或自适应Huffman编码(紧凑)要好得多。 压缩总是被执行,即使压缩文件比原始文件稍大。最坏的情况是gzip文件头的几个字节,加上每32K块5个字节,或者大文件的扩展率为0.015%。注意,使用过的磁盘块的实际数量几乎从未增加。gzip在压缩或解压缩时保留文件的模式、所有权和时间戳。 此命令的适用范围:RedHat、RHEL、Ubuntu、CentOS、SUSE、openSUSE、Fedora。 1、语法 gzip [ -acdfhlLnNrtvV19 ] [-S suffix] [ name ... ] 2、选项列表 选项 说明 -h | --help 帮助信息 -V | --version 显示命令版本信息 -a | --ascii 文本模式,只适用于某些系统 -c | --stdout | --to-stdout 将解压文件写到标准输出,源文件不变。如果有多个输入文件,则输出由一系列独立压缩的成员组成。为了获得更好的压缩,在压缩所有输入文件之前先将它们连接起来。 -d | --decompress | --uncompress 解压 -f | --force 强制执行 -l | --list 对于每一个压缩的文件,列出压缩文件大小、解压大小、压缩比列、压缩前的文件名字 -L | --license 列出gzip的许可证 -n | --no-name 压缩文件的时候,不保留原始文件名字和时间属性 -N | --name 压缩的时候,保留原始文件和时间属性 -q | --quite 跳过所有的警告信息 -r | --recursive 递归压缩子目录 -S |.suf | --suffix | .suf 指定压缩文件后缀。压缩时,使用后缀.suf代替.gz。可以提供任何非空后缀,但应避免使用.z和.gz以外的后缀,以避免文件传输到其他系统时出现混淆。 -t | --test 测试 -v | --verbose 显示执行过程 -num | --fast | --best 使用指定的数字num调整压缩速度,其中-1或--fast表示最快的压缩方法(较小的压缩)和-9或--best表示最慢的压缩方法(最佳压缩)。默认的压缩级别是-6(也就是说,以牺牲速度为代价倾向于高压缩)。 3、环境变量 环境变量GZIP可以保存gzip的一组默认选项。这些选项首先被解释,可以被显式命令行参数覆盖。例如 for sh: GZIP="-8v --name"; export GZIP for csh: setenv GZIP "-8v --name" for MSDOS: set GZIP=-8v --name 在VAX/VMS上,环境变量的名称为GZIP_OPT,以避免与用于调用程序的符号设置冲突。 4、诊断信息 退出状态通常为0;如果发生错误,退出状态为1。如果发生警告,退出状态为2。 Usage: gzip [-cdfhlLnNrtvV19] [-S suffix] [file ...] 在命令行上指定了无效选项 file: not in gzip format 指定给gunzip的文件尚未压缩。 file: Corrupt input. Use zcat to recover some data. 压缩文件已损坏。 file: compressed with xx bits, can only handle yy bits 用gzip重新压缩文件,压缩更好,内存更少。 file: already has .gz suffix -- no change 假定该文件已被压缩。重命名该文件,然后再试一次。 file already exists; do you wish to overwrite (y or n)? 如果希望替换输出文件,请响应"y";如果不希望替换输出文件,则响应"n"。 gunzip: corrupt input 检测到SIGSEGV违规,这通常意味着输入文件已损坏。 xx.x% Percentage of the input saved by compression. 只与-v和-l有关。 -- not a regular file or directory: ignored 当输入文件不是常规文件或目录(例如符号链接、套接字、FIFO、设备文件)时,它将保持不变。 -- has xx other links: unchanged 输入文件有链接;它保持不变。有关更多信息,请参见ln(1)。使用-f标志强制压缩多链接文件。 5、一些先进用法 可以连接多个压缩文件。在这种情况下,gunzip将立即提取所有成员。例如: gzip -c file1 >

Foo.gz

Gzip-c file2 > > foo.gz

Gunzip-c foo

The above code is equivalent to

Cat file1 file2

If one member of the .gz file is corrupted, other members can still recover (if the corrupted member is removed). However, you can get better compression by compressing all members at once.

Cat file1 file2 | gzip > foo.gz

The code above is better than the one below.

Gzip-c file1 file2 > foo.gz

If you want to recompress the connected files for better compression, you can use the

Gzip-cd old.gz | gzip > new.gz

If a compressed file consists of multiple members, the uncompressed size and CRC reported by the-- list option apply only to the last member. If you need the uncompressed size of all members, you can use the

Gzip-cd file.gz | wc-c

If you want to create a single archive file with multiple members so that members can be extracted independently later, use an archiving program such as tar or zip. GNUtar supports the-z option to call gzip transparently. Gzip is a supplement to, not a substitute for, tar.

6. Examples

1) compress the file

[root@localhost weijie] # gzip 1.c / / Compression

[root@localhost weijie] # ls / / after compression, the source file is deleted

11.c 1.c.gz 2.c 3.c 4.c 5.c 6.c foo

2) decompression

[root@localhost weijie] # gzip-ld 1.c.gz / / decompress and show the detailed process

Compressed uncompressed ratio uncompressed_name

70 53 9.4% 1.c

3) compress two files into one file

[root@localhost weijie] # cat 1.c 2.c / / output the contents of two files

Hello world

I am david.

I love linux

Love code.

one hundred and twenty three

twenty-three

two hundred and twelve

[root@localhost weijie] # gzip-c 1.c > foo.gz / / compress 1.c to foo

[root@localhost weijie] # gzip-c 2.c > > foo.gz / / compress 2.c to foo

[root@localhost weijie] # gzip-d foo.gz / / extract foo

[root@localhost weijie] # cat foo / / display the contents of foo

Hello world

I am david.

I love linux

Love code.

one hundred and twenty three

twenty-three

two hundred and twelve

This is the end of the introduction to "the usage of the Linux basic command gzip". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report