Extraction code snippets

Index

Extracting & browsing existing archives

Overview

Here you can find many code snippets, example of how to perform basic operations with 7-Zip-JBinding. All examples are part of the JUnit tests. Each example runs on one of the test archives. JUnit tests check, that the output of the snippet is exactly the one published on this page. Below is a list of the test archives for this page. You can download all test archives in as a single ZIP file.


Standard vs Simple interface

The 7-Zip-JBinding library offers two different interfaces: standard and simple. The standard interface directly maps all native 7-Zip library methods providing C++ like interface. The simple interface is a try to provide more Java-like easy to use interface. It was designed either for quick coding and easy start and not for feature richness nor for performance.

The following examples will be presented in both standard and simple interface beginning with as simple interface. Please note, that the full performance can only be reached with the standard interface.


Quering items in archive

Simple interface

This example shows how to print a list of items of archive using simple interface.

##INCLUDE_SNIPPET(ListItemsSimple)

The output for the test archive:

##INCLUDE_OUTPUT(ListItemsOutput)

Please note, that not all archive formats support the "folder" items, like "folder/" in our example. Also please note, that packed size in this example as almost all other properties can return null. A returned null value means, that either the property is not supported by the particular archive format or not known for the particular item.

Standard interface

This example shows how to print a list of items of archive using standard interface.

##INCLUDE_SNIPPET(ListItemsStandard)

The output for the test archive is the same:

##INCLUDE_OUTPUT(ListItemsOutput)

Extracting of a single file

This section will show how to access the compressed data of the archive items. The following examples will calculate a simple checksum of each archive item.

Simple interface

This example shows how to get content of the items of the archive using simple interface. In order to consume extracted content the simple checksum will be calculated.

##INCLUDE_SNIPPET(ExtractItemsSimple)

Here is the output of the last snippet:

##INCLUDE_OUTPUT(ExtractItems)

Standard interface

The following two examples show how to perform the same extracting task using standard interface. As in the previous example, the simple checksum will be calculated in order to consume the extracted data.

First example traverse the entire archive and uses call back method to choose on the fly whether proceed with the extracting of an item or not. The second example prepare a list of items for extracting.

Extracting archive using standard interface and call back method as a filter

##INCLUDE_SNIPPET(ExtractItemsStandardCallback)

Extracting archive using standard interface and prepared list of items to extract

##INCLUDE_SNIPPET(ExtractItemsStandard)

Open multi-part archives

Dealing with a multi-part archives is not so easy, as dealing with a ordinary single part archives. Different archive formats need different approaches and therefore will be discussed separately.

Open multi-part 7z archives

Multi-part 7z archives are build by simple splitting of solid 7z archives into multiple peaces. This means, you can easy convert a multi-part 7z archive into single part 7z archive by concatenation of all archive parts into a one single file. Here is an example, how you can do this in a sh-shell.

$ ###
$ ### Compress some files into 7z archive.
$ ### Create mult-part archive 10.240 bytes for each part.
$ ###
$ 7z a test.7z -v10k somedir/
...
$ ls -l
total 32
-rw-r--r-- 1 boris boris 10240 2009-11-29 01:17 test.7z.001
-rw-r--r-- 1 boris boris 10240 2009-11-29 01:17 test.7z.002
-rw-r--r-- 1 boris boris  6684 2009-11-29 01:17 test.7z.003
$ ###
$ ### Concatenate all parts into a single file
$ ###
$ cat test.7z.* > test.7z
$ ls -l
total 60
-rw-r--r-- 1 boris boris 27164 2009-11-29 01:18 test.7z
-rw-r--r-- 1 boris boris 10240 2009-11-29 01:17 test.7z.001
-rw-r--r-- 1 boris boris 10240 2009-11-29 01:17 test.7z.002
-rw-r--r-- 1 boris boris  6684 2009-11-29 01:17 test.7z.003
$ ###
$ ### Test archive
$ ###
$ 7z t test.7z
...
Everything is Ok
...
###
### Split solid archive back into peaces
### Split starts counting from 000, so rename files
###
$ split  -b 15000 -a 3 -d test.7z test2.7z.
$ mv test2.7z.001 test2.7z.002
$ mv test2.7z.000 test2.7z.001
$ ls -l
total 88
-rw-r--r-- 1 boris boris 15000 2009-11-29 01:36 test2.7z.001
-rw-r--r-- 1 boris boris 12164 2009-11-29 01:36 test2.7z.002
-rw-r--r-- 1 boris boris 27164 2009-11-29 01:22 test.7z
-rw-r--r-- 1 boris boris 10240 2009-11-29 01:17 test.7z.001
-rw-r--r-- 1 boris boris 10240 2009-11-29 01:17 test.7z.002
-rw-r--r-- 1 boris boris  6684 2009-11-29 01:17 test.7z.003
$ ###
$ ### Test new multi-part archive file archive
$ ###
$ 7z t test2.7z.001
...
Everything is Ok
...

The naming conventions for the volumes of a multi-part 7z archive are:

It's important to provide corrent names in IArchiveOpenVolumeCallback.getProperty() method and to VolumedArchiveInStream class. Also you will probably need to parse filenames passed to IArchiveOpenVolumeCallback.getStream(String filename) in order to get a number of the required volume.

Here is an example of how to open a multi-part archive directly from the file system. In this example all the volumes are named correctly, so it's no need to parse requred volume name to extract the index. The volumes already exists under the passed names und can be directly opened.

##INCLUDE_SNIPPET(OpenMultipartArchive7z)

Here is the output of the last snippet:

##INCLUDE_OUTPUT(OpenMultipart7zArchiveOutput)

Open multi-part RAR archives

Since RAR uses its own proprietary format for splitting archives into multiple volumes, there is no need to use VolumedArchiveInStream in order to open multi-part RAR archives. To provide access to the different volumes of a RAR archive, the archive open callback class should additionally implement the IArchiveOpenVolumeCallback interface. Here is an example:

##INCLUDE_SNIPPET(OpenMultipartArchiveRar)

Here is the output of the last snippet:

##INCLUDE_OUTPUT(OpenMultipartRarArchiveOutput)