LinuxSir.Org  
| 网站首页 | 注册账号 | 论坛帮助 |

欢迎来到LinuxSir.Org!
您还未登录,请登录后查看论坛,或者点击论坛上方的注册链接注册新账号。


发表新主题 回复
精华主题  
主题工具
旧 04-04-02, 20:03 第 1 帖
home_king
 
home_king 的头像
 
 
临时退役版主  
  注册日期: Mar 2003
  帖子: 2,759
  精华: 15
 

标题: 有什么好办法在解压缩过程中同时获取压缩包的内容目录结构的顶层目录呢?


要求:
1.不能保存解压缩输出为临时文件再对该文件进行分析,要求获取是与解压缩同时进行的
2.要能在屏幕上显示解压缩的输出







__________________
IBOX, a LiveCD distribution based on Gentoo, is fit for newbie.

IBOX brings to you:
- knoppix-style harddisk installation tool, by which you can install Gentoo in 20 minutes.
- hardware auto-configuration. You can run into X desktop directly.
- all-round software set, including OpenOffice, azureus.
- LiveCD-create-tools. Step-by-step, custom a LiveCD yourself with ease.

Any questions and feedbacks is welcome to home_king at 163 dot com

此帖于 04-04-03 22:50 被 KornLee 编辑.
  home_king 当前离线   回复时引用此帖
旧 04-04-02, 20:30 第 2 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

汗~~ 还是举个例子吧~,不同的解压缩命令有不同的使用方法!
  KornLee 当前离线   回复时引用此帖
旧 04-04-02, 20:40 第 3 帖
home_king
 
home_king 的头像
 
 
临时退役版主  
  注册日期: Mar 2003
  帖子: 2,759
  精华: 15
 

限定为bz2。
  home_king 当前离线   回复时引用此帖
旧 04-04-03, 00:53 第 4 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

引用:
/home/javalee:bzip2 --help
bzip2, a block-sorting file compressor. Version 1.0.2, 30-Dec-2001.

usage: bzip2 [flags and input files in any order]

-h --help print this message
-d --decompress force decompression
-z --compress force compression
-k --keep keep (don't delete) input files
-f --force overwrite existing output files
-t --test test compressed file integrity
-c --stdout output to standard out
-q --quiet suppress noncritical error messages
-v --verbose be verbose (a 2nd -v gives more)
-L --license display software version & license
-V --version display software version & license
-s --small use less memory (at most 2500k)
-1 .. -9 set block size to 100k .. 900k
--fast alias for -1
--best alias for -9

If invoked as `bzip2', default action is to compress.
as `bunzip2', default action is to decompress.
as `bzcat', default action is to decompress to stdout.

If no file names are given, bzip2 compresses or decompresses
from standard input to standard output. You can combine
short flags, so `-v -4' means the same as -v4 or -4v, &c.
bzip2 -s -L -v file#compress file
bzip2 -d -v file #decompress file
试了半天,我自己都不知道在干什么了~~~
  KornLee 当前离线   回复时引用此帖
旧 04-04-03, 08:56 第 5 帖
home_king
 
home_king 的头像
 
 
临时退役版主  
  注册日期: Mar 2003
  帖子: 2,759
  精华: 15
 

呵呵,劳烦javalee老兄了,不好意思。
我想到办法了。
代码:
[root@home tmp]# cat gawkscript BEGIN { j=2; } { if(pool[1]!=$1) { pool[j++]=$1; pool[1]=$1; } print $0; } END { print "\nArchive directory structure:"; for(i=2;i<=j;i++) print pool[i]; } [root@home tmp]# tar jcf mplayer.tar.bz2 MPlayer-1.0pre3/ mplayerplug-in/ [root@home tmp]# bunzip2 -dc mplayer.tar.bz2 |tar tf - | gawk -F / -f gawkscript ... Archive directory structure: MPlayer-1.0pre3 mplayerplug-in
1. 在gawk脚本定义一个数组,利用C语言的"监测哨"技术(第一个数组元素保存上次有效值用以对比),滤出冗余的第一域数据;
2. 用print $0输出的同时进行第一域处理。这样就实现了第二个要求。
3. 注意,这里我使用了tar tf - ,这是tar的列表选项,其实解压缩选项tar xf - 是一样的。

gawk十分强大,它和(GNU)sed是bash的左右手。善用gawk&(GNU)sed。

此帖于 04-04-05 14:02 被 home_king 编辑.
  home_king 当前离线   回复时引用此帖
旧 04-04-03, 10:19 第 6 帖
dearvoid
 
 
 
注册会员  
  注册日期: Feb 2004
  我的住址: Beijing
  帖子: 282
  精华: 0
 

tar 的 -v 选项可以显示目录结构, 不知是否是你所需







__________________
.
欢迎到 Linux 伊甸园 shell 论坛做客 - www.linuxeden.com
  dearvoid 当前离线   回复时引用此帖
旧 04-04-03, 11:32 第 7 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

引用:
最初由 home_king 发表
呵呵,劳烦javalee老兄了,不好意思。
我想到办法了。
代码:
[root@home tmp]# cat gawkscript BEGIN { j=2; } { if(pool[1]!=$1) { pool[j++]=$1; pool[1]=$1; } print $0; } END { print "\nArchive directory structure:"; for(i=2;i<=j;i++) print pool[i]; } [root@home tmp]# tar jcf mplayer.tar.bz2 MPlayer-1.0pre3/ mplayerplug-in/ [root@home tmp]# bunzip2 -dc mplayer.tar.bz2 |tar tf - | gawk -F / -f gawkscript ... Archive directory structure: MPlayer-1.0pre3 mplayerplug-in
1. 在gawk脚本定义一个数组,利用C语言的"监测哨"技术(第一个数组元素保存上次有效值用以对比),滤出冗余的第一域数据;
2. 用print $0输出的同时进行第一域处理。这样就实现了第二个要求。
3. 注意,这里我使用了tar tf - ,这是tar的列表选项,其实解压缩选项tar xf - 是一样的。

gawk十分强大,它和(GNU)sed是bash的左右手。善用gawk&(GNU)sed。
精彩~~,

此帖于 04-04-05 14:04 被 home_king 编辑.
  KornLee 当前离线   回复时引用此帖
旧 04-04-03, 13:23 第 8 帖
home_king
 
home_king 的头像
 
 
临时退役版主  
  注册日期: Mar 2003
  帖子: 2,759
  精华: 15
 

之所以有冲动感欲编写这类shell代码,并不是无中生有的。
原因有二
很多源码包的内部目录结构与其名字是不相符的。如filename.tar.bz2被解压缩出来后,生成的目录名不一定就是"filename"或是"filename"的模式,甚至不止一个目录。这样如果我们在一个有众多文件的目录下解开这类源码包的时候,就会不知所措。此其一
为了提高"时空效率",不应该把冗长的输出保存下来,解决空间问题;不应该解压缩过后才进行分析,解决时间问题。或许有人说,一个源码包的时空消耗是微不足道的。没错,一个源码包可能如此,但如果上百个源码包呢(把楼上的脚本嵌套进循环结构)?或许有人说,我可以在屏幕输出里记住有多少顶层目录啊——那如果一个源码包包含了上百个或更多目录呢(如系统备份)?此其二

其实shell用途广泛,变化多端。
Bash是Linux的半壁江山,而gawk&(GNU)sed就是Bash的左右手。

此帖于 04-04-03 13:29 被 home_king 编辑.
  home_king 当前离线   回复时引用此帖
发表新主题 回复


主题工具

发帖规则
您 [不可以] 发表新主题
您 [不可以] 回复主题
您 [不可以] 上传附件
您 [不可以] 编辑您的帖子

已 [启用] BB 代码
已 [启用] 表情符号
已 [启用] IMG 代码
已 [禁用] HTML 代码
[论坛跳转…]


所有时间均为[北京时间]。现在的时间是 12:38


Powered by vBulletin 版本 3.6.8
版权所有 ©2000 - 2010, Jelsoft Enterprises Ltd.
官方中文技术支持: vBulletin 中文
版权所有 ©2002 - 2009, LinuxSir.Org