Archive
Inheritance: java.lang.Object
All Implemented Interfaces: com.aspose.zip.IArchive, java.lang.AutoCloseable
public class Archive implements IArchive, AutoCloseable
This class represents zip archive file. Use it to compose, extract, or update zip archives.
Constructors
Constructor | Description |
---|---|
Archive() | Initializes a new instance of the Archive class with optional settings for its entries. |
Archive(ArchiveEntrySettings newEntrySettings) | Initializes a new instance of the Archive class with optional settings for its entries. |
Archive(InputStream sourceStream) | Initializes a new instance of the Archive class and composes entries list can be extracted from the archive. |
Archive(InputStream sourceStream, ArchiveLoadOptions loadOptions) | Initializes a new instance of the Archive class and composes entries list can be extracted from the archive. |
Archive(InputStream sourceStream, ArchiveLoadOptions loadOptions, ArchiveEntrySettings newEntrySettings) | Initializes a new instance of the Archive class and composes entries list can be extracted from the archive. |
Archive(String path) | Initializes a new instance of the Archive class and composes entries list can be extracted from the archive. |
Archive(String path, ArchiveLoadOptions loadOptions) | Initializes a new instance of the Archive class and composes entries list can be extracted from the archive. |
Archive(String path, ArchiveLoadOptions loadOptions, ArchiveEntrySettings newEntrySettings) | Initializes a new instance of the Archive class and composes entries list can be extracted from the archive. |
Archive(String mainSegment, String[] segmentsInOrder) | Initializes a new instance of the Archive class from multi-volume zip archive and composes entries list can be extracted from the archive. |
Archive(String mainSegment, String[] segmentsInOrder, ArchiveLoadOptions loadOptions) | Initializes a new instance of the Archive class from multi-volume zip archive and composes entries list can be extracted from the archive. |
Methods
Archive()
public Archive()
Initializes a new instance of the Archive class with optional settings for its entries.
The following example shows how to compress a single file with default settings.
try (FileOutputStream zipFile = new FileOutputStream("archive.zip")) {
try (Archive archive = new Archive()) {
archive.createEntry("data.bin", "file.dat");
archive.save(zipFile);
}
} catch (IOException ex) {
}
Archive(ArchiveEntrySettings newEntrySettings)
public Archive(ArchiveEntrySettings newEntrySettings)
Initializes a new instance of the Archive class with optional settings for its entries.
The following example shows how to compress a single file with default settings.
try (FileOutputStream zipFile = new FileOutputStream("archive.zip")) {
try (Archive archive = new Archive()) {
archive.createEntry("data.bin", "file.dat");
archive.save(zipFile);
}
} catch (IOException ex) {
}
Parameters:
Parameter | Type | Description |
---|---|---|
newEntrySettings | ArchiveEntrySettings | Compression and encryption settings used for newly added ArchiveEntry items. If not specified, most common Deflate compression without encryption would be used. |
Archive(InputStream sourceStream)
public Archive(InputStream sourceStream)
Initializes a new instance of the Archive class and composes entries list can be extracted from the archive.
The following example extract an encrypted archive, then decompress first entry to a ByteArrayOutputStream
.
try (FileInputStream fs = new FileInputStream("encrypted.zip")) {
ByteArrayOutputStream extracted = new ByteArrayOutputStream();
ArchiveLoadOptions options = new ArchiveLoadOptions();
options.setDecryptionPassword("p@s$");
try (Archive archive = new Archive(fs, options)) {
try (InputStream decompressed = archive.getEntries().get(0).open()) {
byte[] b = new byte[8192];
int bytesRead;
while (0 < (bytesRead = decompressed.read(b, 0, b.length)))
extracted.write(b, 0, bytesRead);
}
}
} catch (IOException ex) {
}
This constructor does not decompress any entry. See ArchiveEntry.open() method for decompressing.
Parameters:
Parameter | Type | Description |
---|---|---|
sourceStream | java.io.InputStream | The source of the archive. |
Archive(InputStream sourceStream, ArchiveLoadOptions loadOptions)
public Archive(InputStream sourceStream, ArchiveLoadOptions loadOptions)
Initializes a new instance of the Archive class and composes entries list can be extracted from the archive.
The following example extract an encrypted archive, then decompress first entry to a ByteArrayOutputStream
.
try (FileInputStream fs = new FileInputStream("encrypted.zip")) {
ByteArrayOutputStream extracted = new ByteArrayOutputStream();
ArchiveLoadOptions options = new ArchiveLoadOptions();
options.setDecryptionPassword("p@s$");
try (Archive archive = new Archive(fs, options)) {
try (InputStream decompressed = archive.getEntries().get(0).open()) {
byte[] b = new byte[8192];
int bytesRead;
while (0 < (bytesRead = decompressed.read(b, 0, b.length)))
extracted.write(b, 0, bytesRead);
}
}
} catch (IOException ex) {
}
This constructor does not decompress any entry. See ArchiveEntry.open() method for decompressing.
Parameters:
Parameter | Type | Description |
---|---|---|
sourceStream | java.io.InputStream | The source of the archive. |
loadOptions | ArchiveLoadOptions | Options to load existing archive with. |
Archive(InputStream sourceStream, ArchiveLoadOptions loadOptions, ArchiveEntrySettings newEntrySettings)
public Archive(InputStream sourceStream, ArchiveLoadOptions loadOptions, ArchiveEntrySettings newEntrySettings)
Initializes a new instance of the Archive class and composes entries list can be extracted from the archive.
The following example extract an encrypted archive, then decompress first entry to a ByteArrayOutputStream
.
try (FileInputStream fs = new FileInputStream("encrypted.zip")) {
ByteArrayOutputStream extracted = new ByteArrayOutputStream();
ArchiveLoadOptions options = new ArchiveLoadOptions();
options.setDecryptionPassword("p@s$");
try (Archive archive = new Archive(fs, options)) {
try (InputStream decompressed = archive.getEntries().get(0).open()) {
byte[] b = new byte[8192];
int bytesRead;
while (0 < (bytesRead = decompressed.read(b, 0, b.length)))
extracted.write(b, 0, bytesRead);
}
}
} catch (IOException ex) {
}
This constructor does not decompress any entry. See ArchiveEntry.open() method for decompressing.
Parameters:
Parameter | Type | Description |
---|---|---|
sourceStream | java.io.InputStream | The source of the archive. |
loadOptions | ArchiveLoadOptions | Options to load existing archive with. |
newEntrySettings | ArchiveEntrySettings | Compression and encryption settings used for newly added ArchiveEntry items. If not specified, most common Deflate compression without encryption would be used. |
Archive(String path)
public Archive(String path)
Initializes a new instance of the Archive class and composes entries list can be extracted from the archive.
The following example extract an encrypted archive, then decompress first entry to a ByteArrayOutputStream
.
ByteArrayOutputStream extracted = new ByteArrayOutputStream();
ArchiveLoadOptions options = new ArchiveLoadOptions();
options.setDecryptionPassword("p@s$");
try (Archive archive = new Archive("encrypted.zip", options)) {
try (InputStream decompressed = archive.getEntries().get(0).open()) {
byte[] b = new byte[8192];
int bytesRead;
while (0 < (bytesRead = decompressed.read(b, 0, b.length)))
extracted.write(b, 0, bytesRead);
} catch (IOException ex) {
}
}
This constructor does not decompress any entry. See ArchiveEntry.open() method for decompressing.
Parameters:
Parameter | Type | Description |
---|---|---|
path | java.lang.String | The fully qualified or the relative path to the archive file. |
Archive(String path, ArchiveLoadOptions loadOptions)
public Archive(String path, ArchiveLoadOptions loadOptions)
Initializes a new instance of the Archive class and composes entries list can be extracted from the archive.
The following example extract an encrypted archive, then decompress first entry to a ByteArrayOutputStream
.
ByteArrayOutputStream extracted = new ByteArrayOutputStream();
ArchiveLoadOptions options = new ArchiveLoadOptions();
options.setDecryptionPassword("p@s$");
try (Archive archive = new Archive("encrypted.zip", options)) {
try (InputStream decompressed = archive.getEntries().get(0).open()) {
byte[] b = new byte[8192];
int bytesRead;
while (0 < (bytesRead = decompressed.read(b, 0, b.length)))
extracted.write(b, 0, bytesRead);
} catch (IOException ex) {
}
}
This constructor does not decompress any entry. See ArchiveEntry.open() method for decompressing.
Parameters:
Parameter | Type | Description |
---|---|---|
path | java.lang.String | The fully qualified or the relative path to the archive file. |
loadOptions | ArchiveLoadOptions | Options to load existing archive with. |
Archive(String path, ArchiveLoadOptions loadOptions, ArchiveEntrySettings newEntrySettings)
public Archive(String path, ArchiveLoadOptions loadOptions, ArchiveEntrySettings newEntrySettings)
Initializes a new instance of the Archive class and composes entries list can be extracted from the archive.
The following example extract an encrypted archive, then decompress first entry to a ByteArrayOutputStream
.
ByteArrayOutputStream extracted = new ByteArrayOutputStream();
ArchiveLoadOptions options = new ArchiveLoadOptions();
options.setDecryptionPassword("p@s$");
try (Archive archive = new Archive("encrypted.zip", options)) {
try (InputStream decompressed = archive.getEntries().get(0).open()) {
byte[] b = new byte[8192];
int bytesRead;
while (0 < (bytesRead = decompressed.read(b, 0, b.length)))
extracted.write(b, 0, bytesRead);
} catch (IOException ex) {
}
}
This constructor does not decompress any entry. See ArchiveEntry.open() method for decompressing.
Parameters:
Parameter | Type | Description |
---|---|---|
path | java.lang.String | The fully qualified or the relative path to the archive file. |
loadOptions | ArchiveLoadOptions | Options to load existing archive with. |
newEntrySettings | ArchiveEntrySettings | Compression and encryption settings used for newly added ArchiveEntry items. If not specified, most common Deflate compression without encryption would be used. |
Archive(String mainSegment, String[] segmentsInOrder)
public Archive(String mainSegment, String[] segmentsInOrder)
Initializes a new instance of the Archive class from multi-volume zip archive and composes entries list can be extracted from the archive.
try (Archive a = new Archive("archive.zip", new String[] { "archive.z01", "archive.z02" })) {
a.extractToDirectory("destination");
}
Parameters:
Parameter | Type | Description |
---|---|---|
mainSegment | java.lang.String | Path to the last segment of multi-volume archive with the central directory. |
Usually this segment has *.zip extension and smaller than others. | | segmentsInOrder | java.lang.String[] | Paths to each segment but the last of multi-volume zip archive respecting order.
Usually they named filename.z01, filename.z02, …, filename.z(n-1). |
Archive(String mainSegment, String[] segmentsInOrder, ArchiveLoadOptions loadOptions)
public Archive(String mainSegment, String[] segmentsInOrder, ArchiveLoadOptions loadOptions)
Initializes a new instance of the Archive class from multi-volume zip archive and composes entries list can be extracted from the archive.
try (Archive a = new Archive("archive.zip", new String[] { "archive.z01", "archive.z02" })) {
a.extractToDirectory("destination");
}
Parameters:
Parameter | Type | Description |
---|---|---|
mainSegment | java.lang.String | Path to the last segment of multi-volume archive with the central directory. |
Usually this segment has *.zip extension and smaller than others. | | segmentsInOrder | java.lang.String[] | Paths to each segment but the last of multi-volume zip archive respecting order.
Usually they named filename.z01, filename.z02, …, filename.z(n-1). | | loadOptions | ArchiveLoadOptions | Options to load existing archive with. |
close()
public void close()
createEntries(File directory)
public final Archive createEntries(File directory)
Adds to the archive all files and directories recursively in the directory given.
try (Archive archive = new Archive()) {
java.io.File folder = new java.io.File("C:\\folder");
archive.createEntries(folder);
archive.save("folder.zip");
}
Parameters:
Parameter | Type | Description |
---|---|---|
directory | java.io.File | Directory to compress. |
Returns: Archive - The archive with entries composed.
createEntries(File directory, boolean includeRootDirectory)
public final Archive createEntries(File directory, boolean includeRootDirectory)
Adds to the archive all files and directories recursively in the directory given.
try (Archive archive = new Archive()) {
java.io.File folder = new java.io.File("C:\\folder");
archive.createEntries(folder);
archive.save("folder.zip");
}
Parameters:
Parameter | Type | Description |
---|---|---|
directory | java.io.File | Directory to compress. |
includeRootDirectory | boolean | Indicates whether to include the root directory itself or not. |
Returns: Archive - The archive with entries composed.
createEntries(String sourceDirectory)
public final Archive createEntries(String sourceDirectory)
Adds to the archive all files and directories recursively in the directory given.
try (Archive archive = new Archive()) {
archive.createEntries("C:\\folder");
archive.save("folder.zip");
}
Parameters:
Parameter | Type | Description |
---|---|---|
sourceDirectory | java.lang.String | Directory to compress. |
Returns: Archive - The archive with entries composed.
createEntries(String sourceDirectory, boolean includeRootDirectory)
public final Archive createEntries(String sourceDirectory, boolean includeRootDirectory)
Adds to the archive all files and directories recursively in the directory given.
try (Archive archive = new Archive()) {
archive.createEntries("C:\\folder");
archive.save("folder.zip");
}
Parameters:
Parameter | Type | Description |
---|---|---|
sourceDirectory | java.lang.String | Directory to compress. |
includeRootDirectory | boolean | Indicates whether to include the root directory itself or not. |
Returns: Archive - The archive with entries composed.
createEntry(String name, File file)
public final ArchiveEntry createEntry(String name, File file)
Create single entry within the archive.
Compose archive with entries encrypted with different encryption methods and passwords each.
try (FileOutputStream zipFile = new FileOutputStream("archive.zip")) {
java.io.File fi1 = new java.io.File("data1.bin");
java.io.File fi2 = new java.io.File("data2.bin");
java.io.File fi3 = new java.io.File("data3.bin");
try (Archive archive = new Archive()) {
archive.createEntry("entry1.bin", fi1, false, new ArchiveEntrySettings(new DeflateCompressionSettings(), new TraditionalEncryptionSettings("pass1")));
archive.createEntry("entry2.bin", fi2, false, new ArchiveEntrySettings(new DeflateCompressionSettings(), new AesEncryptionSettings("pass2", EncryptionMethod.AES128)));
archive.createEntry("entry3.bin", fi3, false, new ArchiveEntrySettings(new DeflateCompressionSettings(), new AesEncryptionSettings("pass3", EncryptionMethod.AES256)));
archive.save(zipFile);
}
} catch (IOException ignored) {
}
The entry name is solely set within name
parameter. The file name provided in file
parameter does not affect the entry name.
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | The name of the entry. |
file | java.io.File | The metadata of file to be compressed. |
Returns: ArchiveEntry - Zip entry instance.
createEntry(String name, File file, boolean openImmediately)
public final ArchiveEntry createEntry(String name, File file, boolean openImmediately)
Create single entry within the archive.
Compose archive with entries encrypted with different encryption methods and passwords each.
try (FileOutputStream zipFile = new FileOutputStream("archive.zip")) {
java.io.File fi1 = new java.io.File("data1.bin");
java.io.File fi2 = new java.io.File("data2.bin");
java.io.File fi3 = new java.io.File("data3.bin");
try (Archive archive = new Archive()) {
archive.createEntry("entry1.bin", fi1, false, new ArchiveEntrySettings(new DeflateCompressionSettings(), new TraditionalEncryptionSettings("pass1")));
archive.createEntry("entry2.bin", fi2, false, new ArchiveEntrySettings(new DeflateCompressionSettings(), new AesEncryptionSettings("pass2", EncryptionMethod.AES128)));
archive.createEntry("entry3.bin", fi3, false, new ArchiveEntrySettings(new DeflateCompressionSettings(), new AesEncryptionSettings("pass3", EncryptionMethod.AES256)));
archive.save(zipFile);
}
} catch (IOException ignored) {
}
The entry name is solely set within name
parameter. The file name provided in file
parameter does not affect the entry name.
If the file is opened immediately with openImmediately
parameter it becomes blocked until archive is saved.
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | The name of the entry. |
file | java.io.File | The metadata of file to be compressed. |
openImmediately | boolean | True if open the file immediately, otherwise open the file on archive saving. |
Returns: ArchiveEntry - Zip entry instance.
createEntry(String name, File file, boolean openImmediately, ArchiveEntrySettings newEntrySettings)
public final ArchiveEntry createEntry(String name, File file, boolean openImmediately, ArchiveEntrySettings newEntrySettings)
Create single entry within the archive.
Compose archive with entries encrypted with different encryption methods and passwords each.
try (FileOutputStream zipFile = new FileOutputStream("archive.zip")) {
java.io.File fi1 = new java.io.File("data1.bin");
java.io.File fi2 = new java.io.File("data2.bin");
java.io.File fi3 = new java.io.File("data3.bin");
try (Archive archive = new Archive()) {
archive.createEntry("entry1.bin", fi1, false, new ArchiveEntrySettings(new DeflateCompressionSettings(), new TraditionalEncryptionSettings("pass1")));
archive.createEntry("entry2.bin", fi2, false, new ArchiveEntrySettings(new DeflateCompressionSettings(), new AesEncryptionSettings("pass2", EncryptionMethod.AES128)));
archive.createEntry("entry3.bin", fi3, false, new ArchiveEntrySettings(new DeflateCompressionSettings(), new AesEncryptionSettings("pass3", EncryptionMethod.AES256)));
archive.save(zipFile);
}
} catch (IOException ignored) {
}
The entry name is solely set within name
parameter. The file name provided in file
parameter does not affect the entry name.
If the file is opened immediately with openImmediately
parameter it becomes blocked until archive is saved.
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | The name of the entry. |
file | java.io.File | The metadata of file to be compressed. |
openImmediately | boolean | True if open the file immediately, otherwise open the file on archive saving. |
newEntrySettings | ArchiveEntrySettings | Compression and encryption settings used for added ArchiveEntry item. |
Returns: ArchiveEntry - Zip entry instance.
createEntry(String name, InputStream source)
public final ArchiveEntry createEntry(String name, InputStream source)
Create single entry within the archive.
try (Archive archive = new Archive(new ArchiveEntrySettings(null, new AesEncryptionSettings("p@s$", EncryptionMethod.AES256)))) {
archive.createEntry("data.bin", new ByteArrayInputStream(new byte[] {
0x00,
(byte) 0xFF
}));
archive.save("archive.zip");
}
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | The name of the entry. |
source | java.io.InputStream | The input stream for the entry. |
Returns: ArchiveEntry - Zip entry instance.
createEntry(String name, InputStream source, ArchiveEntrySettings newEntrySettings)
public final ArchiveEntry createEntry(String name, InputStream source, ArchiveEntrySettings newEntrySettings)
Create single entry within the archive.
try (Archive archive = new Archive(new ArchiveEntrySettings(null, new AesEncryptionSettings("p@s$", EncryptionMethod.AES256)))) {
archive.createEntry("data.bin", new ByteArrayInputStream(new byte[] {
0x00,
(byte) 0xFF
}));
archive.save("archive.zip");
}
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | The name of the entry. |
source | java.io.InputStream | The input stream for the entry. |
newEntrySettings | ArchiveEntrySettings | Compression and encryption settings used for added ArchiveEntry item. |
Returns: ArchiveEntry - Zip entry instance.
createEntry(String name, InputStream source, ArchiveEntrySettings newEntrySettings, File file)
public final ArchiveEntry createEntry(String name, InputStream source, ArchiveEntrySettings newEntrySettings, File file)
Create single entry within the archive.
Compose archive with encrypted entry.
try (FileOutputStream zipFile = new FileOutputStream("archive.zip")) {
try (Archive archive = new Archive()) {
archive.createEntry("entry1.bin", new ByteArrayInputStream(new byte[] {
0x00,
(byte) 0xFF
}), new ArchiveEntrySettings(new DeflateCompressionSettings(), new TraditionalEncryptionSettings("pass1")), new java.io.File("data1.bin"));
archive.save(zipFile);
}
} catch (IOException ignored) {
}
The entry name is solely set within name
parameter. The file name provided in file
parameter does not affect the entry name.
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | The name of the entry. |
source | java.io.InputStream | The input stream for the entry. |
newEntrySettings | ArchiveEntrySettings | Compression and encryption settings used for added ArchiveEntry item. |
file | java.io.File | The metadata of file or folder to be compressed. |
Returns: ArchiveEntry - Zip entry instance.
createEntry(String name, String path)
public final ArchiveEntry createEntry(String name, String path)
Create single entry within the archive.
try (FileOutputStream zipFile = new FileOutputStream("archive.zip")) {
try (Archive archive = new Archive()) {
archive.createEntry("data.bin", "file.dat");
archive.save(zipFile);
}
} catch (IOException ex) {
}
The entry name is solely set within name
parameter. The file name provided in path
parameter does not affect the entry name.
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | The name of the entry. |
path | java.lang.String | The fully qualified name of the new file, or the relative file name to be compressed. |
Returns: ArchiveEntry - Zip entry instance.
createEntry(String name, String path, boolean openImmediately)
public final ArchiveEntry createEntry(String name, String path, boolean openImmediately)
Create single entry within the archive.
try (FileOutputStream zipFile = new FileOutputStream("archive.zip")) {
try (Archive archive = new Archive()) {
archive.createEntry("data.bin", "file.dat");
archive.save(zipFile);
}
} catch (IOException ex) {
}
The entry name is solely set within name
parameter. The file name provided in path
parameter does not affect the entry name.
If the file is opened immediately with openImmediately
parameter it becomes blocked until archive is saved.
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | The name of the entry. |
path | java.lang.String | The fully qualified name of the new file, or the relative file name to be compressed. |
openImmediately | boolean | True if open the file immediately, otherwise open the file on archive saving. |
Returns: ArchiveEntry - Zip entry instance.
createEntry(String name, String path, boolean openImmediately, ArchiveEntrySettings newEntrySettings)
public final ArchiveEntry createEntry(String name, String path, boolean openImmediately, ArchiveEntrySettings newEntrySettings)
Create single entry within the archive.
try (FileOutputStream zipFile = new FileOutputStream("archive.zip")) {
try (Archive archive = new Archive()) {
archive.createEntry("data.bin", "file.dat");
archive.save(zipFile);
}
} catch (IOException ex) {
}
The entry name is solely set within name
parameter. The file name provided in path
parameter does not affect the entry name.
If the file is opened immediately with openImmediately
parameter it becomes blocked until archive is saved.
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | The name of the entry. |
path | java.lang.String | The fully qualified name of the new file, or the relative file name to be compressed. |
openImmediately | boolean | True if open the file immediately, otherwise open the file on archive saving. |
newEntrySettings | ArchiveEntrySettings | Compression and encryption settings used for added ArchiveEntry item. |
Returns: ArchiveEntry - Zip entry instance.
createEntry(String name, Supplier<InputStream> streamProvider)
public final ArchiveEntry createEntry(String name, Supplier<InputStream> streamProvider)
Create single entry within the archive.
Compose archive with encrypted entry.
Supplier<InputStream> provider = new Supplier<InputStream>() {
public InputStream get() {
return new ByteArrayInputStream(new byte[] {(byte) 0xFF, 0x00});
}
};
try (FileOutputStream zipFile = new FileOutputStream("archive.zip")) {
try (Archive archive = new Archive()) {
archive.createEntry("entry1.bin", provider, new ArchiveEntrySettings(new DeflateCompressionSettings(), new TraditionalEncryptionSettings("pass1")));
archive.save(zipFile);
}
} catch (IOException ex) {
System.out.println(ex);
}
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | the name of the entry |
streamProvider | java.util.function.Supplier<java.io.InputStream> | the method providing input stream for the entry |
Returns: ArchiveEntry - zip entry instance
createEntry(String name, Supplier<InputStream> streamProvider, ArchiveEntrySettings newEntrySettings)
public final ArchiveEntry createEntry(String name, Supplier<InputStream> streamProvider, ArchiveEntrySettings newEntrySettings)
Create single entry within the archive.
Compose archive with encrypted entry.
Supplier<InputStream> provider = new Supplier<InputStream>() {
public InputStream get() {
return new ByteArrayInputStream(new byte[] {(byte) 0xFF, 0x00});
}
};
try (FileOutputStream zipFile = new FileOutputStream("archive.zip")) {
try (Archive archive = new Archive()) {
archive.createEntry("entry1.bin", provider, new ArchiveEntrySettings(new DeflateCompressionSettings(), new TraditionalEncryptionSettings("pass1")));
archive.save(zipFile);
}
} catch (IOException ex) {
System.out.println(ex);
}
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | the name of the entry |
streamProvider | java.util.function.Supplier<java.io.InputStream> | the method providing input stream for the entry |
newEntrySettings | ArchiveEntrySettings | compression and encryption settings used for added ArchiveEntry item |
Returns: ArchiveEntry - zip entry instance
deleteEntry(ArchiveEntry entry)
public final Archive deleteEntry(ArchiveEntry entry)
Removes the first occurrence of a specific entry from the entries list.
Here is how you can remove all entries except the last one:
try (Archive archive = new Archive("archive.zip")) {
while (archive.getEntries().size() > 1)
archive.deleteEntry(archive.getEntries().get(0));
archive.save("last_entry.zip");
}
Parameters:
Parameter | Type | Description |
---|---|---|
entry | ArchiveEntry | The entry to remove from the entries list. |
Returns: Archive - The archive with the entry deleted.
deleteEntry(int entryIndex)
public final Archive deleteEntry(int entryIndex)
Removes the entry from the entries list by index.
try (Archive archive = new Archive("two_files.zip")) {
archive.deleteEntry(0);
archive.save("single_file.zip");
}
Parameters:
Parameter | Type | Description |
---|---|---|
entryIndex | int | The zero-based index of the entry to remove. |
Returns: Archive - The archive with the entry deleted.
extractToDirectory(String destinationDirectory)
public final void extractToDirectory(String destinationDirectory)
Extracts all the files in the archive to the directory provided.
try (Archive archive = new Archive("archive.zip")) {
archive.extractToDirectory("C:\\extracted");
}
If the directory does not exist, it will be created.
Parameters:
Parameter | Type | Description |
---|---|---|
destinationDirectory | java.lang.String | The path to the directory to place the extracted files in. |
getEntries()
public final List<ArchiveEntry> getEntries()
Gets entries of ArchiveEntry type constituting the archive.
Returns: java.util.List<com.aspose.zip.ArchiveEntry> - entries of ArchiveEntry type constituting the archive.
getFileEntries()
public final Iterable<IArchiveFileEntry> getFileEntries()
Gets entries of IArchiveFileEntry type constituting the archive.
Returns: java.lang.Iterable<com.aspose.zip.IArchiveFileEntry> - entries of IArchiveFileEntry type constituting the archive.
getNewEntrySettings()
public final ArchiveEntrySettings getNewEntrySettings()
Compression and encryption settings used for newly added ArchiveEntry items.
Returns: ArchiveEntrySettings - compression and encryption settings used for newly added ArchiveEntry items.
save(OutputStream outputStream)
public final void save(OutputStream outputStream)
Saves archive to the stream provided.
try (FileOutputStream zipFile = new FileOutputStream("archive.zip")) {
try (Archive archive = new Archive()) {
archive.createEntry("entry.bin", "data.bin");
archive.save(zipFile);
}
} catch (IOException ex) {
}
outputStream
must be writable.
Parameters:
Parameter | Type | Description |
---|---|---|
outputStream | java.io.OutputStream | Destination stream. |
save(OutputStream outputStream, ArchiveSaveOptions saveOptions)
public final void save(OutputStream outputStream, ArchiveSaveOptions saveOptions)
Saves archive to the stream provided.
try (FileOutputStream zipFile = new FileOutputStream("archive.zip")) {
try (Archive archive = new Archive()) {
archive.createEntry("entry.bin", "data.bin");
archive.save(zipFile);
}
} catch (IOException ex) {
}
outputStream
must be writable.
Parameters:
Parameter | Type | Description |
---|---|---|
outputStream | java.io.OutputStream | Destination stream. |
saveOptions | ArchiveSaveOptions | Options for archive saving. |
save(String destinationFileName)
public final void save(String destinationFileName)
Saves archive to destination file provided.
try (Archive archive = new Archive()) {
archive.createEntry("entry.bin", "data.bin");
ArchiveSaveOptions options = new ArchiveSaveOptions();
options.setEncoding(StandardCharsets.US_ASCII);
archive.save("archive.zip", options);
}
It is possible to save an archive to the same path as it was loaded from. However, this is not recommended because this approach uses copying to temporary file.
Parameters:
Parameter | Type | Description |
---|---|---|
destinationFileName | java.lang.String | The path of the archive to be created. If the specified file name points to an existing file, it will be overwritten. |
save(String destinationFileName, ArchiveSaveOptions saveOptions)
public final void save(String destinationFileName, ArchiveSaveOptions saveOptions)
Saves archive to destination file provided.
try (Archive archive = new Archive()) {
archive.createEntry("entry.bin", "data.bin");
ArchiveSaveOptions options = new ArchiveSaveOptions();
options.setEncoding(StandardCharsets.US_ASCII);
archive.save("archive.zip", options);
}
It is possible to save an archive to the same path as it was loaded from. However, this is not recommended because this approach uses copying to temporary file.
Parameters:
Parameter | Type | Description |
---|---|---|
destinationFileName | java.lang.String | The path of the archive to be created. If the specified file name points to an existing file, it will be overwritten. |
saveOptions | ArchiveSaveOptions | Options for archive saving. |
saveSplit(String destinationDirectory, SplitArchiveSaveOptions options)
public final void saveSplit(String destinationDirectory, SplitArchiveSaveOptions options)
Saves multi-volume archive to destination directory provided.
try (Archive archive = new Archive()) {
archive.createEntry("entry.bin", "data.bin");
archive.saveSplit( "C:\\Folder", new SplitArchiveSaveOptions("volume", 65536));
}
This method compose several (n) files filename.z01, filename.z02, …, filename.z(n-1), filename.zip.
Can not make existing archive multi-volume.
Parameters:
Parameter | Type | Description |
---|---|---|
destinationDirectory | java.lang.String | The path to the directory where archive segments to be created. |
options | SplitArchiveSaveOptions | Options for archive saving, including file name. |