The most simple way to get 7-Zip-JBinding to work is to download a pre-compiled binary package for your operating system here. All provided packages are Zip-compressed and you will need an extraction utility to expand them. For windows users securing a copy of 7-Zip is highly recommended. Linux and Mac OS X users may use the corresponding command line tool p7Zip or build-in Zip extractors.
There are two dependencies need to be added to the pom.xml file. 7-Zip-JBinding library jar and the platform dependend (native) jar.
Note: 7-Zip-JBinding library doesn't declare maven depenency to any platform jars. So you always need two <dependency> tags.
The simplest way to use 7-Zip-JBinding is to use the platform autodetection with the "all-platform" native package. (The down side is the relatively big size of the "all-platform" jar though):
<dependency> <groupId>net.sf.sevenzipjbinding</groupId> <artifactId>sevenzipjbinding</artifactId> <version>##INCLUDE_VERSION</version> </dependency> <dependency> <groupId>net.sf.sevenzipjbinding</groupId> <artifactId>sevenzipjbinding-all-platforms</artifactId> <version>##INCLUDE_VERSION</version> </dependency>
Alternatively the proper platform jar artifact can be manually selected or selected using platform dependend maven profiles. Here is a list of all available platform artifactIds:
To use the 7-Zip-JBinding library you will need two JAR-files from lib folder of the distribution in your classpath:
To run this program correctly you will need to add two 7-Zip-JBinding JAR-files to the classpath. On Linux it could be done as following:
$ java -cp ‹path-to-lib›/sevenzipjbinding.jar: \ ‹path-to-lib›/sevenzipjbinding-Linux-i686.jar:. \ SevenZipJBindingInitCheck
Windows users can do the same with (written in a single line)
C:\Test> java -cp ‹path-to-lib›\sevenzipjbinding.jar; \ ‹path-to-lib›\sevenzipjbinding-Windows-x86.jar;. \ SevenZipJBindingInitCheck
If the message
##INCLUDE_OUTPUT(SevenZipJBindingInitCheck)shows up than 7-Zip-JBinding is working properly and is ready to use.
Before you can do anything with an archive, you have to open it. In order to do this you will need to call one of the corresponding static methods SevenZip.openInArchive(...). The single mandatory parameter is "inStream" - an implementation of the IInStream interface.
In case you want to open an archive file from the file system 7-Zip-JBinding provides a standard implementation of the IInStream interface: RandomAccessFileInStream. As the name says, this takes an instance of RandomAccessFile and uses it to provide an implementation of the IInStream.
Here is a simple example of how to open an archive from the file system:
##INCLUDE_SNIPPET(SimpleOpen)The opened archive can now be used for browsing or extraction operations. The last method called on archive should always be IInArchive.close(). It will close the archive and free system resources. The last, but not least, step is to close the random access file 'randomAccessFile' using close() method.
Here is a complete program to print count of items in an archive of any supported format: ##INCLUDE_SNIPPET(PrintCountOfItems)
If you run this program with
C:\Test> java -cp ‹path-to-lib›\sevenzipjbinding.jar; \ ‹path-to-lib›\sevenzipjbinding-Windows-x86.jar;. \ PrintCountOfItems my-test-archive.zip
you should get something like this:
##INCLUDE_OUTPUT(PrintCountOfItems)Continue with extraction code snippets to get more examples.
In order to create a new archive you need to call the corresponding open method of the SevenZip class first. Here you should choose one of the archive format specific methods (like SevenZip.openOutArchiveZip(...) for Zip) or use the archive format independent method SevenZip.openOutArchive(...). Lets use Zip-specific method and see how a very simple Zip archive with a single item (archived file) containing a text message can be created.
There are 4 following steps involved:
If you run this program with
C:\Test> java -cp ‹path-to-lib›\sevenzipjbinding.jar; \ ‹path-to-lib›\sevenzipjbinding-Windows-x86.jar;. \ CompressMessage compressed_message.zip HelloWorld
you will get the output
##INCLUDE_OUTPUT(CompressMessage)Also the archive compressed_message.zip should be created. It contains single compressed file "message.txt" with the message "HelloWorld".
C:\Test> 7z l compressed_message.zip Listing archive: compressed_message.zip -- Path = compressed_message.zip Type = zip Physical Size = 135 Date Time Attr Size Compressed Name ------------------- ----- ------------ ------------ ------------------------ 2015-09-09 08:56:42 ..... 15 15 message.txt ------------------- ----- ------------ ------------ ------------------------ 15 15 1 files, 0 folders
C:\Test> 7z x compressed_message.zip Processing archive: compressed_message.zip Extracting message.txt Everything is Ok Size: 15 Compressed: 135
C:\Test> type message.txt HelloWorld
Continue with compression/update code snippets to get more examples.