Previewing Files in tar and zip Archives

Open source software is often distributed as archived code: compressed tar balls (.tar.gz) or as .zip files. A developer might download and install a number of these archives directly in a project they’re working on, or the archive may be installed indirectly via a software module manager like Maven.

But sometimes the developer may want to check selected contents of the archive – some file may have particular information, like the exact major/minor version of the archived software, an INSTALL or README file with information on how to use the code, or maybe configuration files that could be customized with a copy in the local environment.

For a tar file, the “x” option extracts a file from the archive; specifying the “O” (capital letter “O”) option along with it will display that file in the console window instead of writing it to the filesystem:

$ tar Oxvf somearchive.tar path/to/file.txt

For a zip file, “unzip -p” similarly displays a file from the archive to console:

$ unzip -p somearchive.zip path/to/file.txt

As an example, look at the Tomcat java lang commons library:

$ tar tf commons-lang3-3.6-bin.tar.gz | less
commons-lang3-3.6/CONTRIBUTING.md
commons-lang3-3.6/LICENSE.txt
commons-lang3-3.6/NOTICE.txt
commons-lang3-3.6/README.md
commons-lang3-3.6/RELEASE-NOTES.txt
commons-lang3-3.6/commons-lang3-3.6-javadoc.jar
commons-lang3-3.6/commons-lang3-3.6-sources.jar
commons-lang3-3.6/commons-lang3-3.6-test-sources.jar
commons-lang3-3.6/commons-lang3-3.6-tests.jar
commons-lang3-3.6/commons-lang3-3.6.jar
commons-lang3-3.6/apidocs/
commons-lang3-3.6/apidocs/org/
commons-lang3-3.6/apidocs/org/apache/

Let’s take a look at the RELEASE-NOTES.txt file:

$ tar Oxf commons-lang3-3.6-bin.tar.gz commons-lang3-3.6/RELEASE-NOTES.txt | less
                           Apache Commons Lang
                               Version 3.6
                              Release Notes
 
INTRODUCTION:
 
This document contains the release notes for the 3.6 version of
Apache Commons Lang as well as a history all changes in the Commons Lang 3.x
release line. Commons Lang is a set of utility functions and reusable
...more text...

Looking at the .zip file download of the commons lang library:

$ unzip -l commons-lang3-3.6-bin.zip | less
Archive:  commons-lang3-3.6-bin.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
     5440  06-06-17 15:09   commons-lang3-3.6/CONTRIBUTING.md
    53733  06-09-17 11:38   commons-lang3-3.6/RELEASE-NOTES.txt
   840113  06-09-17 11:40   commons-lang3-3.6/commons-lang3-3.6-tests.jar
     7264  06-09-17 11:40   commons-lang3-3.6/apidocs/org/apache/commons/lang3/builder/HashCodeExclude.html
...more files...
$ unzip -p commons-lang3-3.6-bin.zip commons-lang3-3.6/CONTRIBUTING.md | less
...header text...
Contributing to Apache Commons Lang
======================
 
You have found a bug or you have an idea for a cool new feature? Contributing code is a great way to give something back to
the open source community. Before you dig right into the code there are a few guidelines that we need contributors to
follow so that we can have a chance of keeping on top of things.
 
Getting Started
---------------
 
+ Make sure you have a [JIRA account](https://issues.apache.org/jira/).
+ Make sure you have a [GitHub account](https://github.com/signup/free).
...more text...

The Java Archive (.jar) file uses a slightly enhanced version of the zip file format:

  • the jar allows longer file names which arise from long package paths for class files
  • also include structured attribute information in the META-INF/MANIFEST.MF file

Untar the archive and inspect the jar file containing the built code:

$ jar tf commons-lang3-3.6/commons-lang3-3.6.jar | less
META-INF/MANIFEST.MF
META-INF/
org/
org/apache/
org/apache/commons/
org/apache/commons/lang3/
org/apache/commons/lang3/arch/
org/apache/commons/lang3/builder/
org/apache/commons/lang3/concurrent/
org/apache/commons/lang3/event/
org/apache/commons/lang3/exception/
org/apache/commons/lang3/math/
org/apache/commons/lang3/mutable/
org/apache/commons/lang3/reflect/
org/apache/commons/lang3/text/
org/apache/commons/lang3/text/translate/
org/apache/commons/lang3/time/
org/apache/commons/lang3/tuple/
META-INF/maven/
META-INF/maven/org.apache.commons/
META-INF/maven/org.apache.commons/commons-lang3/
META-INF/LICENSE.txt
...more files...
$ unzip -p commons-lang3-3.6/commons-lang3-3.6.jar META-INF/MANIFEST.MF | less
Manifest-Version: 1.0
Bundle-License: https://www.apache.org/licenses/LICENSE-2.0.txt
Bundle-SymbolicName: org.apache.commons.lang3
Archiver-Version: Plexus Archiver
Built-By: britter
Bnd-LastModified: 1497001210959
Specification-Title: Apache Commons Lang
Implementation-Vendor-Id: org.apache.commons
Bundle-DocURL: http://commons.apache.org/proper/commons-lang/
Include-Resource: META-INF/LICENSE.txt=LICENSE.txt,META-INF/NOTICE.txt
 =NOTICE.txt
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.7))"
Export-Package: org.apache.commons.lang3;version="3.6",org.apache.comm
 ons.lang3.arch;version="3.6",org.apache.commons.lang3.builder;version
 ="3.6",org.apache.commons.lang3.concurrent;version="3.6",org.apache.c
 ommons.lang3.event;version="3.6",org.apache.commons.lang3.exception;v
 ersion="3.6",org.apache.commons.lang3.math;version="3.6",org.apache.c
 ommons.lang3.mutable;version="3.6",org.apache.commons.lang3.reflect;v
 ersion="3.6",org.apache.commons.lang3.text;version="3.6",org.apache.c
 ommons.lang3.text.translate;version="3.6",org.apache.commons.lang3.ti
 me;version="3.6",org.apache.commons.lang3.tuple;version="3.6"
Bundle-Name: Apache Commons Lang
...more text...
Bundle-Version: 3.6.0
Created-By: Apache Maven Bundle Plugin
Build-Jdk: 1.8.0_112
Specification-Version: 3.6

(in the above example, the manifest attributes like “Bundle-Name” and “Export-Package” are related to OSGI-standard tools that can be used to manage modularity in Java code)

For more extensive access to a Java Jar file, the core JDK includes a java.util.jar package which can be used to write a program that reads the contents of a jar file, including a java.util.jar.Manifest class to access the contents of the Manifest file.

For convenience, these commands can be wrapped in Linux style aliases (works on Mac OS X too): put these in your $HOME/.bashrc file:

alias tview='tar Oxf'
alias zview='unzip -p'
alias jmfview='function _jmfv(){ unzip -p "$1" META-INF/MANIFEST.MF; }; _jmfv'

Some extended bash shell syntax had to be used to pass the path to the jar file to the ‘jmfview’ alias, details are here.

TL; DR

Examining a file in a tar archive:

$ tar Oxvf somearchive.tar path/to/file.txt

Examining a file in a zip archive:

$ unzip -p somearchive.zip path/to/file.txt

Viewing the manifest file in a Java jar archive:

$ unzip -p somejavacode.jar META-INF/MANIFEST.MF | less

References

Tar Utility (Wikipedia)

Zip File Format(Wikipedia)

Jar File Format(Wikipedia)

JAR File Specification: JAR Manifest

Tomcat Commons Lang Library

Hello, OSGi, Part 1: Bundles for beginners | JavaWorld (2008)

OSGi™ Alliance – The Dynamic Module System for Java

Versions

$ tar –version
bsdtar 2.8.3 – libarchive 2.8.3
$ unzip –help
UnZip 5.52 of 28 February 2005, by Info-ZIP. Maintained by C. Spieler.
$ java -version
java version “1.8.0_40”
Java(TM) SE Runtime Environment (build 1.8.0_40-b25)

Add a Comment