First steps
Download and extraction
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.
Get from maven central
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>16.02-2.01</version>
</dependency>
<dependency>
<groupId>net.sf.sevenzipjbinding</groupId>
<artifactId>sevenzipjbinding-all-platforms</artifactId>
<version>16.02-2.01</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:
- sevenzipjbinding-all-linux
- sevenzipjbinding-all-mac
- sevenzipjbinding-all-platforms
- sevenzipjbinding-all-windows
- sevenzipjbinding-linux-amd64
- sevenzipjbinding-linux-arm
- sevenzipjbinding-linux-i386
- sevenzipjbinding-mac-i386
- sevenzipjbinding-mac-x86_64
- sevenzipjbinding-windows-amd64
- sevenzipjbinding-windows-x86
Run an example
To use the 7-Zip-JBinding library you will need two JAR-files from lib folder of the
distribution in your classpath:
- sevenzipjbinding.jar - java part of 7-Zip-JBinding.
(JavaDoc)
- sevenzip-jbinding-‹OS›.jar - native libraries for
the target operating system ‹OS›
Here is a simple test program that can be used for checking all 7-Zip-JBinding requirements:
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
7-Zip-JBinding library was initialized
shows up than 7-Zip-JBinding is working properly and is ready to use.
Working with existing archives
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:
public void openArchive(String archiveFilename)
throws SevenZipException, FileNotFoundException {
randomAccessFile = new RandomAccessFile(archiveFilename, "r");
inArchive = SevenZip.openInArchive(null,
new RandomAccessFileInStream(randomAccessFile));
}
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:
import java.io.IOException;
import java.io.RandomAccessFile;
import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.SevenZipException;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
public class PrintCountOfItems {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java PrintCountOfItems <archive-name>");
return;
}
String archiveFilename = args[0];
RandomAccessFile randomAccessFile = null;
IInArchive inArchive = null;
try {
randomAccessFile = new RandomAccessFile(archiveFilename, "r");
inArchive = SevenZip.openInArchive(null,
new RandomAccessFileInStream(randomAccessFile));
System.out.println("Count of items in archive: "
+ inArchive.getNumberOfItems());
} catch (Exception e) {
System.err.println("Error occurs: " + e);
} finally {
if (inArchive != null) {
try {
inArchive.close();
} catch (SevenZipException e) {
System.err.println("Error closing archive: " + e);
}
}
if (randomAccessFile != null) {
try {
randomAccessFile.close();
} catch (IOException e) {
System.err.println("Error closing file: " + e);
}
}
}
}
}
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:
Count of items in archive: 4
Continue with extraction code snippets to get more examples.
Creating new archives
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:
- Implement IOutCreateCallback<IOutItemCallbackZip> interface describing the archive item
- Open new out-archive
- Optionally configure the new archive (e.g. setting compression level)
- Call createArchive(...) method to create the new archive
(Note: the following example snippet lacks the proper error handling
and shouldn't be used as a template for production code. See compression/update code snippets for complete examples)
Here is the complete class:
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Date;
import net.sf.sevenzipjbinding.IOutCreateArchiveZip;
import net.sf.sevenzipjbinding.IOutCreateCallback;
import net.sf.sevenzipjbinding.IOutItemZip;
import net.sf.sevenzipjbinding.ISequentialInStream;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.SevenZipException;
import net.sf.sevenzipjbinding.impl.OutItemFactory;
import net.sf.sevenzipjbinding.impl.RandomAccessFileOutStream;
import net.sf.sevenzipjbinding.util.ByteArrayStream;
public class CompressMessage {
/**
* The callback provides information about archive items
*/
static class MyCreateCallback implements IOutCreateCallback<IOutItemZip> {
private final byte[] bytesToCompress;
private MyCreateCallback(byte[] bytesToCompress) {
this.bytesToCompress = bytesToCompress;
}
public void setOperationResult(boolean operationResultOk) {
}
public void setTotal(long total) {
}
public void setCompleted(long complete) {
}
public IOutItemZip getItemInformation(int index,
OutItemFactory<IOutItemZip> outItemFactory) {
IOutItemZip outItem = outItemFactory.createOutItem();
outItem.setDataSize((long) bytesToCompress.length);
outItem.setPropertyPath("message.txt");
outItem.setPropertyCreationTime(new Date());
return outItem;
}
public ISequentialInStream getStream(int index) {
return new ByteArrayStream(bytesToCompress, true);
}
}
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Usage: java CompressMessage <archive> <msg>");
return;
}
final byte[] bytesToCompress = args[1].getBytes();
RandomAccessFile raf = null;
IOutCreateArchiveZip outArchive = null;
try {
raf = new RandomAccessFile(args[0], "rw");
outArchive = SevenZip.openOutArchiveZip();
outArchive.setLevel(5);
outArchive.createArchive(new RandomAccessFileOutStream(raf), 1,
new MyCreateCallback(bytesToCompress));
System.out.println("Compression operation succeeded");
} catch (SevenZipException e) {
System.err.println("7-Zip-JBinding-Error:");
e.printStackTraceExtended();
} catch (Exception e) {
System.err.println("Error occurs: " + e);
} finally {
if (outArchive != null) {
try {
outArchive.close();
} catch (IOException e) {
System.err.println("Error closing archive: " + e);
}
}
if (raf != null) {
try {
raf.close();
} catch (IOException e) {
System.err.println("Error closing file: " + e);
}
}
}
}
}
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
Compression operation succeeded
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.
|