递归列出一个tar / zip压缩包中的内容 [英] Recursively listing the contents of a tar/zip archive

查看:461
本文介绍了递归列出一个tar / zip压缩包中的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何得到压缩/ tar文件的内容,例如: http://www.if-not-true-then-false.com/2010/list-tar-tar-gz-tar-bz2-contents/

I understand how to get contents of zip/tar files, for example: http://www.if-not-true-then-false.com/2010/list-tar-tar-gz-tar-bz2-contents/

但在我的情况:我希望得到一个zip压缩包中的所有内容。

But in my case: I want to get all contents of a zip archive.

ABCD.zip
  -->somefile.txt
  -->somezip.zip
  -->someother.tar

目的:我想获得ABCD.zip的内容,这样我也得到进一步的东西里面是somezip.zip和someother.tar和someother.tar可能也有一些其他的拉链等。我怎么能做到这一点递归?可能与一个bash / perl脚本?

OBJECTIVE: I want to get contents of ABCD.zip so that I also get what is further inside somezip.zip and someother.tar, and someother.tar may also have some other zips etc. How can I do that with recursion? Possibly with a bash/perl script?

推荐答案

下面一个 perl的脚本,该脚本将列出包括递归超过压缩所有文件焦油文件:

Here a perl script that will list all files including recursion over zip and tar files:

#!/usr/bin/env perl

use strict;
use warnings;
use Archive::Extract;
use File::Temp;

my ($indent) = (0);

die qq|Usage: perl $0 <zip-file>\n| unless @ARGV == 1;

printf qq|%s\n|, $ARGV[0];
$indent += 2;
recursive_extract( shift );

exit 0;

sub recursive_extract {
        my ($file) = @_; 
        my $tmpdir = File::Temp->newdir;

        my $ae = Archive::Extract->new(
                archive => $file,
        );  

        $ae->extract( to => $tmpdir->dirname );

        for my $f ( @{ $ae->files } ) { 
                printf qq|%s%s\n|, q| | x $indent, $f; 
                if ( $f =~ m/\.(?:zip|tar)\z/ ) { 
                        $indent += 2;
                        recursive_extract( $f );
                }   
        }   

        $indent -= 2;
}

一些缺点:它不缓存已处理的文件,因此,如果有相同的COM pressed文件,它会提取并重新读取它们。它会搜索COM pressed文件来只在它们的延伸,而不是他们的内容。因此,它可以为任何人谁需要或者希望得到改善。

Some drawbacks: It doesn't cache already processed files, so if there are identical compressed files, it will extract and read them again. And it will search for compressed files looking only in their extension, not their content. So it can be improved for anyone who need or want it.

假设下面的脚本被命名为 script.pl ,给拉链文件作为参数,运行它,如:

Assuming following script is named script.pl, give the zip file as argument, running it like:

perl script.pl myzip.zip

在我的测试产生类似:

And in my test it yields something like:

myzip.zip
  f1
  f2
  f3
  f4
  mytar.tar
    f5
    f6
    f7
    f8
    testtar.tar
      f11
      f12
      f13
      f14
  testtar.tar
    f11
    f12
    f13
    f14
  testzip.zip
    fd
    fd2

这篇关于递归列出一个tar / zip压缩包中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆