in reply to Re: help with Archive::tar
in thread help with Archive::tar

Thanks for the replies so far.

Still kind of new to Perl and always looking for insight when i hit a brick wall :)

I expect that it does not matter, but I noticed that the man page for Archive::Tar does say that to use "the full (unix) path (including file name)" as the second arg to the "extract_file" method. I suppose it might be surprising if this really made a difference, but you should be aware that in perl generally, you can use the unix-style forward-slash instead of dos-style back-slashes in path specs.

I could give this a shot and see what happens. Also I didnt know you can use unix-style forward slashes when you are working on a windows machine like that.

So for example instead of using something like U:\\EMM Loads (escapeing the backslash) i can do \U\EMM Loads ?

Anyway, I think the first reply is more likely to have nailed it. When you do "tar tf whatever.tar" on the solaris box, do you see one or more directory names and slashes in front of "toc"?

When i untar one of these on a solaris box the tarball would be named something like 81334567-JX4578.tar and when i untar it on the solaris box it would look like 81334567-COMPLETE/toc, 81334567-COMPLETE/<next file>, etc. Same if i just extract the whole tarball in winzip.

Replies are listed 'Best First'.
Re^3: help with Archive::tar
by graff (Chancellor) on Mar 31, 2006 at 03:12 UTC
    the tarball would be named something like 81334567-JX4578.tar and when i untar it on the solaris box it would look like 81334567-COMPLETE/toc, 81334567-COMPLETE/<next file>, etc.

    That means this particular tar ball contains a directory called "81334567-COMPLETE", and the file you want to extract is inside that directory. You need to pass "81334567-COMPLETE/toc" as first arg in the "extract_file" call.

    To do that in a programmatic way -- without having to hard-code the directory name for each tar file -- you could try something like this:

    #!/usr/bin/perl use strict; use Archive::Tar; my $wdir = "U:/EMM loads"; # load an array of tar file names: open( DIR, $wdir ) or die "$wdir: $!"; my @tarballs = grep /\.tar$/, readdir DIR; closedir DIR; # for each tar file, yank out the "toc" file: for my $tarball ( @tarballs ) { my $tar = Archive::Tar->new( "$wdir/$tarball" ); my ($toc) = grep { $_->name =~ m{/toc$} } $tar->get_files; # make up a local name for this toc file, based on the tarball name: ( my $tocname = $tarball ) =~ s/tar$/toc/; # write the toc file data to the local toc file: open( TOC, ">", $tocname ) or die "$tocname: $!"; print TOC $toc->get_content; close TOC; }
      Thank you for the examples, I am going give this a whirl today when i get into work!
        Actually, on second thought -- depending on the number of data files in your tar files, it might be quicker if you replace this line:
        my ($toc) = grep { $_->name =~ m{/toc$} } $tar->get_files;
        with this short-circuit loop:
        my $toc = undef; for my $file ( $tar->get_files ) { if ( $file->name =~ /toc$/ ) { $toc = $file; last; } } ( defined $toc ) or do { warn "no toc file in $wdir/$tarball\n"; n +ext };
        That assumes that the first time you see "toc" as the file name, this is the actual "toc" file that you want. (It also checks for the case where a tarball lacks a "toc" file.)
Re^3: help with Archive::tar
by mikeock (Hermit) on Mar 29, 2006 at 15:19 UTC
    Actually, the dir structure for unix is the other slash. / not \.

    Here is the example that I could find from my tar program for extracting:

    sub untar{ my $tar=Archive::Tar->new; @_ = glob ('*'); foreach my $otar(@_){ next unless($otar =~ /(\.tar)$/i); $tar->read($otar,1); my @contents = $tar->get_files(); foreach(@contents){ $tar->extract($_->{name}); print "$tar-error\n"; sleep 60; if($tar->error !~ //){ print "$_->{name} extracted\n"; }else{ print "Error extracting $_->{name} from $otar\n"; } } undef $tar; } }