0

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?

Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71
django
  • 2,809
  • 5
  • 47
  • 80

2 Answers2

3

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
Birei
  • 35,723
  • 2
  • 77
  • 82
0

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)
detly
  • 29,332
  • 18
  • 93
  • 152