Class Archive
- java.lang.Object
-
- com.aspose.zip.Archive
-
- All Implemented Interfaces:
IArchive,AutoCloseable
public class Archive extends Object implements IArchive, AutoCloseable
This class represents a zip archive file. Use it to compose, extract, or update zip archives.
-
-
Constructor Summary
Constructors Constructor Description Archive()Initializes a new instance of theArchiveclass with optional settings for its entries.Archive(ArchiveEntrySettings newEntrySettings)Initializes a new instance of theArchiveclass with optional settings for its entries.Archive(InputStream sourceStream)Initializes a new instance of theArchiveclass and composes an entry list can be extracted from the archive.Archive(InputStream sourceStream, ArchiveLoadOptions loadOptions)Initializes a new instance of theArchiveclass and composes an entry list can be extracted from the archive.Archive(InputStream sourceStream, ArchiveLoadOptions loadOptions, ArchiveEntrySettings newEntrySettings)Initializes a new instance of theArchiveclass and composes an entry list can be extracted from the archive.Archive(String path)Initializes a new instance of theArchiveclass and composes an entry list can be extracted from the archive.Archive(String path, ArchiveLoadOptions loadOptions)Initializes a new instance of theArchiveclass and composes an entry list can be extracted from the archive.Archive(String path, ArchiveLoadOptions loadOptions, ArchiveEntrySettings newEntrySettings)Initializes a new instance of theArchiveclass and composes an entry list can be extracted from the archive.Archive(String mainSegment, String[] segmentsInOrder)Initializes a new instance of theArchiveclass from multi-volume ZIP archive and composes an entry list can be extracted from the archive.Archive(String mainSegment, String[] segmentsInOrder, ArchiveLoadOptions loadOptions)Initializes a new instance of theArchiveclass from multi-volume ZIP archive and composes an entry list can be extracted from the archive.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()ArchivecreateEntries(File directory)Add to the archive all files and directories recursively in the directory given.ArchivecreateEntries(File directory, boolean includeRootDirectory)Add to the archive all files and directories recursively in the directory given.ArchivecreateEntries(String sourceDirectory)Add to the archive all files and directories recursively in the directory given.ArchivecreateEntries(String sourceDirectory, boolean includeRootDirectory)Add to the archive all files and directories recursively in the directory given.ArchiveEntrycreateEntry(String name, File file)Creates a single entry within the archive.ArchiveEntrycreateEntry(String name, File file, boolean openImmediately)Creates a single entry within the archive.ArchiveEntrycreateEntry(String name, File file, boolean openImmediately, ArchiveEntrySettings newEntrySettings)Creates a single entry within the archive.ArchiveEntrycreateEntry(String name, InputStream source)Creates a single entry within the archive.ArchiveEntrycreateEntry(String name, InputStream source, ArchiveEntrySettings newEntrySettings)Creates a single entry within the archive.ArchiveEntrycreateEntry(String name, InputStream source, ArchiveEntrySettings newEntrySettings, File file)Creates a single entry within the archive.ArchiveEntrycreateEntry(String name, String path)Creates a single entry within the archive.ArchiveEntrycreateEntry(String name, String path, boolean openImmediately)Creates a single entry within the archive.ArchiveEntrycreateEntry(String name, String path, boolean openImmediately, ArchiveEntrySettings newEntrySettings)Creates a single entry within the archive.ArchiveEntrycreateEntry(String name, Supplier<InputStream> streamProvider)Creates a single entry within the archive.ArchiveEntrycreateEntry(String name, Supplier<InputStream> streamProvider, ArchiveEntrySettings newEntrySettings)Creates a single entry within the archive.ArchivedeleteEntry(int entryIndex)Removes the entry from the entry list by index.ArchivedeleteEntry(ArchiveEntry entry)Removes the first occurrence of the specific entry from the entry list.voidextractToDirectory(String destinationDirectory)Extracts all the files in the archive to the directory provided.List<ArchiveEntry>getEntries()Gets entries ofArchiveEntrytype constituting the archive.Iterable<IArchiveFileEntry>getFileEntries()Gets entries ofIArchiveFileEntrytype constituting the archive.ArchiveFormatgetFormat()Gets the archive format.ArchiveEntrySettingsgetNewEntrySettings()Compression and encryption settings used for newly addedArchiveEntryitems.voidsave(OutputStream outputStream)Saves archive to the stream provided.voidsave(OutputStream outputStream, ArchiveSaveOptions saveOptions)Saves archive to the stream provided.voidsave(String destinationFileName)Saves archive to the destination file provided.voidsave(String destinationFileName, ArchiveSaveOptions saveOptions)Saves archive to the destination file provided.voidsaveSplit(String destinationDirectory, SplitArchiveSaveOptions options)Saves multi-volume archive to the destination directory provided.
-
-
-
Constructor Detail
-
Archive
public Archive()
Initializes a new instance of the
Archiveclass 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
Archiveclass 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 addedArchiveEntryitems. If not specified, the most common Deflate compression without encryption would be used.
-
Archive
public Archive(InputStream sourceStream)
Initializes a new instance of the
Archiveclass 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-sourceStreamis 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
Archiveclass 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-sourceStreamis 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
Archiveclass 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 addedArchiveEntryitems. If not specified, the most common Deflate compression without encryption would be used.- Throws:
com.aspose.ms.System.ArgumentException-sourceStreamis 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
Archiveclass 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-pathis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access.com.aspose.ms.System.ArgumentException- Thepathis empty, contains only white spaces, or contains invalid characters.com.aspose.ms.System.IO.PathTooLongException- The specifiedpath, 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 atpathcontains a colon (:) in the middle of the string.
-
Archive
public Archive(String path, ArchiveLoadOptions loadOptions)
Initializes a new instance of the
Archiveclass 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-pathis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access.com.aspose.ms.System.ArgumentException- Thepathis empty, contains only white spaces, or contains invalid characters.com.aspose.ms.System.IO.PathTooLongException- The specifiedpath, 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 atpathcontains a colon (:) in the middle of the string.
-
Archive
public Archive(String path, ArchiveLoadOptions loadOptions, ArchiveEntrySettings newEntrySettings)
Initializes a new instance of the
Archiveclass 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 addedArchiveEntryitems. If not specified, the most common Deflate compression without encryption would be used.- Throws:
com.aspose.ms.System.ArgumentNullException-pathis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access.com.aspose.ms.System.ArgumentException- Thepathis empty, contains only white spaces, or contains invalid characters.com.aspose.ms.System.IO.PathTooLongException- The specifiedpath, 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 atpathcontains a colon (:) in the middle of the string.
-
Archive
public Archive(String mainSegment, String[] segmentsInOrder)
Initializes a new instance of the
Archiveclass 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
This sample extract to a directory an archive of three segments.Archiveclass 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).
loadOptions- Options to load existing archive with.
-
-
Method Detail
-
getNewEntrySettings
public final ArchiveEntrySettings getNewEntrySettings()
Compression and encryption settings used for newly added
ArchiveEntryitems.- Returns:
- compression and encryption settings used for newly added
ArchiveEntryitems.
-
getEntries
public final List<ArchiveEntry> getEntries()
Gets entries of
ArchiveEntrytype constituting the archive.- Returns:
- entries of
ArchiveEntrytype constituting the archive.
-
getFormat
public final ArchiveFormat getFormat()
Gets the archive format.
-
getFileEntries
public final Iterable<IArchiveFileEntry> getFileEntries()
Gets entries of
IArchiveFileEntrytype constituting the archive.- Specified by:
getFileEntriesin interfaceIArchive- Returns:
- entries of
IArchiveFileEntrytype constituting the archive.
-
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
nameparameter. The file name provided inpathparameter 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-pathis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access.com.aspose.ms.System.ArgumentException- Thepathis empty, contains only white spaces, or contains invalid characters.com.aspose.ms.System.IO.PathTooLongException- The specifiedpath, 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 atpathcontains 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
nameparameter. The file name provided inpathparameter does not affect the entry name.If the file is opened immediately with
openImmediatelyparameter 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-pathis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access.com.aspose.ms.System.ArgumentException- Thepathis empty, contains only white spaces, or contains invalid characters.com.aspose.ms.System.IO.PathTooLongException- The specifiedpath, 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 atpathcontains 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
nameparameter. The file name provided inpathparameter does not affect the entry name.If the file is opened immediately with
openImmediatelyparameter 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 addedArchiveEntryitem.- Returns:
- Zip entry instance.
- Throws:
com.aspose.ms.System.ArgumentNullException-pathis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access.com.aspose.ms.System.ArgumentException- Thepathis empty, contains only white spaces, or contains invalid characters.com.aspose.ms.System.IO.PathTooLongException- The specifiedpath, 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 atpathcontains 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 addedArchiveEntryitem.- 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
nameparameter. The file name provided infileparameter 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
nameparameter. The file name provided infileparameter does not affect the entry name.If the file is opened immediately with
openImmediatelyparameter 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
nameparameter. The file name provided infileparameter does not affect the entry name.If the file is opened immediately with
openImmediatelyparameter 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 addedArchiveEntryitem.- 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
nameparameter. The file name provided infileparameter 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 addedArchiveEntryitem.file- The metadata of file or folder to be compressed.- Returns:
- Zip entry instance.
- Throws:
com.aspose.ms.System.InvalidOperationException- Bothsourceandfileare null orsourceis null andfilestands 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 entrystreamProvider- 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 entrystreamProvider- the method providing input stream for the entrynewEntrySettings- compression and encryption settings used for addedArchiveEntryitem- 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 todirectoryis invalid, such as being on an unmapped drive.com.aspose.ms.System.SecurityException- The caller does not have the required permission to accessdirectory.
-
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 todirectoryis invalid, such as being on an unmapped drive.com.aspose.ms.System.SecurityException- The caller does not have the required permission to accessdirectory.
-
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-entryIndexis less than 0.-or-entryIndexis equal to or greater thanEntriescount.
-
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) { }outputStreammust be writable.- Parameters:
outputStream- Destination stream.- Throws:
com.aspose.ms.System.ArgumentException-outputStreamis 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) { }outputStreammust be writable.- Parameters:
outputStream- Destination stream.saveOptions- Options for archive saving.- Throws:
com.aspose.ms.System.ArgumentException-outputStreamis 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-destinationFileNameis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access.com.aspose.ms.System.ArgumentException- ThedestinationFileNameis empty, contains only white spaces, or contains invalid characters.com.aspose.ms.System.IO.PathTooLongException- The specifieddestinationFileName, 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 atdestinationFileNamecontains 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-destinationFileNameis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access.com.aspose.ms.System.ArgumentException- ThedestinationFileNameis empty, contains only white spaces, or contains invalid characters.com.aspose.ms.System.IO.PathTooLongException- The specifieddestinationFileName, 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 atdestinationFileNamecontains 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-destinationDirectoryis null.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access the directory.com.aspose.ms.System.ArgumentException-destinationDirectorycontains 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:
extractToDirectoryin interfaceIArchive- Parameters:
destinationDirectory- The path to the directory to place the extracted files in.- Throws:
com.aspose.ms.System.ArgumentNullException-destinationDirectoryis 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-destinationDirectoryis 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.
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceIArchive
-
-