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 (download link).
All provided packages are Zip-compressed and you will need an extraction utility to expand them. For windows users is
securing a copy of 7-Zip is highly recommended. Linux and Mac OS X
users may use the corresponding command line tool p7Zip.
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:
- sevenzip-jbinding.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›/sevenzip-jbinding.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›\sevenzip-jbinding.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 for use.
Opening an archive
Before you can do any operation on an archive file, you have to open it. In order to do this
you will need to call one of the corresponding static methods
SevenZip.openInArchive(...). The only
mandatory parameter is "inStream" - an instance of the
IInStream interface.
In many cases
the task is to open an archive file from the file system. 7-Zip-JBinding has a standard implementation
of IInStream for this purpose:
RandomAccessFileInStream.
As the name says, this takes an instance of RandomAccessFile and turns it into an instance of
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 exploring and extraction operations. The last method called on archive should always be
ISevenZipInArchive.close().
This closes the archive and frees system resources. The last, but not least, step is to close the random access file
'randomAccessFile' using close() method of the RandomAccessFile class.
Here is a complete program to print count of items in the archive:
import java.io.IOException;
import java.io.RandomAccessFile;
import net.sf.sevenzipjbinding.ISevenZipInArchive;
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;
ISevenZipInArchive 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);
System.exit(1);
} 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›\sevenzip-jbinding.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 Code snippets to get more examples.
|