Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Reading tar.gz file recursively

by vinoth.ree (Monsignor)
on Oct 13, 2017 at 05:20 UTC ( #1201287=perlquestion: print w/replies, xml ) Need Help??

vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

How to list out files from foo.tar.gz, where foo.tar.gz in bar.tar.gz file.

For example, I have bar.tar.gz file which contains many tar.gz files, I need to read bar.tar.gz file and find tar.gz files in it and read files from that tar.gz

bar.tar.gz--> foo.tar.gz --> text1.txt, text2.txt Here I need to list out those text1.txt and text2.txt files. I can do this by `tar-xzOf bar.tar.gz foo.tar.gz|tar tzf`. but to how to automate this thing? any module available for this ? suggestions are greatly appreciated.

All is well. I learn by answering your questions...

Replies are listed 'Best First'.
Re: Reading tar.gz file recursively
by atcroft (Abbot) on Oct 13, 2017 at 05:31 UTC

    After seeing your request in the Chatterbox earlier this evening, I hacked (in the bloody horror movie sense) together something that appears to do what you are requesting (view the content of text2.txt, for example, from within foo.tar.gz contained in baz.tar.gz). I hope the code below can give you at least a starting point to work from.

    Hope that helps.

      Here we need to pass outer and inner tar.gz filename right, while automating this I may nt know the inner tar.gz files, it should automatically need to find inner tar.gz files from outer.tar.gz file and list out the inner tar.gz file content.


      All is well. I learn by answering your questions...

        In the code I posted, lines 105-140, if no values for --inner_filename were given, I believe it will display all of the files contained within all filenames that end in .tar, .tar.gz, or .tgz. (Let me know if I am incorrect, however.)

        Hope that helps.

Re: Reading tar.gz file recursively
by thanos1983 (Parson) on Oct 13, 2017 at 09:37 UTC

    Hello vinoth.ree,

    Give a try to this, it seems to do the work that you ask for:

    #!/usr/bin/perl use strict; use warnings; use File::Temp; use feature 'say'; use Archive::Extract; my $indent = 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; } die qq|Usage: perl $0 <zip-file>\n| unless @ARGV == 1; printf qq|%s\n|, $ARGV[0]; $indent += 2; recursive_extract( shift ); __END__ $ perl test.pl TestPrimeZip.tar.gz TestPrimeZip.tar.gz TestPrimeZip/ZipFiles/in.txt TestPrimeZip/ZipFiles/ TestPrimeZip/ZipFiles/test.zip in.txt TestPrimeZip/ZipFiles.tar.gz TestPrimeZip/

    Source of code Recursively listing the contents of a tar/zip archive.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1201287]
Approved by atcroft
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2020-09-19 03:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (114 votes). Check out past polls.

    Notices?