Как распечатать dir дерево 7zip архива?

944
kev

Перед извлечением archive.7zя всегда проверяю, что в стороне:

$ 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 

Он напечатает длинное сообщение, если archive.7zсодержит много файлов и каталогов.

Это не очень полезно. Потому что я не вижу общей структуры archive.7z.

Есть возможность распечатать дерево каталогов перед тем, как извлечь его.

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

Я использую, tree -d archiveчтобы получить дерево после запуска 7z x archive.7z -oarchive.

Если я могу извлечь только каталоги archive.7z,
я могу запустить, tree -d archive ; rm -r archiveчтобы получить дерево.

4

2 ответа на вопрос

4
Daniel Andersson

Quick hack:

#!/bin/sh 7z l "$" |\ tail -n +17 |\ sed 's/.\//' |\ tac |\ awk 'NR>2 { n=split($6, a, "/") 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/.\//; 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(<>) for my $i (reverse 0..$#lines-2) 

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

1
Andreas

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.