Recursively listing the contents of a tar/zip archive

谁都会走 提交于 2019-12-08 12:44:01

问题


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/

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

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

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?


回答1:


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;
}

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.

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



回答2:


I wrote a Python script to recursively search archives, called arkfind. You can omit the search text to just list all contents to an arbitrary depth.

$ arkfind ABCD.zip
ABCD.zip
  > somefile.txt
  > somezip.zip
      > (contents of somezip.zip)
  > someother.tar
      > (contents of someother.tar)


来源:https://stackoverflow.com/questions/16643969/recursively-listing-the-contents-of-a-tar-zip-archive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!