LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1145|回复: 4

最大限度压缩你的文本

[复制链接]
发表于 2008-5-3 20:07:59 | 显示全部楼层 |阅读模式
压缩中文文本的一个技巧:
如果你的文本文件是UTF-8编码的话,先转为GB18030,然后在gzip -9压缩,则可以得到最小的压缩文件。
发表于 2008-5-4 18:52:02 | 显示全部楼层
不这样认为, 压缩后只会小一点, 毕竟最好的压缩算法接近熵估计.

而编码对熵的影响不大
回复 支持 反对

使用道具 举报

发表于 2008-5-4 21:31:26 | 显示全部楼层
Post by pointer;1845668
不这样认为, 压缩后只会小一点, 毕竟最好的压缩算法接近熵估计.

而编码对熵的影响不大


同意楼上,而且,如果是纯文本的话,不知道是什么东西还考虑这么小的压缩差别(一本《红楼梦》都写不到 1 M ,哈哈)
还有就是,UTF8 编码很好用呀,如果除了汉字和英文,还有其它的特殊字符,转成 gb18030 可会丢失信息的。

况且,最好的办法应该是换其它的压缩算法吧,比如 z7 之类,而不是考虑编码问题。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-5-5 11:01:40 | 显示全部楼层
可以简单比较一下文本的大小,u比g大了二分之一:

  1. $g2u < 电脑史话.txt > 电脑史话-u.txt
  2. $ll 电脑史话*.txt -lh
  3. 245K  电脑史话.txt
  4. 362K  电脑史话-u.txt
复制代码


另外,压缩前的文件大小当然会影响的压缩文件的大小。
回复 支持 反对

使用道具 举报

发表于 2008-5-5 12:16:57 | 显示全部楼层
文件大小尽管不一样, 但是信息量是一样的, 只能说用 utf-8 对纯中文进行编码会带来更多的无用的或重复的信息, 但是压缩的目的正是要去除这种信息. 理论上来说是这样, 不过具体到实现中的压缩算法上其结果应该还是有所不同的. 我作了一个实际的测试, 如下
  1. -rw-rw-r--  1 yun yun  81806 May  5 12:04 gb18030.txt
  2. -rw-rw-r--  1 yun yun 118540 May  5 12:03 utf8.txt
  3. -rw-------  1 yun yun  40057 May  5 12:07 gb18030.txt.7z
  4. -rw-------  1 yun yun  43416 May  5 12:07 utf8.txt.7z
  5. -rw-rw-r--  1 yun yun  39473 May  5 12:05 gb18030.txt.bz2
  6. -rw-rw-r--  1 yun yun  39644 May  5 12:05 utf8.txt.bz2
  7. -rw-rw-r--  1 yun yun  43352 May  5 12:04 gb18030.txt.gz
  8. -rw-rw-r--  1 yun yun  49430 May  5 12:05 utf8.txt.gz
复制代码

采用的压缩软件的版本分别为
  1. [yun@localhost shm]$ gzip --version
  2. gzip 1.3.5
  3. (2002-09-30)
  4. Copyright 2002 Free Software Foundation
  5. Copyright 1992-1993 Jean-loup Gailly
  6. This program comes with ABSOLUTELY NO WARRANTY.
  7. You may redistribute copies of this program
  8. under the terms of the GNU General Public License.
  9. For more information about these matters, see the file named COPYING.
  10. Compilation options:
  11. DIRENT UTIME STDC_HEADERS HAVE_UNISTD_H HAVE_MEMORY_H HAVE_STRING_H HAVE_LSTAT
  12. Written by Jean-loup Gailly.
  13. [yun@localhost shm]$ bzip2 --version
  14. bzip2, a block-sorting file compressor.  Version 1.0.2, 30-Dec-2001.
  15.    Copyright (C) 1996-2002 by Julian Seward.
  16.    This program is free software; you can redistribute it and/or modify
  17.    it under the terms set out in the LICENSE file, which is included
  18.    in the bzip2-1.0 source distribution.
  19.    This program is distributed in the hope that it will be useful,
  20.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22.    LICENSE file for more details.
  23. bzip2: I won't write compressed data to a terminal.
  24. bzip2: For help, type: `bzip2 --help'.
  25. [yun@localhost shm]$ 7za
  26. 7-Zip (A) 4.57  Copyright (c) 1999-2007 Igor Pavlov  2007-12-06
  27. p7zip Version 4.57 (locale=zh_CN.UTF-8,Utf16=on,HugeFiles=on,1 CPU)
复制代码

gzip 与 bzip2 都采用 -9 进行压缩, 7za 采用 -mx=9 (或许进一步指定一下字典大小会产生更理想的效果)
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表