Class Archive

    • Constructor Detail

      • 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

        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:
        newEntrySettings - Compression and encryption settings used for newly added ArchiveEntry items. If not specified, the most common Deflate compression without encryption would be used.
      • Archive

        public Archive​(InputStream sourceStream)

        Initializes a new instance of the Archive class and composes an entry list can be extracted from the archive.

        The following example extracts an encrypted archive, then decompresses 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:
        sourceStream - The source of the archive.
        Throws:
        com.aspose.ms.System.ArgumentException - sourceStream is not seekable.
        com.aspose.ms.System.IO.InvalidDataException - Encryption header for AES contradicts WinZip compression method.
      • Archive

        public Archive​(InputStream sourceStream,
                       ArchiveLoadOptions loadOptions)

        Initializes a new instance of the Archive class and composes an entry list can be extracted from the archive.

        The following example extracts an encrypted archive, then decompresses 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:
        sourceStream - The source of the archive.
        loadOptions - Options to load existing archive with.
        Throws:
        com.aspose.ms.System.ArgumentException - sourceStream is not seekable.
        com.aspose.ms.System.IO.InvalidDataException - Encryption header for AES contradicts WinZip compression method.
      • Archive

        public Archive​(InputStream sourceStream,
                       ArchiveLoadOptions loadOptions,
                       ArchiveEntrySettings newEntrySettings)

        Initializes a new instance of the Archive class and composes an entry list can be extracted from the archive.

        The following example extracts an encrypted archive, then decompresses 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:
        sourceStream - The source of the archive.
        loadOptions - Options to load existing archive with.
        newEntrySettings - Compression and encryption settings used for newly added ArchiveEntry items. If not specified, the most common Deflate compression without encryption would be used.
        Throws:
        com.aspose.ms.System.ArgumentException - sourceStream is not seekable.
        com.aspose.ms.System.IO.InvalidDataException - Encryption header for AES contradicts WinZip compression method.
      • Archive

        public Archive​(String path)

        Initializes a new instance of the Archive class and composes an entry list can be extracted from the archive.

        The following example extracts an encrypted archive, then decompresses 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:
        path - The fully qualified or the relative path to the archive file.
        Throws:
        com.aspose.ms.System.ArgumentNullException - path is null.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access.
        com.aspose.ms.System.ArgumentException - The path is empty, contains only white spaces, or contains invalid characters.
        com.aspose.ms.System.IO.PathTooLongException - The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
        com.aspose.ms.System.NotSupportedException - File at path contains a colon (:) in the middle of the string.
      • Archive

        public Archive​(String path,
                       ArchiveLoadOptions loadOptions)

        Initializes a new instance of the Archive class and composes an entry list can be extracted from the archive.

        The following example extracts an encrypted archive, then decompresses 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:
        path - The fully qualified or the relative path to the archive file.
        loadOptions - Options to load existing archive with.
        Throws:
        com.aspose.ms.System.ArgumentNullException - path is null.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access.
        com.aspose.ms.System.ArgumentException - The path is empty, contains only white spaces, or contains invalid characters.
        com.aspose.ms.System.IO.PathTooLongException - The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
        com.aspose.ms.System.NotSupportedException - File at path contains a colon (:) in the middle of the string.
      • Archive

        public Archive​(String path,
                       ArchiveLoadOptions loadOptions,
                       ArchiveEntrySettings newEntrySettings)

        Initializes a new instance of the Archive class and composes an entry list can be extracted from the archive.

        The following example extracts an encrypted archive, then decompresses 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:
        path - The fully qualified or the relative path to the archive file.
        loadOptions - Options to load existing archive with.
        newEntrySettings - Compression and encryption settings used for newly added ArchiveEntry items. If not specified, the most common Deflate compression without encryption would be used.
        Throws:
        com.aspose.ms.System.ArgumentNullException - path is null.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access.
        com.aspose.ms.System.ArgumentException - The path is empty, contains only white spaces, or contains invalid characters.
        com.aspose.ms.System.IO.PathTooLongException - The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
        com.aspose.ms.System.NotSupportedException - File at path contains a colon (:) in the middle of the string.
      • Archive

        public Archive​(String mainSegment,
                       String[] segmentsInOrder)

        Initializes a new instance of the Archive class from multi-volume ZIP archive and composes an entry list can be extracted from the archive.

        
             try (Archive a = new Archive("archive.zip", new String[] { "archive.z01", "archive.z02" })) {
                 a.extractToDirectory("destination");
             }
         
        Parameters:
        mainSegment - Path to the last segment of multi-volume archive with the central directory.

        Usually this segment has *.zip extension and smaller than others.

        segmentsInOrder - 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

        public Archive​(String mainSegment,
                       String[] segmentsInOrder,
                       ArchiveLoadOptions loadOptions)

        Initializes a new instance of the Archive class from multi-volume ZIP archive and composes an entry list can be extracted from the archive.

        This sample extract to a directory an archive of three segments.
        
             try (Archive a = new Archive("archive.zip", new String[] { "archive.z01", "archive.z02" })) {
                 a.extractToDirectory("destination");
             }
         
        Parameters:
        mainSegment - Path to the last segment of multi-volume archive with the central directory.

        Usually this segment has *.zip extension and smaller than others.

        segmentsInOrder - 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 - Options to load existing archive with.
    • Method Detail

      • getNewEntrySettings

        public final ArchiveEntrySettings getNewEntrySettings()

        Compression and encryption settings used for newly added ArchiveEntry items.

        Returns:
        compression and encryption settings used for newly added ArchiveEntry items.
      • getFormat

        public final ArchiveFormat getFormat()

        Gets the archive format.

        Specified by:
        getFormat in interface IArchive
        Returns:
        the archive format
      • createEntry

        public final ArchiveEntry createEntry​(String name,
                                              String path)

        Creates a 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:
        name - The name of the entry.
        path - The fully qualified name of the new file, or the relative file name to be compressed.
        Returns:
        Zip entry instance.
        Throws:
        com.aspose.ms.System.ArgumentNullException - path is null.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access.
        com.aspose.ms.System.ArgumentException - The path is empty, contains only white spaces, or contains invalid characters.
        com.aspose.ms.System.IO.PathTooLongException - The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
        com.aspose.ms.System.NotSupportedException - File at path contains a colon (:) in the middle of the string.
      • createEntry

        public final ArchiveEntry createEntry​(String name,
                                              String path,
                                              boolean openImmediately)

        Creates a 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:
        name - The name of the entry.
        path - The fully qualified name of the new file, or the relative file name to be compressed.
        openImmediately - True, if open the file immediately, otherwise open the file on archive saving.
        Returns:
        Zip entry instance.
        Throws:
        com.aspose.ms.System.ArgumentNullException - path is null.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access.
        com.aspose.ms.System.ArgumentException - The path is empty, contains only white spaces, or contains invalid characters.
        com.aspose.ms.System.IO.PathTooLongException - The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
        com.aspose.ms.System.NotSupportedException - File at path contains a colon (:) in the middle of the string.
      • createEntry

        public final ArchiveEntry createEntry​(String name,
                                              String path,
                                              boolean openImmediately,
                                              ArchiveEntrySettings newEntrySettings)

        Creates a 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:
        name - The name of the entry.
        path - The fully qualified name of the new file, or the relative file name to be compressed.
        openImmediately - True, if open the file immediately, otherwise open the file on archive saving.
        newEntrySettings - Compression and encryption settings used for added ArchiveEntry item.
        Returns:
        Zip entry instance.
        Throws:
        com.aspose.ms.System.ArgumentNullException - path is null.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access.
        com.aspose.ms.System.ArgumentException - The path is empty, contains only white spaces, or contains invalid characters.
        com.aspose.ms.System.IO.PathTooLongException - The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
        com.aspose.ms.System.NotSupportedException - File at path contains a colon (:) in the middle of the string.
      • createEntry

        public final ArchiveEntry createEntry​(String name,
                                              InputStream source)

        Creates a 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:
        name - The name of the entry.
        source - The input stream for the entry.
        Returns:
        Zip entry instance.
      • createEntry

        public final ArchiveEntry createEntry​(String name,
                                              InputStream source,
                                              ArchiveEntrySettings newEntrySettings)

        Creates a 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:
        name - The name of the entry.
        source - The input stream for the entry.
        newEntrySettings - Compression and encryption settings used for added ArchiveEntry item.
        Returns:
        Zip entry instance.
      • createEntry

        public final ArchiveEntry createEntry​(String name,
                                              File file)

        Creates a 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:
        name - The name of the entry.
        file - The metadata of file to be compressed.
        Returns:
        Zip entry instance.
        Throws:
        com.aspose.ms.System.IO.DirectoryNotFoundException - The specified path is invalid, such as being on an unmapped drive.
        com.aspose.ms.System.IO.IOException - The file is already open.
      • createEntry

        public final ArchiveEntry createEntry​(String name,
                                              File file,
                                              boolean openImmediately)

        Creates a 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:
        name - The name of the entry.
        file - The metadata of file to be compressed.
        openImmediately - True, if open the file immediately, otherwise open the file on archive saving.
        Returns:
        Zip entry instance.
        Throws:
        com.aspose.ms.System.IO.DirectoryNotFoundException - The specified path is invalid, such as being on an unmapped drive.
        com.aspose.ms.System.IO.IOException - The file is already open.
      • createEntry

        public final ArchiveEntry createEntry​(String name,
                                              File file,
                                              boolean openImmediately,
                                              ArchiveEntrySettings newEntrySettings)

        Creates a 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:
        name - The name of the entry.
        file - The metadata of file to be compressed.
        openImmediately - True, if open the file immediately, otherwise open the file on archive saving.
        newEntrySettings - Compression and encryption settings used for added ArchiveEntry item.
        Returns:
        Zip entry instance.
        Throws:
        com.aspose.ms.System.IO.DirectoryNotFoundException - The specified path is invalid, such as being on an unmapped drive.
        com.aspose.ms.System.IO.IOException - The file is already open.
      • createEntry

        public final ArchiveEntry createEntry​(String name,
                                              InputStream source,
                                              ArchiveEntrySettings newEntrySettings,
                                              File file)

        Creates a 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:
        name - The name of the entry.
        source - The input stream for the entry.
        newEntrySettings - Compression and encryption settings used for added ArchiveEntry item.
        file - The metadata of file or folder to be compressed.
        Returns:
        Zip entry instance.
        Throws:
        com.aspose.ms.System.InvalidOperationException - Both source and file are null or source is null and file stands for directory.
      • createEntry

        public final ArchiveEntry createEntry​(String name,
                                              Supplier<InputStream> streamProvider)

        Creates a 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:
        name - the name of the entry
        streamProvider - the method providing input stream for the entry
        Returns:
        zip entry instance
      • createEntry

        public final ArchiveEntry createEntry​(String name,
                                              Supplier<InputStream> streamProvider,
                                              ArchiveEntrySettings newEntrySettings)

        Creates a 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:
        name - the name of the entry
        streamProvider - the method providing input stream for the entry
        newEntrySettings - compression and encryption settings used for added ArchiveEntry item
        Returns:
        zip entry instance
      • createEntries

        public final Archive createEntries​(File directory)

        Add 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:
        directory - Directory to compress.
        Returns:
        The archive with entries composed.
        Throws:
        com.aspose.ms.System.IO.DirectoryNotFoundException - The path to directory is invalid, such as being on an unmapped drive.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access directory.
      • createEntries

        public final Archive createEntries​(File directory,
                                           boolean includeRootDirectory)

        Add 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:
        directory - Directory to compress.
        includeRootDirectory - Indicates whether to include the root directory itself or not.
        Returns:
        The archive with entries composed.
        Throws:
        com.aspose.ms.System.IO.DirectoryNotFoundException - The path to directory is invalid, such as being on an unmapped drive.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access directory.
      • createEntries

        public final Archive createEntries​(String sourceDirectory)

        Add 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:
        sourceDirectory - Directory to compress.
        Returns:
        The archive with entries composed.
      • createEntries

        public final Archive createEntries​(String sourceDirectory,
                                           boolean includeRootDirectory)

        Add 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:
        sourceDirectory - Directory to compress.
        includeRootDirectory - Indicates whether to include the root directory itself or not.
        Returns:
        The archive with entries composed.
      • deleteEntry

        public final Archive deleteEntry​(ArchiveEntry entry)

        Removes the first occurrence of the specific entry from the entry 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:
        entry - The entry to remove from the entries list.
        Returns:
        The archive with the entry deleted.
      • deleteEntry

        public final Archive deleteEntry​(int entryIndex)

        Removes the entry from the entry list by index.

        
            try (Archive archive = new Archive("two_files.zip")) {
                archive.deleteEntry(0);
                archive.save("single_file.zip");
            }
         
        Parameters:
        entryIndex - The zero-based index of the entry to remove.
        Returns:
        The archive with the entry deleted.
        Throws:
        com.aspose.ms.System.ArgumentOutOfRangeException - entryIndex is less than 0.-or- entryIndex is equal to or greater than Entries count.
      • save

        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:
        outputStream - Destination stream.
        Throws:
        com.aspose.ms.System.ArgumentException - outputStream is not writable.
      • save

        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:
        outputStream - Destination stream.
        saveOptions - Options for archive saving.
        Throws:
        com.aspose.ms.System.ArgumentException - outputStream is not writable.
      • save

        public final void save​(String destinationFileName)

        Saves archive to the 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:
        destinationFileName - The path of the archive to be created. If the specified file name points to an existing file, it will be overwritten.
        Throws:
        com.aspose.ms.System.ArgumentNullException - destinationFileName is null.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access.
        com.aspose.ms.System.ArgumentException - The destinationFileName is empty, contains only white spaces, or contains invalid characters.
        com.aspose.ms.System.IO.PathTooLongException - The specified destinationFileName, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
        com.aspose.ms.System.NotSupportedException - File at destinationFileName contains a colon (:) in the middle of the string.
      • save

        public final void save​(String destinationFileName,
                               ArchiveSaveOptions saveOptions)

        Saves archive to the 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:
        destinationFileName - The path of the archive to be created. If the specified file name points to an existing file, it will be overwritten.
        saveOptions - Options for archive saving.
        Throws:
        com.aspose.ms.System.ArgumentNullException - destinationFileName is null.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access.
        com.aspose.ms.System.ArgumentException - The destinationFileName is empty, contains only white spaces, or contains invalid characters.
        com.aspose.ms.System.IO.PathTooLongException - The specified destinationFileName, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
        com.aspose.ms.System.NotSupportedException - File at destinationFileName contains a colon (:) in the middle of the string.
      • saveSplit

        public final void saveSplit​(String destinationDirectory,
                                    SplitArchiveSaveOptions options)

        Saves multi-volume archive to the destination directory provided.

        
            try (Archive archive = new Archive()) {
                archive.createEntry("entry.bin", "data.bin");
                archive.saveSplit( "C:\\Folder", new SplitArchiveSaveOptions("volume", 65536));
            }
         

        This method composes several (n) files filename.z01, filename.z02, ..., filename.z(n-1), filename.zip.

        Cannot make existing archive multi-volume.

        Parameters:
        destinationDirectory - The path to the directory where archive segments to be created.
        options - Options for archive saving, including file name.
        Throws:
        com.aspose.ms.System.InvalidOperationException - This archive was opened from the existing source.
        com.aspose.ms.System.NotSupportedException - This archive is both compressed with XZ method and encrypted.
        com.aspose.ms.System.ArgumentNullException - destinationDirectory is null.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access the directory.
        com.aspose.ms.System.ArgumentException - destinationDirectory contains invalid characters such as ", >, <, or |.
        com.aspose.ms.System.IO.PathTooLongException - The specified path exceeds the system-defined maximum length.
      • extractToDirectory

        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.

        Specified by:
        extractToDirectory in interface IArchive
        Parameters:
        destinationDirectory - The path to the directory to place the extracted files in.
        Throws:
        com.aspose.ms.System.ArgumentNullException - destinationDirectory is null.
        com.aspose.ms.System.IO.PathTooLongException - The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters.
        com.aspose.ms.System.SecurityException - The caller does not have the required permission to access the existing directory.
        com.aspose.ms.System.NotSupportedException - If directory does not exist, the path contains a colon character (:) that is not part of a drive label ("C:\").
        com.aspose.ms.System.ArgumentException - destinationDirectory is a zero-length string, contains only white space, or contains one or more invalid characters.
        com.aspose.ms.System.IO.IOException - The directory specified by path is a file. -or- The network name is not known.
        com.aspose.ms.System.IO.InvalidDataException - Wrong password has been supplied.