Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Before extract archive.7z, I always check what in side:

$ 7z l archive.7z

...SKIP...

2010-01-01 00:00:00 .....        25856         7367  jsloader/resource/gre/modules/source-editor-textarea.jsm
2010-01-01 00:00:00 .....         4656         1669  jsloader/resource/gre/modules/FileUtils.jsm
2010-01-01 00:00:00 .....         1856          943  jsloader/resource/gre/modules/DownloadPaths.jsm
2010-01-01 00:00:00 .....         7096         2724  jsloader/resource/gre/modules/CertUtils.jsm
2010-01-01 00:00:00 .....          540          346  jsloader/resource/gre/modules/jsdebugger.jsm
2010-01-01 00:00:00 .....        12964         4225  jsloader/resource/gre/modules/CommonDialog.jsm
2010-01-01 00:00:00 .....         9692         3272  jsloader/resource/gre/modules/NetworkHelper.jsm
2010-01-01 00:00:00 .....        11252         3503  jsloader/resource/gre/modules/AutocompletePopup.jsm
------------------- ----- ------------ ------------  ------------------------
                              17928547      5269642  1489 files, 0 folders

It will print a long message, if archive.7z contains a lot of files and dirs.

It's not very useful. Because I cannot see the overall structure of archive.7z.

Is is possible to print out a dir tree before extract it.

├── jsloader
│   └── resource
│       └── gre
│           ├── components
│           └── modules
│               ├── devtools
│               ├── services-crypto
│               ├── services-sync
│               │   ├── engines
│               │   └── ext
│               └── tabview

I use tree -d archive to get the tree after running 7z x archive.7z -oarchive.

If I can extract only the directories of archive.7z,
I can run tree -d archive ; rm -r archive to get the tree.

share|improve this question

2 Answers 2

You can run a little bash script to do your job for you. As I have no access to Linux and not enough time right now, I can only give you a rundown.

  • list the archive contents
  • Use split or whatever command of your choice to extract the path
  • output the folder name once
  • for every "/" character you find, you indent your output

This should give you an output similar to tree. You can also write a small C++ program to print that for you, that might be easier or prettier.

share|improve this answer

Quick hack:

#!/bin/sh
7z l "${1}" |\
tail -n +17 |\
sed 's/.\{53\}//' |\
tac |\
awk 'NR>2 {
    n=split($6, a, "/")
    {for (i=1; i<n; i++)
        printf "   "
    }
    print a[n]
}'

Save as 7ztree, use as

$ 7ztree archive.7z
jsloader
   resource
      gre
         modules
            NetworkHelper.jsm
            CommonDialog.jsm
            jsdebugger.jsm
            CertUtils.jsm
            DownloadPaths.jsm
            FileUtils.jsm
            source-editor-textarea.jsm
  • tail is used to strip irrelevant information. 17 here, 53 for sed and 2 for awk were the correct magic numbers on my 7-zip version at least.
  • sed will strip the first magic 53 characters (this is to better the handling of spaces in awk).
  • tac is used to reverse input (otherwise the tree would be upside down the way 7z presents the listing).

It is straight forward, but quirky, to add logic to get the same fancy output as tree has.

awk could be used to filter and reverse lines in a single command instead of tail and tac, but it would be a bit more convoluted.

EDIT: Added sed to better handle spaces. And on that note, a pure sed version with the same output as the above script in its current form:

#!/bin/sh
7zr l ../testing.7z |\
tail -n +17 |\
tac |\
tail -n +3 |\
sed 's/.\{53\}//; s#[^/]*/#   #g'

but this will not be easy to get nicer output from.


EDIT2: And some Perl golf! :-D

#!/usr/bin/perl
my @lines;
my $i=0;
while(<>) {next if ++$i<17; push @lines,substr $_,53}
for my $i (reverse 0..$#lines-2) {print ' 'x3x$lines[$i]=~tr#/##.$lines[$i]}

If one adds some line breaks in that, it is probably the easiest way to build nice output formatting.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.