Tar and unzip on single command

Hi All,

First of all I don't know whether this is possible. or no. Thought of getting experts thought.

I am having a tar file which contains zipped file in it . I tried individual command with extraction and it worked

tar -tvf TRANS_279.tar
-rw-rw-r-- qqa00 1394 2016-10-03 10:39:19 M9999_406.dat.zip
-rw-rw-r-- qqa00 142428 2016-10-03 10:43:58 P9999_406.dat.zip
-rw-rw-r-- qqa00  5504 2016-10-03 10:44:11 T9999_406.dat.zip

I want do unzip -l on the out of tar -tvf and see what are the files in it. Is that is possible which out extracting the tar

Thanks
Arun

Hi,
with gnu tar, you have option "--to-commande=COMMANDE", so you can try:

tar --to-commande="unzip -l" -xf TRANS_279.tar

Regards.

4 Likes

disedorgue's locale may have added a trailing "e"
to --to-commande .

Your locale may not support that. We have some linux boxes that are set to Spanish.
On those boxes, I mess up the spelling of some options/filenames for commands all the time, because I type without thinking.

1 Like

I think I made the mistake when I copied it by hand the test I made from another workstation.

Thanks.

---------- Post updated at 09:11 PM ---------- Previous update was at 07:24 PM ----------

I'm very confused, but not work with unzip because not supported stdin stream, just file :frowning:

zip files unfortunately don't stream well. They've got a table of contents and do lots of seeking around. You might have funzip, which can extract the first file from a zip via pipe, but only the first file.

We can replace unzip by script perl, like this script to show only file name in zip archive:

$ unzip -l f1.zip 
Archive:  f1.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
      283  2017-06-17 01:54   fich1.txt
      204  2017-06-17 01:54   fich2.txt
---------                     -------
      487                     2 files
$ unzip -l f2.zip 
Archive:  f2.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
      487  2017-06-17 01:55   fich3.txt
      974  2017-06-17 01:56   fich4.txt
---------                     -------
     1461                     2 files
$ tar tf arch.tar 
f1.zip
f2.zip
$ tar xf arch.tar -O | perl ./unziplist.pl 
fich1.txt
fich2.txt
fich3.txt
fich4.txt

or

$ tar xf arch.tar --to-command="perl ./unziplist.pl"
fich1.txt
fich2.txt
fich3.txt
fich4.txt

Perl script code:

#!/usr/bin/env perl

use strict;
use warnings;
use IO::Uncompress::Unzip qw(unzip $UnzipError) ;

my $status=1;
my $z = new IO::Uncompress::Unzip "-"
	or die "IO::Uncompress::Unzip failed: $UnzipError\n";

$status = $z->nextStream() while $status;
my @hdrs = $z->getHeaderInfo();

for (@hdrs) { print $$_{"Name"}."\n"; }

EDIT: perl script more short:

#!/usr/bin/env perl

use strict;
use warnings;
use IO::Uncompress::Unzip qw($UnzipError) ;

my $z = new IO::Uncompress::Unzip "-"
    or die "IO::Uncompress::Unzip failed: $UnzipError\n";

do { print $z->getHeaderInfo()->{"Name"}."\n" } while $z->nextStream();

Regards.

Hi arunkumar_mca,

The answer to your question, unfortunately, is no. It is not possible to know the content of the zip files without untarring them first. Regardless of other suggestions.
The tar -tvf will present to you a list of the content in the tar, but that's a list only, not the actual zip file and unzip -l does need to have the zip file to tell you what its content is. Therefore, the only way is to untar (extract the tar) first. And then let unzip -l read the content of it and report to you.

1 Like

I' m remember that jar command manage also zip archive and can to work with stream:

$ tar xf arch.tar --to-command="jar tv"
   283 Sat Jun 17 01:54:41 CEST 2017 fich1.txt
   204 Sat Jun 17 01:54:54 CEST 2017 fich2.txt
   487 Sat Jun 17 01:55:34 CEST 2017 fich3.txt
   974 Sat Jun 17 01:56:01 CEST 2017 fich4.txt

Regards.

1 Like

It would still be extracting and the GNU extension may not be even available.