Package net.sf.sevenzipjbinding
Class SevenZip
java.lang.Object
net.sf.sevenzipjbinding.SevenZip
7-Zip-JBinding main class.
The platform jar is an additional jar file
Here is a schema of the different initialization processes:
By default the initialization occurred within the
The list of the temporary created artifact can be obtained with
For more information see
- 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 callinginitSevenZipFromPlatformJAR() 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:
isInitializedSuccessfully()- get initialization statusisAutoInitializationWillOccur()- determine, if an initialization attempt was madegetLastInitializationException()- get last thrown initialization exception
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.jarwith native library for exact one platform: Linux, 32 bitsevenzipjbinding-AllWindows.jarwith native libraries for two platforms: Windows 32 and 64 bitsevenzipjbinding-AllPlatforms.jarwith native libraries for all available platforms
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.propertiesfile on the class path by callinggetPlatformList()method. The list is cached in a static variable. - The platform is chosen by calling
getPlatformBestMatchmethod. 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 propertiesos.archandos.name(first part) are used to make the choice. - The list of the native libraries is determined by reading
/ChosenPlatform/sevenzipjbinding-lib.propertieson 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 propertyjava.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.
- First, the list of the available native libraries is loaded from the
- Manual initialization
- User loads 7-Zip-JBinding native dynamic libraries manually into JVM using
System.load(String)orSystem.loadLibrary(String) - User calls
initLoadedLibraries()method to initialize manually loaded native library
- User loads 7-Zip-JBinding native dynamic libraries manually into JVM using
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 (seecreateOrVerifyTmpDir
method):
- If path specified directly using
tmpDirectoryparameter ofinitSevenZipFromPlatformJAR(File)orinitSevenZipFromPlatformJAR(String, File)it will be used - If no path specified directly, the system property
java.io.tmpdirget used - If the system property
java.io.tmpdirisn't set, an exception get raised
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 areopenInArchive(ArchiveFormat, IInStream)- simple open archive method.openInArchive(ArchiveFormat, IInStream, IArchiveOpenCallback)- generic open archive method. It's possible to open all kinds of archives providing call back object that implements following interfacesIArchiveOpenCallback- base interface. Must be implemented by all call back classesICryptoGetTextPassword- (optional) Provides password encrypted indexIArchiveOpenVolumeCallback- (optional) Provides information about volumes in multipart archives. Currently used only for multipartRARarchives. For opening multipart7zarchives useVolumedArchiveInStream.
openInArchive(ArchiveFormat, IInStream, String)a shortcut method for opening password protected archives with an encrypted index.
Creating new archives
There are two ways to create a new archive:- Use
openOutArchive(ArchiveFormat)method. It will return an instance of theIOutCreateArchive<IOutItemAllFormats>interface allowing creation of an archive of any supported archive format. To get all currently supported formats see the 'compression' column of theArchiveFormat-JavaDoc. - Use one of the
SevenZip.openOutArchiveXxxmethods, 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.
IOutCreateArchive.
Updating existing archives
In order to update an existing archive three simple steps are necessary:- Open the existing archive need to be modified (getting an instance of the
IInArchiveinterface) - Call
IInArchive.getConnectedOutArchive()to get connected instance of theIOutUpdateArchiveinterface - Call
IOutUpdateArchive.updateItems(ISequentialOutStream, int, IOutCreateCallback)to start the archive update operation
For more information see
IOutUpdateArchive.- Since:
- 4.65-1
- Author:
- Boris Brodski
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic ThrowableReturns last native library initialization exception, if occurs.static StringChoose the platform whose native library best matches the current runtime, out of the platforms available in the platform jar(s) on the class path (seegetPlatformList()).Load list of the available platforms out ofsevenzipjbinding-Platform.jaron the class path.static StringReturn version of the 7-Zip-JBinding.static SevenZip.VersionReturn information about the native 7-Zip engine.static File[]Returns list of the temporary created artifacts (one directory and one or more files within this directory).static StringReturn the platform used for the initialization.static voidInitialize 7-Zip-JBinding native library without loading libraries in JVM first.static voidInitialize native SevenZipJBinding library assumingsevenzipjbinding-Platform.jaron the class path.static voidinitSevenZipFromPlatformJAR(File tmpDirectory) Initialize native SevenZipJBinding library assumingsevenzipjbinding-Platform.jaron the class path.static voidinitSevenZipFromPlatformJAR(String platform) Initialize native SevenZipJBinding library assumingsevenzipjbinding-Platform.jaron the class path.static voidinitSevenZipFromPlatformJAR(String platform, File tmpDirectory) Initialize native SevenZipJBinding library assumingsevenzipjbinding-Platform.jaron the class path.static booleanReturns whether automatic initialization will occur or not.static booleanTests native library initialization status of SevenZipJBinding.static IInArchiveopenInArchive(ArchiveFormat archiveFormat, IInStream inStream) Open archive of typearchiveFormatfrom the input streaminStream.static IInArchiveopenInArchive(ArchiveFormat archiveFormat, IInStream inStream, String passwordForOpen) Open archive of typearchiveFormatfrom the input streaminStreamusing 'archive open call-back' listenerarchiveOpenCallback.static IInArchiveopenInArchive(ArchiveFormat archiveFormat, IInStream inStream, IArchiveOpenCallback archiveOpenCallback) Open archive of typearchiveFormatfrom the input streaminStreamusing 'archive open call back' listenerarchiveOpenCallback.static IOutCreateArchive<IOutItemAllFormats>openOutArchive(ArchiveFormat archiveFormat) Create a new archive of typearchiveFormat.static IOutCreateArchive7zCreate a new 7z archive.static IOutCreateArchiveBZip2Create a new BZip2 archive.static IOutCreateArchiveGZipCreate a new GZip archive.static IOutCreateArchiveTarCreate a new Tar archive.static IOutCreateArchiveXzCreate a new Xz archive.static IOutCreateArchiveZipCreate a new Zip archive.
-
Method Details
-
isInitializedSuccessfully
public static boolean isInitializedSuccessfully()Tests native library initialization status of SevenZipJBinding. UsegetLastInitializationException()method to get more information in case of initialization failure.- Returns:
true- 7-Zip-JBinding native library was initialized successfully,false- the native library was not initialized successfully (yet).- See Also:
-
getLastInitializationException
Returns last native library initialization exception, if occurs.- Returns:
null- no initialization exception occurred (yet), else initialization exception- See Also:
-
isAutoInitializationWillOccur
public static boolean isAutoInitializationWillOccur()Returns whether automatic initialization will occur or not. Automatic initialization starts before opening an archive, if native library wasn't already initialized manually with one of theinitSevenZip...methods. If manual or automatic initialization failed, no further automatic initialization attempts will be made.- Returns:
trueautomatic initialization will occur,falseautomatic initialization will not occur- See Also:
-
getUsedPlatform
Return the platform used for the initialization. The Platform is one element out of the list of available platforms returned bygetPlatformList().- Returns:
- the platform used for the initialization or
nullif initialization wasn't performed yet. - See Also:
-
getPlatformList
Load list of the available platforms out ofsevenzipjbinding-Platform.jaron the class path.- Returns:
- list of the available platforms
- Throws:
SevenZipNativeInitializationException- indicates problems finding or parsing platform property file
-
getTemporaryArtifacts
Returns list of the temporary created artifacts (one directory and one or more files within this directory). The directory is always the last element in the array.- Returns:
- array of
Files.
-
initSevenZipFromPlatformJAR
Initialize native SevenZipJBinding library assumingsevenzipjbinding-Platform.jaron the class path. The platform-dependent library will be extracted from the jar file and copied to the temporary directory. Then it will be loaded into JVM usingSystem.load(String)method. Finally the library specific native initialization method will be called. Please see JavaDoc ofSevenZipfor detailed information.
If libraries for more than one platform exist, the choice will be made by callinggetPlatformBestMatch()method. UseinitSevenZipFromPlatformJAR(String)to set platform manually.- Throws:
SevenZipNativeInitializationException- indicates problems finding a native library, copying it into the temporary directory or loading it.- See Also:
-
initSevenZipFromPlatformJAR
public static void initSevenZipFromPlatformJAR(File tmpDirectory) throws SevenZipNativeInitializationException Initialize native SevenZipJBinding library assumingsevenzipjbinding-Platform.jaron the class path. The platform-dependent library will be extracted from the jar file and copied to the temporary directory. Then it will be loaded into JVM usingSystem.load(String)method. Finally the library specific native initialization method will be called. Please see JavaDoc ofSevenZipfor detailed information.
If libraries for more than one platform exist, the choice will be made by callinggetPlatformBestMatch()method. UseinitSevenZipFromPlatformJAR(String)to set platform manually.- Parameters:
tmpDirectory- temporary directory to copy native libraries to. This directory must be writable and contain at least 2 MB free space.- Throws:
SevenZipNativeInitializationException- indicates problems finding a native library, copying it into the temporary directory or loading it.- See Also:
-
initSevenZipFromPlatformJAR
public static void initSevenZipFromPlatformJAR(String platform, File tmpDirectory) throws SevenZipNativeInitializationException Initialize native SevenZipJBinding library assumingsevenzipjbinding-Platform.jaron the class path. The platform-dependent library will be extracted from the jar file and copied to the temporary directory. Then it will be loaded into JVM usingSystem.load(String)method. Finally the library specific native initialization method will be called. Please see JavaDoc ofSevenZipfor detailed information.
If libraries for more than one platform exist, the choice will be made by callinggetPlatformBestMatch()method. UseinitSevenZipFromPlatformJAR(String)to set platform manually.- Parameters:
tmpDirectory- temporary directory to copy native libraries to. This directory must be writable and contain at least 2 MB free space.platform- Platform to load native library for. The platform must be one of the elements of the list of available platforms returned bygetPlatformList().- Throws:
SevenZipNativeInitializationException- indicates problems finding a native library, copying it into the temporary directory or loading it.- See Also:
-
initSevenZipFromPlatformJAR
public static void initSevenZipFromPlatformJAR(String platform) throws SevenZipNativeInitializationException Initialize native SevenZipJBinding library assumingsevenzipjbinding-Platform.jaron the class path. The platform-dependent library will be extracted from the jar file and copied to the temporary directory. Then it will be loaded into JVM usingSystem.load(String)method. Finally the library specific native initialization method will be called. Please see JavaDoc ofSevenZipfor detailed information.
If libraries for more than one platform exist, the choice will be made by callinggetPlatformBestMatch()method. UseinitSevenZipFromPlatformJAR(String)to set platform manually.- Parameters:
platform- Platform to load native library for. The platform must be one of the elements of the list of available platforms returned bygetPlatformList().- Throws:
SevenZipNativeInitializationException- indicates problems finding a native library, copying it into the temporary directory or loading it.- See Also:
-
initLoadedLibraries
Initialize 7-Zip-JBinding native library without loading libraries in JVM first. Prevent automatic loading of 7-Zip-JBinding native libraries into JVM. This method will only call 7-Zip-JBinding internal initialization method, considering all needed native libraries as loaded. It method is useful, if the java application wants to load 7-Zip-JBinding native libraries manually.- Throws:
SevenZipNativeInitializationException- in case of an initialization error
-
openInArchive
public static IInArchive openInArchive(ArchiveFormat archiveFormat, IInStream inStream, IArchiveOpenCallback archiveOpenCallback) throws SevenZipException Open archive of typearchiveFormatfrom the input streaminStreamusing 'archive open call back' listenerarchiveOpenCallback. To open archive from the file, useRandomAccessFileInStream.- Parameters:
archiveFormat- format of archiveinStream- input stream to open archive fromarchiveOpenCallback- archive open call back listener to use. You can optionally implementICryptoGetTextPasswordto specify password to use.- Returns:
- implementation of
IInArchivewhich represents opened archive. - Throws:
SevenZipException- 7-Zip or 7-Zip-JBinding error occurs. UseSevenZipException.printStackTraceExtended()to get stack traces of this SevenZipException and of all thrown 'caused by' exceptions.NullPointerException- is thrown, if inStream is null- See Also:
-
openInArchive
public static IInArchive openInArchive(ArchiveFormat archiveFormat, IInStream inStream, String passwordForOpen) throws SevenZipException Open archive of typearchiveFormatfrom the input streaminStreamusing 'archive open call-back' listenerarchiveOpenCallback. To open archive from the file, useRandomAccessFileInStream.- Parameters:
archiveFormat- format of archiveinStream- input stream to open archive frompasswordForOpen- password to use. Warning: this password will not be used to extract item from archive but only to open archive. (7-zip format supports encrypted filename)- Returns:
- implementation of
IInArchivewhich represents opened archive. - Throws:
SevenZipException- 7-Zip or 7-Zip-JBinding error occurs. UseSevenZipException.printStackTraceExtended()to get stack traces of this SevenZipException and of all thrown 'caused by' exceptions.NullPointerException- is thrown, if inStream is null- See Also:
-
openInArchive
public static IInArchive openInArchive(ArchiveFormat archiveFormat, IInStream inStream) throws SevenZipException Open archive of typearchiveFormatfrom the input streaminStream. To open archive from the file, useRandomAccessFileInStream.- Parameters:
archiveFormat- (optional) format of archive. Ifnullarchive format will be auto-detected.inStream- input stream to open archive from- Returns:
- implementation of
IInArchivewhich represents opened archive. - Throws:
SevenZipException- 7-Zip or 7-Zip-JBinding error occurs. UseSevenZipException.printStackTraceExtended()to get stack traces of this SevenZipException and of all thrown 'caused by' exceptions.NullPointerException- is thrown, if inStream is null- See Also:
-
getPlatformBestMatch
Choose the platform whose native library best matches the current runtime, out of the platforms available in the platform jar(s) on the class path (seegetPlatformList()).The choice is made as follows:
- If only one platform is available, it is returned.
- An exact match of
<os.name>-<os.arch>is tried first (this covers e.g.Linux-amd64,Linux-i386,Windows-amd64). - Otherwise the runtime architecture is resolved to an ordered list of candidate arch suffixes
(see
PlatformArchDetector). This normalizesos.archspellings (aarch64→arm64,x86_64→amd64,i686→i386) and, in particular, resolves the 32-bit ARM sub-architecture: the JVM reportsos.arch=armfor all of armv5/armv6/armv7, so the CPU architecture level and the float ABI (soft = armel, hard = armhf) are detected (viauname, the ELF auxiliary vector, the ELF float-ABI flags and the dynamic loader). The first candidate that is actually available is returned.
SevenZipNativeInitializationExceptionis thrown with a detailed message that includes the detected candidates, the available platforms and the full detection trail.- Returns:
- the chosen platform name (e.g.
Linux-armv7) - Throws:
SevenZipNativeInitializationException- if no suitable platform could be chosen- See Also:
-
getPlatformList()PlatformArchDetector
-
getSevenZipVersion
Return information about the native 7-Zip engine.- Returns:
- Version
-
getSevenZipJBindingVersion
Return version of the 7-Zip-JBinding.- Returns:
- version of the 7-Zip-JBinding
-
openOutArchiveZip
Create a new Zip archive.- Returns:
- an out-archive object initialized to create the new Zip archive
- Throws:
SevenZipException- 7-Zip or 7-Zip-JBinding error occurs. UseSevenZipException.printStackTraceExtended()to get stack traces of this SevenZipException and of all thrown 'caused by' exceptions.- See Also:
-
openOutArchive7z
Create a new 7z archive.- Returns:
- an out-archive object initialized to create the new 7z archive
- Throws:
SevenZipException- 7-Zip or 7-Zip-JBinding error occurs. UseSevenZipException.printStackTraceExtended()to get stack traces of this SevenZipException and of all thrown 'caused by' exceptions.- See Also:
-
openOutArchiveTar
Create a new Tar archive.- Returns:
- an out-archive object initialized to create the new Tar archive
- Throws:
SevenZipException- 7-Zip or 7-Zip-JBinding error occurs. UseSevenZipException.printStackTraceExtended()to get stack traces of this SevenZipException and of all thrown 'caused by' exceptions.- See Also:
-
openOutArchiveBZip2
Create a new BZip2 archive.- Returns:
- an out-archive object initialized to create the new BZip2 archive
- Throws:
SevenZipException- 7-Zip or 7-Zip-JBinding error occurs. UseSevenZipException.printStackTraceExtended()to get stack traces of this SevenZipException and of all thrown 'caused by' exceptions.- See Also:
-
openOutArchiveGZip
Create a new GZip archive.- Returns:
- an out-archive object initialized to create the new GZip archive
- Throws:
SevenZipException- 7-Zip or 7-Zip-JBinding error occurs. UseSevenZipException.printStackTraceExtended()to get stack traces of this SevenZipException and of all thrown 'caused by' exceptions.- See Also:
-
openOutArchiveXz
Create a new Xz archive.- Returns:
- an out-archive object initialized to create the new Xz archive
- Throws:
SevenZipException- 7-Zip or 7-Zip-JBinding error occurs. UseSevenZipException.printStackTraceExtended()to get stack traces of this SevenZipException and of all thrown 'caused by' exceptions.- Since:
- 23.01-2.2
- See Also:
-
openOutArchive
public static IOutCreateArchive<IOutItemAllFormats> openOutArchive(ArchiveFormat archiveFormat) throws SevenZipException Create a new archive of typearchiveFormat.- Parameters:
archiveFormat- archive format of the new archive- Returns:
- an out-archive object initialized to create the new archive
- Throws:
SevenZipException- 7-Zip or 7-Zip-JBinding error occurs. UseSevenZipException.printStackTraceExtended()to get stack traces of this SevenZipException and of all thrown 'caused by' exceptions.- See Also:
-