Class SevenZipArchive
- java.lang.Object
-
- com.aspose.zip.SevenZipArchive
-
- All Implemented Interfaces:
IArchive,AutoCloseable
public class SevenZipArchive extends Object implements IArchive, AutoCloseable
This class represents 7z archive file. Use it to compose and extract 7z archives.
-
-
Constructor Summary
Constructors Constructor Description SevenZipArchive()Initializes a new instance of theSevenZipArchiveclass with optional settings for its entries.SevenZipArchive(SevenZipEntrySettings newEntrySettings)Initializes a new instance of theSevenZipArchiveclass with optional settings for its entries.SevenZipArchive(InputStream sourceStream)Initializes a new instance of theSevenZipArchiveclass and composes an entry list can be extracted from the archive.SevenZipArchive(InputStream sourceStream, SevenZipLoadOptions options)Initializes a new instance of theSevenZipArchiveclass and composes an entry list can be extracted from the archive.SevenZipArchive(InputStream sourceStream, String password)Initializes a new instance of theSevenZipArchiveclass and composes an entry list can be extracted from the archive.SevenZipArchive(String path)Initializes a new instance of theSevenZipArchiveclass and composes an entry list can be extracted from the archive.SevenZipArchive(String[] parts)Initializes a new instance of theSevenZipArchiveclass from multi-volume 7z archive and composes an entry list can be extracted from the archive.SevenZipArchive(String[] parts, String password)Initializes a new instance of theSevenZipArchiveclass from multi-volume 7z archive and composes an entry list can be extracted from the archive.SevenZipArchive(String path, SevenZipLoadOptions options)Initializes a new instance of theSevenZipArchiveclass and composes an entry list can be extracted from the archive.SevenZipArchive(String path, String password)Initializes a new instance of theSevenZipArchiveclass 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()SevenZipArchivecreateEntries(File directory)Adds to the archive all files and directories recursively in the directory given.SevenZipArchivecreateEntries(File directory, boolean includeRootDirectory)Adds to the archive all files and directories recursively in the directory given.SevenZipArchivecreateEntries(String sourceDirectory)Adds to the archive all files and directories recursively in the directory given.SevenZipArchivecreateEntries(String sourceDirectory, boolean includeRootDirectory)Adds to the archive all files and directories recursively in the directory given.SevenZipArchiveEntrycreateEntry(String name, File file)Creates a single entry within the archive.SevenZipArchiveEntrycreateEntry(String name, File file, boolean openImmediately)Creates a single entry within the archive.SevenZipArchiveEntrycreateEntry(String name, File file, boolean openImmediately, SevenZipEntrySettings newEntrySettings)Creates a single entry within the archive.SevenZipArchiveEntrycreateEntry(String name, InputStream source)Creates a single entry within the archive.SevenZipArchiveEntrycreateEntry(String name, InputStream source, SevenZipEntrySettings newEntrySettings)Creates a single entry within the archive.SevenZipArchiveEntrycreateEntry(String name, InputStream source, SevenZipEntrySettings newEntrySettings, File file)Creates a single entry within the archive.SevenZipArchiveEntrycreateEntry(String name, String path)Creates a single entry within the archive.SevenZipArchiveEntrycreateEntry(String name, String path, boolean openImmediately)Creates a single entry within the archive.SevenZipArchiveEntrycreateEntry(String name, String path, boolean openImmediately, SevenZipEntrySettings newEntrySettings)Creates a single entry within the archive.voidextractToDirectory(String destinationDirectory)Extracts all the files in the archive to the directory provided.voidextractToDirectory(String destinationDirectory, String password)Extracts all the files in the archive to the directory provided.List<SevenZipArchiveEntry>getEntries()Gets entries ofSevenZipArchiveEntrytype constituting the archive.Iterable<IArchiveFileEntry>getFileEntries()Gets entries ofIArchiveFileEntrytype constituting the 7z archive.ArchiveFormatgetFormat()Gets the archive format.SevenZipEntrySettingsgetNewEntrySettings()Compression and encryption settings used for newly addedSevenZipArchiveEntryitems.voidsave(OutputStream output)Saves 7z archive to the stream provided.voidsave(String destinationFileName)Saves archive to a destination file provided.voidsaveSplit(String destinationDirectory, SplitSevenZipArchiveSaveOptions options)Saves multi-volume archive to destination directory provided.
-
-
-
Constructor Detail
-
SevenZipArchive
public SevenZipArchive()
Initializes a new instance of the
SevenZipArchiveclass with optional settings for its entries.The following example shows how to compress a single file with default settings: LZMA compression without encryption.
LZMA compression without encryption would be used.try (FileOutputStream sevenZipFile = new FileOutputStream("archive.7z")) { try (SevenZipArchive archive = new SevenZipArchive()) { archive.createEntry("data.bin", "file.dat"); archive.save(sevenZipFile); } } catch (IOException ex) { }
-
SevenZipArchive
public SevenZipArchive(SevenZipEntrySettings newEntrySettings)
Initializes a new instance of the
SevenZipArchiveclass with optional settings for its entries.The following example shows how to compress a single file with default settings: LZMA compression without encryption.
try (FileOutputStream sevenZipFile = new FileOutputStream("archive.7z")) { try (SevenZipArchive archive = new SevenZipArchive()) { archive.createEntry("data.bin", "file.dat"); archive.save(sevenZipFile); } } catch (IOException ex) { }- Parameters:
newEntrySettings- compression and encryption settings used for newly addedSevenZipArchiveEntryitems. If not specified, LZMA compression without encryption would be used
-
SevenZipArchive
public SevenZipArchive(InputStream sourceStream)
Initializes a new instance of the
SevenZipArchiveclass and composes an entry list can be extracted from the archive.try (SevenZipArchive archive = new SevenZipArchive(new FileInputStream("archive.7z"))) { archive.extractToDirectory("C:\\extracted"); } catch (FileNotFoundException ex) { }This constructor does not decompress any entry. See
extractToDirectory(String, String)method for decompressing.- Parameters:
sourceStream- the source of the archive- Throws:
com.aspose.ms.System.ArgumentException-sourceStreamis not seekable.com.aspose.ms.System.ArgumentNullException-sourceStreamis null.com.aspose.ms.System.NotImplementedException- The archive contains more than one coder.
-
SevenZipArchive
public SevenZipArchive(InputStream sourceStream, String password)
Initializes a new instance of the
SevenZipArchiveclass and composes an entry list can be extracted from the archive.try (SevenZipArchive archive = new SevenZipArchive(new FileInputStream("archive.7z"))) { archive.extractToDirectory("C:\\extracted"); } catch (FileNotFoundException ex) { }This constructor does not decompress any entry. See
extractToDirectory(String, String)method for decompressing.- Parameters:
sourceStream- the source of the archivepassword- optional password for decryption. If file names are encrypted, it must be present- Throws:
com.aspose.ms.System.ArgumentException-sourceStreamis not seekable.com.aspose.ms.System.ArgumentNullException-sourceStreamis null.com.aspose.ms.System.NotImplementedException- The archive contains more than one coder.
-
SevenZipArchive
public SevenZipArchive(String path)
Initializes a new instance of the
SevenZipArchiveclass and composes an entry list can be extracted from the archive.try (SevenZipArchive archive = new SevenZipArchive("archive.7z")) { archive.extractToDirectory("C:\\extracted"); } catch (FileNotFoundException ex) { }This constructor does not decompress any entry. See
extractToDirectory(String, String)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.com.aspose.ms.System.IO.FileNotFoundException- The file is not found.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.
-
SevenZipArchive
public SevenZipArchive(String path, String password)
Initializes a new instance of the
SevenZipArchiveclass and composes an entry list can be extracted from the archive.try (SevenZipArchive archive = new SevenZipArchive("archive.7z")) { archive.extractToDirectory("C:\\extracted"); } catch (FileNotFoundException ex) { }This constructor does not decompress any entry. See
extractToDirectory(String, String)method for decompressing.- Parameters:
path- the fully qualified or the relative path to the archive filepassword- optional password for decryption. If file names are encrypted, it must be present- 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.com.aspose.ms.System.IO.FileNotFoundException- The file is not found.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.
-
SevenZipArchive
public SevenZipArchive(InputStream sourceStream, SevenZipLoadOptions options)
Initializes a new instance of the
SevenZipArchiveclass and composes an entry list can be extracted from the archive.Extract an encrypted archive. Allow up to 60 seconds to proceed, cancel after that period.
try (CancellationFlag cf = new CancellationFlag()) { SevenZipLoadOptions options = new SevenZipLoadOptions(); options.setDecryptionPassword("Top$ecr3t"); options.setCancellationFlag(cf); cf.cancelAfter(TimeUnit.SECONDS.toMillis(60)); try (SevenZipArchive a = new SevenZipArchive(new FileInputStream("archive.7z"), options)) { a.extractToDirectory("C:\\extracted"); } catch (IOException ex) { } }- Parameters:
sourceStream- The source of the archive.options- Options to load existing archive with.This constructor does not decompress any entry. See
extractToDirectory(String, String)method for decompressing.- Throws:
com.aspose.ms.System.ArgumentException-sourceStreamis not seekable.com.aspose.ms.System.ArgumentNullException-sourceStreamis null.com.aspose.ms.System.NotImplementedException- The archive contains more than one coder. Now only LZMA compression supported.
-
SevenZipArchive
public SevenZipArchive(String path, SevenZipLoadOptions options)
Initializes a new instance of the
SevenZipArchiveclass and composes an entry list can be extracted from the archive.Extract an encrypted archive. Allow up to 60 seconds to proceed, cancel after that period.
try (CancellationFlag cf = new CancellationFlag()) { SevenZipLoadOptions options = new SevenZipLoadOptions(); options.setDecryptionPassword("Top$ecr3t"); options.setCancellationFlag(cf); cf.cancelAfter(TimeUnit.SECONDS.toMillis(60)); try (SevenZipArchive a = new SevenZipArchive(new FileInputStream("archive.7z"), options)) { a.extractToDirectory("C:\\extracted"); } catch (IOException ex) { } }- Parameters:
path- The fully qualified or the relative path to the archive file.options- Options to load existing archive with.This constructor does not decompress any entry. See
extractToDirectory(String, String)method for decompressing.- 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.com.aspose.ms.System.IO.FileNotFoundException- The file is not found.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.
-
SevenZipArchive
public SevenZipArchive(String[] parts)
Initializes a new instance of the
SevenZipArchiveclass from multi-volume 7z archive and composes an entry list can be extracted from the archive.try (SevenZipArchive archive = new SevenZipArchive(new String[] { "multi.7z.001", "multi.7z.002", "multi.7z.003" } )) { archive.extractToDirectory("C:\\extracted"); }- Parameters:
parts- paths to each segment of multi-volume 7z archive respecting order- Throws:
com.aspose.ms.System.ArgumentNullException-partsis null.com.aspose.ms.System.ArgumentException-partshas no entries.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access.com.aspose.ms.System.ArgumentException- The path to a file is empty, contains only white spaces, or contains invalid characters.com.aspose.ms.System.IO.PathTooLongException- The specified path to a part, 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 a path contains a colon (:) in the middle of the string.
-
SevenZipArchive
public SevenZipArchive(String[] parts, String password)
Initializes a new instance of the
SevenZipArchiveclass from multi-volume 7z archive and composes an entry list can be extracted from the archive.try (SevenZipArchive archive = new SevenZipArchive(new String[] { "multi.7z.001", "multi.7z.002", "multi.7z.003" } )) { archive.extractToDirectory("C:\\extracted"); }- Parameters:
parts- paths to each segment of multi-volume 7z archive respecting orderpassword- optional password for decryption. If file names are encrypted, it must be present- Throws:
com.aspose.ms.System.ArgumentNullException-partsis null.com.aspose.ms.System.ArgumentException-partshas no entries.com.aspose.ms.System.SecurityException- The caller does not have the required permission to access.com.aspose.ms.System.ArgumentException- The path to a file is empty, contains only white spaces, or contains invalid characters.com.aspose.ms.System.IO.PathTooLongException- The specified path to a part, 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 a path contains a colon (:) in the middle of the string.
-
-
Method Detail
-
getFileEntries
public final Iterable<IArchiveFileEntry> getFileEntries()
Gets entries of
IArchiveFileEntrytype constituting the 7z archive.- Specified by:
getFileEntriesin interfaceIArchive- Returns:
- entries of
IArchiveFileEntrytype constituting the 7z archive
-
getFormat
public final ArchiveFormat getFormat()
Gets the archive format.
-
getNewEntrySettings
public final SevenZipEntrySettings getNewEntrySettings()
Compression and encryption settings used for newly added
SevenZipArchiveEntryitems.- Returns:
- compression and encryption settings used for newly added
SevenZipArchiveEntryitems
-
getEntries
public final List<SevenZipArchiveEntry> getEntries()
Gets entries of
SevenZipArchiveEntrytype constituting the archive.- Returns:
- entries of
SevenZipArchiveEntrytype constituting the archive
-
createEntry
public final SevenZipArchiveEntry createEntry(String name, File file)
Creates a single entry within the archive.
Compose archive with entries encrypted with different passwords each.
try (FileOutputStream sevenZipFile = new FileOutputStream("archive.7z")) { File fi1 = new File("data1.bin"); File fi2 = new File("data2.bin"); File fi3 = new File("data3.bin"); try (SevenZipArchive archive = new SevenZipArchive()) { archive.createEntry("entry1.bin", fi1, false, new SevenZipEntrySettings(new SevenZipStoreCompressionSettings(), new SevenZipAESEncryptionSettings("test1"))); archive.createEntry("entry2.bin", fi2, false, new SevenZipEntrySettings(new SevenZipStoreCompressionSettings(), new SevenZipAESEncryptionSettings("test2"))); archive.createEntry("entry3.bin", fi3, false, new SevenZipEntrySettings(new SevenZipStoreCompressionSettings(), new SevenZipAESEncryptionSettings("test3"))); archive.save(sevenZipFile); } } catch (IOException ex) { }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 entryfile- the metadata of file to be compressed- Returns:
- Seven 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 SevenZipArchiveEntry createEntry(String name, File file, boolean openImmediately)
Creates a single entry within the archive.
Compose archive with entries encrypted with different passwords each.
try (FileOutputStream sevenZipFile = new FileOutputStream("archive.7z")) { File fi1 = new File("data1.bin"); File fi2 = new File("data2.bin"); File fi3 = new File("data3.bin"); try (SevenZipArchive archive = new SevenZipArchive()) { archive.createEntry("entry1.bin", fi1, false, new SevenZipEntrySettings(new SevenZipStoreCompressionSettings(), new SevenZipAESEncryptionSettings("test1"))); archive.createEntry("entry2.bin", fi2, false, new SevenZipEntrySettings(new SevenZipStoreCompressionSettings(), new SevenZipAESEncryptionSettings("test2"))); archive.createEntry("entry3.bin", fi3, false, new SevenZipEntrySettings(new SevenZipStoreCompressionSettings(), new SevenZipAESEncryptionSettings("test3"))); archive.save(sevenZipFile); } } catch (IOException ex) { }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 entryfile- the metadata of file to be compressedopenImmediately- true, if open the file immediately, otherwise open the file on archive saving- Returns:
- Seven 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 SevenZipArchiveEntry createEntry(String name, File file, boolean openImmediately, SevenZipEntrySettings newEntrySettings)
Creates a single entry within the archive.
Compose archive with entries encrypted with different passwords each.
try (FileOutputStream sevenZipFile = new FileOutputStream("archive.7z")) { File fi1 = new File("data1.bin"); File fi2 = new File("data2.bin"); File fi3 = new File("data3.bin"); try (SevenZipArchive archive = new SevenZipArchive()) { archive.createEntry("entry1.bin", fi1, false, new SevenZipEntrySettings(new SevenZipStoreCompressionSettings(), new SevenZipAESEncryptionSettings("test1"))); archive.createEntry("entry2.bin", fi2, false, new SevenZipEntrySettings(new SevenZipStoreCompressionSettings(), new SevenZipAESEncryptionSettings("test2"))); archive.createEntry("entry3.bin", fi3, false, new SevenZipEntrySettings(new SevenZipStoreCompressionSettings(), new SevenZipAESEncryptionSettings("test3"))); archive.save(sevenZipFile); } } catch (IOException ex) { }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 entryfile- the metadata of file to be compressedopenImmediately- true, if open the file immediately, otherwise open the file on archive savingnewEntrySettings- compression and encryption settings used for addedSevenZipArchiveEntryitem. Individual compression settings is ignored in case of solid compression, seeSevenZipEntrySettings.Solid(SevenZipEntrySettings.getSolid()/SevenZipEntrySettings.setSolid(boolean)).- Returns:
- Seven 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 SevenZipArchiveEntry createEntry(String name, InputStream source, SevenZipEntrySettings newEntrySettings, File file)
Creates a single entry within the archive.
Compose archive with LZMA compressed encrypted entry.
try (FileOutputStream sevenZipFile = new FileOutputStream("archive.7z")) { try (SevenZipArchive archive = new SevenZipArchive()) { archive.createEntry("entry1.bin", new ByteArrayInputStream(new byte[] {0x00, (byte)0xFF}), new SevenZipEntrySettings(new SevenZipLZMACompressionSettings(), new SevenZipAESEncryptionSettings("test1")), new File("data1.bin")); archive.save(sevenZipFile); } } catch (IOException ex) { }The entry name is solely set within
nameparameter. The file name provided infileparameter does not affect the entry name.filecan refer to directory.- Parameters:
name- the name of the entrysource- the input stream for the entrynewEntrySettings- compression and encryption settings used for addedSevenZipArchiveEntryitem. Individual compression settings is ignored in case of solid compression, seeSevenZipEntrySettings.Solid(SevenZipEntrySettings.getSolid()/SevenZipEntrySettings.setSolid(boolean)).file- the metadata of file or folder to be compressed- Returns:
- Seven Zip entry instance
- Throws:
com.aspose.ms.System.InvalidOperationException- Bothsourceandfileare null orsourceis null andfilestands for directory.
-
createEntry
public final SevenZipArchiveEntry createEntry(String name, InputStream source)
Creates a single entry within the archive.
Compose 7z archive with LZMA compression and encryption of all entries.
try (SevenZipArchive archive = new SevenZipArchive(new SevenZipEntrySettings(new SevenZipLZMACompressionSettings(), new SevenZipAESEncryptionSettings("p@s$")))) { archive.createEntry("data.bin", new ByteArrayInputStream(new byte[] {0x00, (byte)0xFF} )); archive.save("archive.7z"); }- Parameters:
name- the name of the entrysource- the input stream for the entry- Returns:
- Seven Zip entry instance
-
createEntry
public final SevenZipArchiveEntry createEntry(String name, InputStream source, SevenZipEntrySettings newEntrySettings)
Creates a single entry within the archive.
Compose 7z archive with LZMA compression and encryption of all entries.
try (SevenZipArchive archive = new SevenZipArchive(new SevenZipEntrySettings(new SevenZipLZMACompressionSettings(), new SevenZipAESEncryptionSettings("p@s$")))) { archive.createEntry("data.bin", new ByteArrayInputStream(new byte[] {0x00, (byte)0xFF} )); archive.save("archive.7z"); }- Parameters:
name- the name of the entrysource- the input stream for the entrynewEntrySettings- compression and encryption settings used for addedSevenZipArchiveEntryitem. Individual compression settings is ignored in case of solid compression, seeSevenZipEntrySettings.Solid(SevenZipEntrySettings.getSolid()/SevenZipEntrySettings.setSolid(boolean)).- Returns:
- Seven Zip entry instance
-
createEntry
public final SevenZipArchiveEntry createEntry(String name, String path)
Creates a single entry within the archive.
try (FileOutputStream sevenZipFile = new FileOutputStream("archive.7z")) { try (SevenZipArchive archive = new SevenZipArchive(new SevenZipEntrySettings(new SevenZipLZMACompressionSettings()))) { archive.createEntry("data.bin", "file.dat"); archive.save(sevenZipFile); } } 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 entrypath- the fully qualified name of the new file, or the relative file name to be compressed- Returns:
- Seven 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 SevenZipArchiveEntry createEntry(String name, String path, boolean openImmediately)
Creates a single entry within the archive.
try (FileOutputStream sevenZipFile = new FileOutputStream("archive.7z")) { try (SevenZipArchive archive = new SevenZipArchive(new SevenZipEntrySettings(new SevenZipLZMACompressionSettings()))) { archive.createEntry("data.bin", "file.dat"); archive.save(sevenZipFile); } } 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 entrypath- the fully qualified name of the new file, or the relative file name to be compressedopenImmediately- true, if open the file immediately, otherwise open the file on archive saving- Returns:
- Seven 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 SevenZipArchiveEntry createEntry(String name, String path, boolean openImmediately, SevenZipEntrySettings newEntrySettings)
Creates a single entry within the archive.
try (FileOutputStream sevenZipFile = new FileOutputStream("archive.7z")) { try (SevenZipArchive archive = new SevenZipArchive(new SevenZipEntrySettings(new SevenZipLZMACompressionSettings()))) { archive.createEntry("data.bin", "file.dat"); archive.save(sevenZipFile); } } 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 entrypath- the fully qualified name of the new file, or the relative file name to be compressedopenImmediately- true, if open the file immediately, otherwise open the file on archive savingnewEntrySettings- compression and encryption settings used for addedSevenZipArchiveEntryitem. Individual compression settings is ignored in case of solid compression, seeSevenZipEntrySettings.Solid(SevenZipEntrySettings.getSolid()/SevenZipEntrySettings.setSolid(boolean)).- 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.
-
createEntries
public final SevenZipArchive createEntries(File directory)
Adds to the archive all files and directories recursively in the directory given.
try (SevenZipArchive archive = new SevenZipArchive()) { File folder = new File("C:\\folder"); archive.createEntries(folder); archive.save("folder.7z"); }- 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 SevenZipArchive createEntries(File directory, boolean includeRootDirectory)
Adds to the archive all files and directories recursively in the directory given.
try (SevenZipArchive archive = new SevenZipArchive()) { File folder = new File("C:\\folder"); archive.createEntries(folder); archive.save("folder.7z"); }- Parameters:
directory- directory to compressincludeRootDirectory- 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 SevenZipArchive createEntries(String sourceDirectory)
Adds to the archive all files and directories recursively in the directory given.
Compose 7z archive with LZMA compression.
try (SevenZipArchive archive = new SevenZipArchive(new SevenZipEntrySettings(new SevenZipLZMACompressionSettings()))) { archive.createEntries("C:\\folder"); archive.save("folder.7z"); }- Parameters:
sourceDirectory- directory to compress- Returns:
- the archive with entries composed
-
createEntries
public final SevenZipArchive createEntries(String sourceDirectory, boolean includeRootDirectory)
Adds to the archive all files and directories recursively in the directory given.
Compose 7z archive with LZMA compression.
try (SevenZipArchive archive = new SevenZipArchive(new SevenZipEntrySettings(new SevenZipLZMACompressionSettings()))) { archive.createEntries("C:\\folder"); archive.save("folder.7z"); }- Parameters:
sourceDirectory- directory to compressincludeRootDirectory- indicates whether to include the root directory itself or not- Returns:
- the archive with entries composed
-
save
public final void save(OutputStream output)
Saves 7z archive to the stream provided.
try (FileOutputStream sevenZipFile = new FileOutputStream("archive.7z")) { try (FileInputStream source = new FileInputStream("data.bin")) { try (SevenZipArchive archive = new SevenZipArchive()) { archive.createEntry("data", source); archive.save(sevenZipFile); } } } catch (IOException ex) { }- Parameters:
output- destination stream- Throws:
com.aspose.ms.System.ArgumentNullException-outputis null.com.aspose.ms.System.InvalidOperationException- Encoder failed to compress data.
-
save
public final void save(String destinationFileName)
Saves archive to a destination file provided.
try (FileInputStream source = new FileInputStream("data.bin")) { try (SevenZipArchive archive = new SevenZipArchive(new SevenZipEntrySettings(new SevenZipLZMACompressionSettings()))) { archive.createEntry("data", source); archive.save("archive.7z"); } } catch (IOException ex) { }- Parameters:
destinationFileName- the path of the archive to be created. If the specified file name points to an existing file, it will be overwritten.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 a temporary file.
- 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, SplitSevenZipArchiveSaveOptions options)
Saves multi-volume archive to destination directory provided.
try (SevenZipArchive archive = new SevenZipArchive()) { archive.createEntry("entry.bin", "data.bin"); archive.saveSplit("C:\\Folder", new SplitSevenZipArchiveSaveOptions("volume", 65536)); }This method composes several
(n)files filename.7z.001, filename.7z.002, ..., filename.7z.(n).Cannot make existing archive multi-volume.
- Parameters:
destinationDirectory- the path to the directory where archive segments to be createdoptions- 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.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 (SevenZipArchive archive = new SevenZipArchive("archive.7z")) { archive.extractToDirectory("C:\\extracted"); }- Specified by:
extractToDirectoryin interfaceIArchive- Parameters:
destinationDirectory- the path to the directory to place the extracted files in.If the directory does not exist, it will be created
- 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 the 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- The archive is corrupted.
-
extractToDirectory
public final void extractToDirectory(String destinationDirectory, String password)
Extracts all the files in the archive to the directory provided.
try (SevenZipArchive archive = new SevenZipArchive("archive.7z")) { archive.extractToDirectory("C:\\extracted"); }- Parameters:
destinationDirectory- the path to the directory to place the extracted files inIf the directory does not exist, it will be created.
password- optional password for content decryption.passwordis used for content decryption only. If file names are encrypted provide password inSevenZipArchive(String, String)orSevenZipArchive(java.io.InputStream, String)constructor.- 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 the 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- The archive is corrupted.
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceIArchive
-
-