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

ConstructorDescription
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

MethodDescription
close(){@inheritDoc}
createEntries(File directory)Adds to the archive all files and directories recursively in the directory given.
createEntries(File directory, boolean includeRootDirectory)Adds to the archive all files and directories recursively in the directory given.
createEntries(String sourceDirectory)Adds to the archive all files and directories recursively in the directory given.
createEntries(String sourceDirectory, boolean includeRootDirectory)Adds to the archive all files and directories recursively in the directory given.
createEntry(String name, File file)Create single entry within the archive.
createEntry(String name, File file, boolean openImmediately)Create single entry within the archive.
createEntry(String name, File file, boolean openImmediately, ArchiveEntrySettings newEntrySettings)Create single entry within the archive.
createEntry(String name, InputStream source)Create single entry within the archive.
createEntry(String name, InputStream source, ArchiveEntrySettings newEntrySettings)Create single entry within the archive.
createEntry(String name, InputStream source, ArchiveEntrySettings newEntrySettings, File file)Create single entry within the archive.
createEntry(String name, String path)Create single entry within the archive.
createEntry(String name, String path, boolean openImmediately)Create single entry within the archive.
createEntry(String name, String path, boolean openImmediately, ArchiveEntrySettings newEntrySettings)Create single entry within the archive.
createEntry(String name, Supplier<InputStream> streamProvider)Create single entry within the archive.
createEntry(String name, Supplier<InputStream> streamProvider, ArchiveEntrySettings newEntrySettings)Create single entry within the archive.
deleteEntry(ArchiveEntry entry)Removes the first occurrence of a specific entry from the entries list.
deleteEntry(int entryIndex)Removes the entry from the entries list by index.
extractToDirectory(String destinationDirectory)Extracts all the files in the archive to the directory provided.
getEntries()Gets entries of ArchiveEntry type constituting the archive.
getFileEntries()Gets entries of IArchiveFileEntry type constituting the archive.
getNewEntrySettings()Compression and encryption settings used for newly added ArchiveEntry items.
save(OutputStream outputStream)Saves archive to the stream provided.
save(OutputStream outputStream, ArchiveSaveOptions saveOptions)Saves archive to the stream provided.
save(String destinationFileName)Saves archive to destination file provided.
save(String destinationFileName, ArchiveSaveOptions saveOptions)Saves archive to destination file provided.
saveSplit(String destinationDirectory, SplitArchiveSaveOptions options)Saves multi-volume archive to destination directory provided.

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:

ParameterTypeDescription
newEntrySettingsArchiveEntrySettingsCompression 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:

ParameterTypeDescription
sourceStreamjava.io.InputStreamThe 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:

ParameterTypeDescription
sourceStreamjava.io.InputStreamThe source of the archive.
loadOptionsArchiveLoadOptionsOptions 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:

ParameterTypeDescription
sourceStreamjava.io.InputStreamThe source of the archive.
loadOptionsArchiveLoadOptionsOptions to load existing archive with.
newEntrySettingsArchiveEntrySettingsCompression 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:

ParameterTypeDescription
pathjava.lang.StringThe 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:

ParameterTypeDescription
pathjava.lang.StringThe fully qualified or the relative path to the archive file.
loadOptionsArchiveLoadOptionsOptions 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:

ParameterTypeDescription
pathjava.lang.StringThe fully qualified or the relative path to the archive file.
loadOptionsArchiveLoadOptionsOptions to load existing archive with.
newEntrySettingsArchiveEntrySettingsCompression 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:

ParameterTypeDescription
mainSegmentjava.lang.StringPath 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:

ParameterTypeDescription
mainSegmentjava.lang.StringPath 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:

ParameterTypeDescription
directoryjava.io.FileDirectory 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:

ParameterTypeDescription
directoryjava.io.FileDirectory to compress.
includeRootDirectorybooleanIndicates 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:

ParameterTypeDescription
sourceDirectoryjava.lang.StringDirectory 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:

ParameterTypeDescription
sourceDirectoryjava.lang.StringDirectory to compress.
includeRootDirectorybooleanIndicates 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:

ParameterTypeDescription
namejava.lang.StringThe name of the entry.
filejava.io.FileThe 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:

ParameterTypeDescription
namejava.lang.StringThe name of the entry.
filejava.io.FileThe metadata of file to be compressed.
openImmediatelybooleanTrue 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:

ParameterTypeDescription
namejava.lang.StringThe name of the entry.
filejava.io.FileThe metadata of file to be compressed.
openImmediatelybooleanTrue if open the file immediately, otherwise open the file on archive saving.
newEntrySettingsArchiveEntrySettingsCompression 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:

ParameterTypeDescription
namejava.lang.StringThe name of the entry.
sourcejava.io.InputStreamThe 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:

ParameterTypeDescription
namejava.lang.StringThe name of the entry.
sourcejava.io.InputStreamThe input stream for the entry.
newEntrySettingsArchiveEntrySettingsCompression 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:

ParameterTypeDescription
namejava.lang.StringThe name of the entry.
sourcejava.io.InputStreamThe input stream for the entry.
newEntrySettingsArchiveEntrySettingsCompression and encryption settings used for added ArchiveEntry item.
filejava.io.FileThe 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:

ParameterTypeDescription
namejava.lang.StringThe name of the entry.
pathjava.lang.StringThe 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:

ParameterTypeDescription
namejava.lang.StringThe name of the entry.
pathjava.lang.StringThe fully qualified name of the new file, or the relative file name to be compressed.
openImmediatelybooleanTrue 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:

ParameterTypeDescription
namejava.lang.StringThe name of the entry.
pathjava.lang.StringThe fully qualified name of the new file, or the relative file name to be compressed.
openImmediatelybooleanTrue if open the file immediately, otherwise open the file on archive saving.
newEntrySettingsArchiveEntrySettingsCompression 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:

ParameterTypeDescription
namejava.lang.Stringthe name of the entry
streamProviderjava.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:

ParameterTypeDescription
namejava.lang.Stringthe name of the entry
streamProviderjava.util.function.Supplier<java.io.InputStream>the method providing input stream for the entry
newEntrySettingsArchiveEntrySettingscompression 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:

ParameterTypeDescription
entryArchiveEntryThe 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:

ParameterTypeDescription
entryIndexintThe 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:

ParameterTypeDescription
destinationDirectoryjava.lang.StringThe 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:

ParameterTypeDescription
outputStreamjava.io.OutputStreamDestination 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:

ParameterTypeDescription
outputStreamjava.io.OutputStreamDestination stream.
saveOptionsArchiveSaveOptionsOptions 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:

ParameterTypeDescription
destinationFileNamejava.lang.StringThe 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:

ParameterTypeDescription
destinationFileNamejava.lang.StringThe path of the archive to be created. If the specified file name points to an existing file, it will be overwritten.
saveOptionsArchiveSaveOptionsOptions 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:

ParameterTypeDescription
destinationDirectoryjava.lang.StringThe path to the directory where archive segments to be created.
optionsSplitArchiveSaveOptionsOptions for archive saving, including file name.