I was building some jar’s for a Jenkins shared pipeline and trying to validate what files/folders were included in the result. Normally I use the tree command to inspect directories like this, as it makes more visual sense to me. However to view the contents of a zip we have to do something like unzip -l filename.zip which gives us a pretty verbose and difficult to understand result (at least to me).

Solution

Add a the following function to my ~/.zshrc

function tree() {
	arr=( "${@}" )
	if ( file $arr[-1] | grep -q 'Zip archive' ) ; then
		tar tf $arr[-1] | tree ${arr[@]:0:-1} --fromfile .
	else
		command tree $@
	fi
}

Now we can tree somefolder just as well as tree somejar.jar, or any other Zip archive! For example:

» tree ~/.m2/repository/org/testng/testng/6.13.1/testng-6.13.1.jar
.
├─ com
│  └─ beust
│    └─ testng
│      └─ TestNG.class
├─ META-INF
│  ├─ services
│  │  └─ org.testng.xml.ISuiteParser
│  └─ MANIFEST.MF
├─ org
│  └─ testng
│    ├─ annotations
│    │  ├─ AfterClass.class
│    │  ├─ AfterGroups.class
│    │  ├─ AfterMethod.class
│    │  ├─ AfterSuite.class
│    │  ├─ AfterTest.class
│    │  ├─ BeforeClass.class