Class SevenZip

java.lang.Object
net.sf.sevenzipjbinding.SevenZip

public class SevenZip extends Object
7-Zip-JBinding main class.
  • Finds and initializes 7-Zip-JBinding native library
  • Opens existing archives and returns implementation of IInArchive
  • Creates new archives by providing different implementations of IOutArchive

Initialization of the native library

Typically the library doesn't need an explicit initialization. The first call to an open/create archive method will try to initialize the native library by calling initSevenZipFromPlatformJAR() method. This initialization process requires a platform jar to be in a class path. The automatic initialization starts before the first access to an archive, if native library wasn't already initialized manually with one of the initSevenZip... methods. If manual or automatic initialization failed, no further automatic initialization attempts will be made. The initialization status and error messages can be obtained by following methods:
The platform jar is an additional jar file sevenzipjbinding-Platform.jar with one or more native libraries for respective one or more platforms. Here are some examples of 7-Zip-JBinding platform jar files.
  • sevenzipjbinding-Linux-i386.jar with native library for exact one platform: Linux, 32 bit
  • sevenzipjbinding-AllWindows.jar with native libraries for two platforms: Windows 32 and 64 bit
  • sevenzipjbinding-AllPlatforms.jar with native libraries for all available platforms
The single and multiple platform jar files can be determined by counting dashes in the filename. Single platform jar files always contain two dashes in their names.

Here is a schema of the different initialization processes:
  • Initialization using platform jar
    • First, the list of the available native libraries is loaded from the /sevenzipjbinding-platforms.properties file on the class path by calling getPlatformList() method. The list is cached in a static variable.
    • The platform is chosen by calling getPlatformBestMatch method. If the list of available platforms contains exactly one platform, that platform will always be the best match. If more than one platform is available to choose from, the system properties os.arch and os.name (first part) are used to make the choice.
    • The list of the native libraries is determined by reading /ChosenPlatform/sevenzipjbinding-lib.properties on the class path. The list contains names and hashes of the dynamic libraries located in the /ChosenPlatform/ directory in the same jar.
    • The dynamic libraries for the chosen platform are copied to the unique temporary directory using "build-ref" postfix. If not passed as a parameter for one of initSevenZipFromPlatformJAR(...) methods, the temporary directory is determined using system property java.io.tmpdir.
    • The dynamic libraries are reused, if the files are already present in the temporary directory and the hash sums are verified
    • The dynamic libraries are loaded into JVM using System.load(String) method.
    • 7-Zip-JBinding native initialization method called to complete initialization process.
  • Manual initialization

By default the initialization occurred within the AccessController.doPrivileged(java.security.PrivilegedAction) block. This can be overruled by setting sevenzip.no_doprivileged_initialization system property. For example:
java -Dsevenzip.no_doprivileged_initialization=1 ...

Temporary artifacts

During automatic initialization of the 7-Zip-JBinding the native libraries from the platform jar must be extracted to the disk in order to be loaded into the JVM. Since the count of the native libraries (depending on the platform) can be greater than one, a temporary sub-directory is created to hold those native libraries. The path to the directory for the temporary artifacts will be determined according to following rules (see createOrVerifyTmpDir method):
The list of the temporary created artifact can be obtained with getTemporaryArtifacts(). By default, 7-Zip-JBinding doesn't delete those artifacts trying to reduce subsequent initialization overhead. If 7-Zip-JBinding finds the native libraries within the temporary directory, it uses those without further verification. In order to allow smooth updates, the temporary sub-directory with the native libraries is named with a unique build reference number. If 7-Zip-JBinding gets updated, a new temporary sub-directory gets created and the new native libraries will be copied and used.

Opening existing archives

The methods for the opening archive files are

Creating new archives

There are two ways to create a new archive:
  • Use openOutArchive(ArchiveFormat) method. It will return an instance of the IOutCreateArchive<IOutItemAllFormats> interface allowing creation of an archive of any supported archive format. To get all currently supported formats see the 'compression' column of the ArchiveFormat -JavaDoc.
  • Use one of the SevenZip.openOutArchiveXxx methods, that provide implementations of the corresponding archive-format-specific interfaces. Those interfaces contain all supported configuration methods for the selected archive format and are more convenient in cases, where only one archive format should be supported.
For more information see IOutCreateArchive.

Updating existing archives

In order to update an existing archive three simple steps are necessary: During update operation user may copy item properties or item properties and content from the existing archive significantly improving performance compared to the extract-and-re-compress alternative.

For more information see IOutUpdateArchive.
Since:
4.65-1
Author:
Boris Brodski