Introduction

Aspose.ZIP for Java API lets work with creating and managing archives in your applications without the need of any other 3rd party applications. Its detailed implementation lets manage ZIP archives easily.

This article descibes manipulation with zip format only. Aspose.Zip support a lot of archive formats, they are listed here.

Basic Concepts of the API

Aspose.ZIP API conceptualizes the functionality of each item in an archive as Entry. Entry(ies) can be added, updated as well as removed from an archive. The API makes it simplified to work with an archive and its entries. This section gives an idea about different classes and methods exposed by the API and their usage.

Working with Archives

Creating Archive

An archive can be created using the Archive class exposed by the API. A basic archive can be created using this class as shown in the following code sample.

1Archive archive = new Archive();

Such archive is prepared for compression.

Creating Archive with Custom Compression

Archives can be created with additional settings specifying custom Compression techniques.

1Archive archive = new Archive(
2    new ArchiveEntrySettings(
3        new CompressionSettings(CompressionMethod.Store), new AesEcryptionSettings("p@s$", EncryptionMethod.AES256)));

Saving Archives

Archives can be saved to the file system on the disc as well as to streams.

1archive.save(outputStream, saveOptions)
2archive.save(filePath, saveOptions)

Archive Extraction

Archives can be extracted by passing the source stream or path to the Archive extracting constructor.

1FileInputStream zipFileStream = new FileInputStream("archive.zip");
2Archive archive = new Archive(zipFileStream);
or
1Archive archive = new Archive("myarchive.zip");

Extracting Encrypted Archives

Password-protected archives can be extracted by specifying the ArchiveLoadOptions

1ArchiveLoadOptions options = new ArchiveLoadOptions();
2options.setDecryptionPassword("p@s$");
3Archive archive = new Archive(sourceStream, options);
or
1ArchiveLoadOptions options = new ArchiveLoadOptions();
2options.setDecryptionPassword("p@s$");
3Archive archive = new Archive("myarchive.zip", options);

Working with Archive Entries

Adding Entries to Archive

Entries can be added to an archive using the createEntry method of Archive. The overloads offered by this method allow you to create entries in an archive from file path or stream along with specification Archive Entry settings. Files from a directory can also be added to an archive specifying the recursive search through the directory.

 1// Creates single entry with given name, data source, optional compression and encryption settings for the entry.
 2createEntry(string name, InputStream source, ArchiveEntrySettings newEntriesSettings)
 3
 4// Creates single entry with given name and file source, optional compression and encryption settings for the entry.
 5createEntry(string name, string path, ArchiveEntrySettings newEntriesSettings)
 6
 7// Creates single entry with given name, file source, optional compression and encryption settings for the entry.
 8// Keeps attributes of NTFS file.
 9createEntry(string name, File file, ArchiveEntrySettings newEntriesSettings)
10
11// Creates single entry with given name, data source, compression and encryption settings for the entry.
12// Keeps attributes of NTFS file and respects optional parameters.
13createEntry(string name, InputStream source, ArchiveEntrySettings newEntriesSettings, File file)
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.