Examples

Factories

Compress provides factory methods to create input/output streams based on the names of the compressor or archiver format as well as factory methods that try to guess the format of an input stream.

To create a compressor writing to a given output by using the algorithm name:

CompressorOutputStream gzippedOut = new CompressorStreamFactory()
    .createCompressorOutputStream("gz", myOutputStream);

Make the factory guess the input format for a given stream:

ArchiveInputStream input = new ArchiveStreamFactory()
    .createArchiveInputStream(originalInput);

ar

In addition to the information stored in ArchiveEntry a ArArchiveEntry stores information about the owner user and group as well as Unix permissions.

Adding an entry to an ar archive:

ArArchiveEntry entry = new ArArchiveEntry(name, size);
arOutput.putNextEntry(entry);
arOutput.write(contentOfEntry);
arOutput.closeArchiveEntry();

Reading entries from an ar archive:

ArArchiveEntry entry = (ArArchiveEntry) arInput.getNextEntry();
byte[] content = new byte[entry.getSize()];
LOOP UNTIL entry.getSize() HAS BEEN READ {
    arInput(read, offset, content.length - offset);
}

cpio

In addition to the information stored in ArchiveEntry a CpioArchiveEntry stores various attributes including information about the original owner and permissions.

The cpio package supports the "new portable" as well as the "old" format of CPIO archives in their binary, ASCII and "with CRC" variants.

Adding an entry to a cpio archive:

CpioArchiveEntry entry = new CpioArchiveEntry(name, size);
cpioOutput.putNextEntry(entry);
cpioOutput.write(contentOfEntry);
cpioOutput.closeArchiveEntry();

Reading entries from an cpio archive:

CpioArchiveEntry entry = cpioInput.getNextCPIOEntry();
byte[] content = new byte[entry.getSize()];
LOOP UNTIL entry.getSize() HAS BEEN READ {
    cpioInput(read, offset, content.length - offset);
}

tar

In addition to the information stored in ArchiveEntry a TarArchiveEntry stores various attributes including information about the original owner and permissions.

There are several different tar formats and the TAR package of Compress 1.0 only provides the common functionality of the existing variants.

The original format didn't support file names longer than 100 characters and the tar package will fail if you try to add an entry longer than that. The longFileMode option of TarArchiveOutputStream can be used to make the archive truncate such names or use the GNU tar variant of storing such names. If you choose the GNU tar option, the archive can not be extracted using many other tar implementations like the ones of OpenBSD, Solaris or MacOS X.

TarArchiveInputStream will recognize the GNU tar extension for long file names and read the longer names accordingly.

Adding an entry to a tar archive:

TarArchiveEntry entry = new TarArchiveEntry(name);
entry.setSize(size);
tarOutput.putNextEntry(entry);
tarOutput.write(contentOfEntry);
tarOutput.closeArchiveEntry();

Reading entries from an tar archive:

TarArchiveEntry entry = tarInput.getNextTarEntry();
byte[] content = new byte[entry.getSize()];
LOOP UNTIL entry.getSize() HAS BEEN READ {
    tarInput(read, offset, content.length - offset);
}

zip

The ZIP package has a dedicated documentation page.

Adding an entry to a zip archive:

ZipArchiveEntry entry = new ZipArchiveEntry(name);
entry.setSize(size);
zipOutput.putNextEntry(entry);
zipOutput.write(contentOfEntry);
zipOutput.closeArchiveEntry();

Reading entries from an zip archive:

ZipArchiveEntry entry = zipInput.getNextZipEntry();
byte[] content = new byte[entry.getSize()];
LOOP UNTIL entry.getSize() HAS BEEN READ {
    zipInput(read, offset, content.length - offset);
}

Reading entries from an zip archive using the recommended ZipFile class:

ZipArchiveEntry entry = zipFile.getEntry(name);
InputStream content = zipFile.getInputStream(entry);
try {
    READ UNTIL content IS EXHAUSTED
} finally {
    content.close();
}

jar

In general, JAR archives are ZIP files, so the JAR package supports all options provided by the ZIP package.

To be interoperable JAR archives should always be created using the UTF-8 encoding for file names (which is the default).

Archives created using JarArchiveOutputStream will implicitly add a JarMarker extra field to the very first archive entry of the archive which will make Solaris recognize them as Java archives and allows them to be used as executables.

Note that ArchiveStreamFactory doesn't distinguish ZIP archives from JAR archives, so if you use the one-argument createArchiveInputStream method on a JAR archive, it will still return the more generic ZipArchiveInputStream.

The JarArchiveEntry class contains fields for certificates and attributes that are planned to be supported in the future but are not supported as of Compress 1.0.

Adding an entry to a jar archive:

JarArchiveEntry entry = new JarArchiveEntry(name, size);
entry.setSize(size);
jarOutput.putNextEntry(entry);
jarOutput.write(contentOfEntry);
jarOutput.closeArchiveEntry();

Reading entries from an jar archive:

JarArchiveEntry entry = jarInput.getNextJarEntry();
byte[] content = new byte[entry.getSize()];
LOOP UNTIL entry.getSize() HAS BEEN READ {
    jarInput(read, offset, content.length - offset);
}

bzip2

Note that BZipCompressorOutputStream keeps hold of some big data structures in memory. While it is true recommended for any stream that you close it as soon as you no longer needed, this is even more important for BZipCompressorOutputStream.

Uncompressing a given bzip2 compressed file (you would certainly add exception handling and make sure all streams get closed properly):

FileInputStream in = new FileInputStream("archive.tar.bz2");
FileOutputStream out = new FileOutputStream("archive.tar");
BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in);
final byte[] buffer = new byte[buffersize];
int n = 0;
while (-1 != (n = bzIn.read(buffer))) {
    out.write(buffer, 0, n);
}
out.close();
bzIn.close();

gzip

The implementation of this package is provided by the java.util.zip package of the Java class library.

Uncompressing a given bzip2 compressed file (you would certainly add exception handling and make sure all streams get closed properly):

FileInputStream in = new FileInputStream("archive.tar.gz");
FileOutputStream out = new FileOutputStream("archive.tar");
GZipCompressorInputStream bzIn = new GZipCompressorInputStream(in);
final byte[] buffer = new byte[buffersize];
int n = 0;
while (-1 != (n = bzIn.read(buffer))) {
    out.write(buffer, 0, n);
}
out.close();
bzIn.close();