SevenZipArchive
Inheritance: java.lang.Object
All Implemented Interfaces: com.aspose.zip.IArchive, java.lang.AutoCloseable
public class SevenZipArchive implements IArchive, AutoCloseable
This class represents 7z archive file. Use it to compose and extract 7z archives.
Constructors
Constructor | Description |
---|---|
SevenZipArchive() | Initializes a new instance of the SevenZipArchive class with optional settings for its entries. |
SevenZipArchive(SevenZipEntrySettings newEntrySettings) | Initializes a new instance of the SevenZipArchive class with optional settings for its entries. |
SevenZipArchive(InputStream sourceStream) | Initializes a new instance of the SevenZipArchive class and composes entries list can be extracted from the archive. |
SevenZipArchive(InputStream sourceStream, String password) | Initializes a new instance of the SevenZipArchive class and composes entries list can be extracted from the archive. |
SevenZipArchive(String path) | Initializes a new instance of the SevenZipArchive class and composes entries list can be extracted from the archive. |
SevenZipArchive(String path, String password) | Initializes a new instance of the SevenZipArchive class and composes entries list can be extracted from the archive. |
SevenZipArchive(String[] parts) | Initializes a new instance of the SevenZipArchive class from multi-volume 7z archive and composes entries list can be extracted from the archive. |
SevenZipArchive(String[] parts, String password) | Initializes a new instance of the SevenZipArchive class from multi-volume 7z archive and composes entries list can be extracted from the archive. |
Methods
SevenZipArchive()
public SevenZipArchive()
Initializes a new instance of the SevenZipArchive class 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) {
}
LZMA compression without encryption would be used.
SevenZipArchive(SevenZipEntrySettings newEntrySettings)
public SevenZipArchive(SevenZipEntrySettings newEntrySettings)
Initializes a new instance of the SevenZipArchive class 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:
Parameter | Type | Description |
---|---|---|
newEntrySettings | SevenZipEntrySettings | compression and encryption settings used for newly added SevenZipArchiveEntry items. If not specified, LZMA compression without encryption would be used |
SevenZipArchive(InputStream sourceStream)
public SevenZipArchive(InputStream sourceStream)
Initializes a new instance of the SevenZipArchive class and composes entries 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:
Parameter | Type | Description |
---|---|---|
sourceStream | java.io.InputStream | the source of the archive |
SevenZipArchive(InputStream sourceStream, String password)
public SevenZipArchive(InputStream sourceStream, String password)
Initializes a new instance of the SevenZipArchive class and composes entries 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:
Parameter | Type | Description |
---|---|---|
sourceStream | java.io.InputStream | the source of the archive |
password | java.lang.String | optional password for decryption. If file names are encrypted it must be present |
SevenZipArchive(String path)
public SevenZipArchive(String path)
Initializes a new instance of the SevenZipArchive class and composes entries 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:
Parameter | Type | Description |
---|---|---|
path | java.lang.String | the fully qualified or the relative path to the archive file |
SevenZipArchive(String path, String password)
public SevenZipArchive(String path, String password)
Initializes a new instance of the SevenZipArchive class and composes entries 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:
Parameter | Type | Description |
---|---|---|
path | java.lang.String | the fully qualified or the relative path to the archive file |
password | java.lang.String | optional password for decryption. If file names are encrypted it must be present |
SevenZipArchive(String[] parts)
public SevenZipArchive(String[] parts)
Initializes a new instance of the SevenZipArchive class from multi-volume 7z archive and composes entries 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:
Parameter | Type | Description |
---|---|---|
parts | java.lang.String[] | paths to each segment of multi-volume 7z archive respecting order |
SevenZipArchive(String[] parts, String password)
public SevenZipArchive(String[] parts, String password)
Initializes a new instance of the SevenZipArchive class from multi-volume 7z archive and composes entries 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:
Parameter | Type | Description |
---|---|---|
parts | java.lang.String[] | paths to each segment of multi-volume 7z archive respecting order |
password | java.lang.String | optional password for decryption. If file names are encrypted it must be present |
close()
public void close()
createEntries(File directory)
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:
Parameter | Type | Description |
---|---|---|
directory | java.io.File | directory to compress |
Returns: SevenZipArchive - the archive with entries composed
createEntries(File directory, boolean includeRootDirectory)
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:
Parameter | Type | Description |
---|---|---|
directory | java.io.File | directory to compress |
includeRootDirectory | boolean | indicates whether to include the root directory itself or not |
Returns: SevenZipArchive - the archive with entries composed
createEntries(String sourceDirectory)
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:
Parameter | Type | Description |
---|---|---|
sourceDirectory | java.lang.String | directory to compress |
Returns: SevenZipArchive - the archive with entries composed
createEntries(String sourceDirectory, boolean includeRootDirectory)
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:
Parameter | Type | Description |
---|---|---|
sourceDirectory | java.lang.String | directory to compress |
includeRootDirectory | boolean | indicates whether to include the root directory itself or not |
Returns: SevenZipArchive - the archive with entries composed
createEntry(String name, File file)
public final SevenZipArchiveEntry createEntry(String name, File file)
Create 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 name
parameter. The file name provided in file
parameter does not affect the entry name.
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | the name of the entry |
file | java.io.File | the metadata of file to be compressed |
Returns: SevenZipArchiveEntry - Seven Zip entry instance
createEntry(String name, File file, boolean openImmediately)
public final SevenZipArchiveEntry createEntry(String name, File file, boolean openImmediately)
Create 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 name
parameter. The file name provided in file
parameter does not affect the entry name.
If the file is opened immediately with openImmediately
parameter it becomes blocked until archive is saved.
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | the name of the entry |
file | java.io.File | the metadata of file to be compressed |
openImmediately | boolean | true if open the file immediately, otherwise open the file on archive saving |
Returns: SevenZipArchiveEntry - Seven Zip entry instance
createEntry(String name, File file, boolean openImmediately, SevenZipEntrySettings newEntrySettings)
public final SevenZipArchiveEntry createEntry(String name, File file, boolean openImmediately, SevenZipEntrySettings newEntrySettings)
Create 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 name
parameter. The file name provided in file
parameter does not affect the entry name.
If the file is opened immediately with openImmediately
parameter it becomes blocked until archive is saved.
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | the name of the entry |
file | java.io.File | the metadata of file to be compressed |
openImmediately | boolean | true if open the file immediately, otherwise open the file on archive saving |
newEntrySettings | SevenZipEntrySettings | compression and encryption settings used for added SevenZipArchiveEntry item |
Returns: SevenZipArchiveEntry - Seven Zip entry instance
createEntry(String name, InputStream source)
public final SevenZipArchiveEntry createEntry(String name, InputStream source)
Create 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:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | the name of the entry |
source | java.io.InputStream | the input stream for the entry |
Returns: SevenZipArchiveEntry - Seven Zip entry instance
createEntry(String name, InputStream source, SevenZipEntrySettings newEntrySettings)
public final SevenZipArchiveEntry createEntry(String name, InputStream source, SevenZipEntrySettings newEntrySettings)
Create 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:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | the name of the entry |
source | java.io.InputStream | the input stream for the entry |
newEntrySettings | SevenZipEntrySettings | compression and encryption settings used for added SevenZipArchiveEntry item |
Returns: SevenZipArchiveEntry - Seven Zip entry instance
createEntry(String name, InputStream source, SevenZipEntrySettings newEntrySettings, File file)
public final SevenZipArchiveEntry createEntry(String name, InputStream source, SevenZipEntrySettings newEntrySettings, File file)
Create 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 name
parameter. The file name provided in file
parameter does not affect the entry name.
file
can refer to directory.
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | the name of the entry |
source | java.io.InputStream | the input stream for the entry |
newEntrySettings | SevenZipEntrySettings | compression and encryption settings used for added SevenZipArchiveEntry item |
file | java.io.File | the metadata of file or folder to be compressed |
Returns: SevenZipArchiveEntry - Seven Zip entry instance
createEntry(String name, String path)
public final SevenZipArchiveEntry createEntry(String name, String path)
Create 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 name
parameter. The file name provided in path
parameter does not affect the entry name.
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | the name of the entry |
path | java.lang.String | the fully qualified name of the new file, or the relative file name to be compressed |
Returns: SevenZipArchiveEntry - Seven Zip entry instance
createEntry(String name, String path, boolean openImmediately)
public final SevenZipArchiveEntry createEntry(String name, String path, boolean openImmediately)
Create 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 name
parameter. The file name provided in path
parameter does not affect the entry name.
If the file is opened immediately with openImmediately
parameter it becomes blocked until archive is saved.
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | the name of the entry |
path | java.lang.String | the fully qualified name of the new file, or the relative file name to be compressed |
openImmediately | boolean | true if open the file immediately, otherwise open the file on archive saving |
Returns: SevenZipArchiveEntry - Seven Zip entry instance
createEntry(String name, String path, boolean openImmediately, SevenZipEntrySettings newEntrySettings)
public final SevenZipArchiveEntry createEntry(String name, String path, boolean openImmediately, SevenZipEntrySettings newEntrySettings)
Create 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 name
parameter. The file name provided in path
parameter does not affect the entry name.
If the file is opened immediately with openImmediately
parameter it becomes blocked until archive is saved.
Parameters:
Parameter | Type | Description |
---|---|---|
name | java.lang.String | the name of the entry |
path | java.lang.String | the fully qualified name of the new file, or the relative file name to be compressed |
openImmediately | boolean | true if open the file immediately, otherwise open the file on archive saving |
newEntrySettings | SevenZipEntrySettings | compression and encryption settings used for added SevenZipArchiveEntry item |
Returns: SevenZipArchiveEntry - Zip entry instance
extractToDirectory(String destinationDirectory)
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");
}
Parameters:
Parameter | Type | Description |
---|---|---|
destinationDirectory | java.lang.String | the path to the directory to place the extracted files in. |
If the directory does not exist, it will be created |
extractToDirectory(String destinationDirectory, String password)
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:
Parameter | Type | Description |
---|---|---|
destinationDirectory | java.lang.String | the path to the directory to place the extracted files in |
If the directory does not exist, it will be created. | | password | java.lang.String | optional password for content decryption.
password
is used for content decryption only. If file names are encrypted provide password in SevenZipArchive(String, String) or SevenZipArchive(java.io.InputStream, String) constructor. |
getEntries()
public final List<SevenZipArchiveEntry> getEntries()
Gets entries of SevenZipArchiveEntry type constituting the archive.
Returns: java.util.List<com.aspose.zip.SevenZipArchiveEntry> - entries of SevenZipArchiveEntry type constituting the archive
getFileEntries()
public final Iterable<IArchiveFileEntry> getFileEntries()
Gets entries of IArchiveFileEntry type constituting the 7z archive.
Returns: java.lang.Iterable<com.aspose.zip.IArchiveFileEntry> - entries of IArchiveFileEntry type constituting the 7z archive
getNewEntrySettings()
public final SevenZipEntrySettings getNewEntrySettings()
Compression and encryption settings used for newly added SevenZipArchiveEntry items.
Returns: SevenZipEntrySettings - compression and encryption settings used for newly added SevenZipArchiveEntry items
save(OutputStream output)
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:
Parameter | Type | Description |
---|---|---|
output | java.io.OutputStream | destination stream |
save(String destinationFileName)
public final void save(String destinationFileName)
Saves archive to 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:
Parameter | Type | Description |
---|---|---|
destinationFileName | java.lang.String | the path of the archive to be created. If the specified file name points to an existing file, it will be overwritten. |
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. |
saveSplit(String destinationDirectory, SplitSevenZipArchiveSaveOptions options)
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 compose several (n)
files filename.7z.001, filename.7z.002, …, filename.7z.(n).
Can not make existing archive multi-volume.
Parameters:
Parameter | Type | Description |
---|---|---|
destinationDirectory | java.lang.String | the path to the directory where archive segments to be created |
options | SplitSevenZipArchiveSaveOptions | options for archive saving, including file name |