Class SevenZFile
- All Implemented Interfaces:
Closeable
,AutoCloseable
The 7z file format is a flexible container that can contain many compression and encryption types, but at the moment only only Copy, LZMA, LZMA2, BZIP2, Deflate and AES-256 + SHA-256 are supported.
The format is very Windows/Intel specific, so it uses little-endian byte order, doesn't store user/group or permission bits, and represents times using NTFS timestamps (100 nanosecond units since 1 January 1601). Hence the official tools recommend against using it for backup purposes on *nix, and recommend .tar.7z or .tar.lzma or .tar.xz instead.
Both the header and file contents may be compressed and/or encrypted. With both encrypted, neither file names nor file contents can be read, but the use of encryption isn't plausibly deniable.
Multi volume archives can be read by concatenating the parts in correct order - either manually or by using {link org.apache.commons.compress.utils.MultiReadOnlySeekableByteChannel} for example.
- Since:
- 1.6
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Archive
private SeekableByteChannel
private long
private int
private int
private InputStream
private static final String
private final ArrayList<InputStream>
private final String
private final SevenZFileOptions
private byte[]
private static final CharsetEncoder
(package private) static final byte[]
(package private) static final int
private long
-
Constructor Summary
ConstructorsModifierConstructorDescriptionSevenZFile
(File fileName) Reads a file as unencrypted 7z archiveSevenZFile
(File fileName, byte[] password) Deprecated.use the char[]-arg version for the password insteadSevenZFile
(File fileName, char[] password) Reads a file as 7z archiveSevenZFile
(File fileName, char[] password, SevenZFileOptions options) Reads a file as 7z archive with additional options.SevenZFile
(File fileName, SevenZFileOptions options) Reads a file as unencrypted 7z archiveSevenZFile
(SeekableByteChannel channel) Reads a SeekableByteChannel as 7z archiveSevenZFile
(SeekableByteChannel channel, byte[] password) Deprecated.use the char[]-arg version for the password insteadSevenZFile
(SeekableByteChannel channel, char[] password) Reads a SeekableByteChannel as 7z archiveSevenZFile
(SeekableByteChannel channel, char[] password, SevenZFileOptions options) Reads a SeekableByteChannel as 7z archive with additional options.SevenZFile
(SeekableByteChannel channel, String fileName) Reads a SeekableByteChannel as 7z archiveSevenZFile
(SeekableByteChannel channel, String fileName, byte[] password) Deprecated.use the char[]-arg version for the password insteadprivate
SevenZFile
(SeekableByteChannel channel, String filename, byte[] password, boolean closeOnError, SevenZFileOptions options) SevenZFile
(SeekableByteChannel channel, String fileName, char[] password) Reads a SeekableByteChannel as 7z archiveSevenZFile
(SeekableByteChannel channel, String fileName, char[] password, SevenZFileOptions options) Reads a SeekableByteChannel as 7z archive with addtional options.SevenZFile
(SeekableByteChannel channel, String fileName, SevenZFileOptions options) Reads a SeekableByteChannel as 7z archive with additional options.SevenZFile
(SeekableByteChannel channel, SevenZFileOptions options) Reads a SeekableByteChannel as 7z archive with addtional options. -
Method Summary
Modifier and TypeMethodDescriptionprivate static int
assertFitsIntoNonNegativeInt
(String what, long value) private InputStream
buildDecoderStack
(Folder folder, long folderOffset, int firstPackStreamIndex, SevenZArchiveEntry entry) private void
buildDecodingStream
(int entryIndex, boolean isRandomAccess) Build the decoding stream for the entry to be read.private void
calculateStreamMap
(Archive archive) private void
checkEntryIsInitialized
(Map<Integer, SevenZArchiveEntry> archiveEntries, int index) void
close()
Closes the archive.private static void
get
(ByteBuffer buf, byte[] to) private static char
getChar
(ByteBuffer buf) private InputStream
Derives a default file name from the archive name - if known.Returns a copy of meta-data of all archive entries.getInputStream
(SevenZArchiveEntry entry) Returns an InputStream for reading the contents of the given entry.private static int
getInt
(ByteBuffer buf) private static long
getLong
(ByteBuffer buf) Returns the next Archive Entry in this archive.Provides statistics for bytes read from the current entry.private static int
private boolean
Find out if any data of current entry has been read or not.private Archive
initializeArchive
(StartHeader startHeader, byte[] password, boolean verifyCrc) static boolean
matches
(byte[] signature, int length) Checks if the signature matches what is expected for a 7z file.int
read()
Reads a byte of data.int
read
(byte[] b) Reads data into an array of bytes.int
read
(byte[] b, int off, int len) Reads data into an array of bytes.private BitSet
readAllOrBits
(ByteBuffer header, int size) private void
readArchiveProperties
(ByteBuffer input) private BitSet
readBits
(ByteBuffer header, int size) private ByteBuffer
readEncodedHeader
(ByteBuffer header, Archive archive, byte[] password) private void
readFilesInfo
(ByteBuffer header, Archive archive) private Folder
readFolder
(ByteBuffer header) private void
readFully
(ByteBuffer buf) private void
readHeader
(ByteBuffer header, Archive archive) private Archive
readHeaders
(byte[] password) private void
readPackInfo
(ByteBuffer header, Archive archive) private StartHeader
readStartHeader
(long startHeaderCrc) private void
readStreamsInfo
(ByteBuffer header, Archive archive) private void
readSubStreamsInfo
(ByteBuffer header, Archive archive) private static long
readUint64
(ByteBuffer in) private void
readUnpackInfo
(ByteBuffer header, Archive archive) private void
reopenFolderInputStream
(int folderIndex, SevenZArchiveEntry file) Discard any queued streams/ folder stream, and reopen the current folder input stream.private SevenZFile.ArchiveStatistics
private void
private void
sanityCheckFilesInfo
(ByteBuffer header, SevenZFile.ArchiveStatistics stats) private int
sanityCheckFolder
(ByteBuffer header, SevenZFile.ArchiveStatistics stats) private void
sanityCheckPackInfo
(ByteBuffer header, SevenZFile.ArchiveStatistics stats) private void
sanityCheckStreamsInfo
(ByteBuffer header, SevenZFile.ArchiveStatistics stats) private void
sanityCheckSubStreamsInfo
(ByteBuffer header, SevenZFile.ArchiveStatistics stats) private void
sanityCheckUnpackInfo
(ByteBuffer header, SevenZFile.ArchiveStatistics stats) private static long
skipBytesFully
(ByteBuffer input, long bytesToSkip) private boolean
skipEntriesWhenNeeded
(int entryIndex, boolean isInSameFolder, int folderIndex) Skip all the entries if needed.toString()
private Archive
tryToLocateEndHeader
(byte[] password) private static byte[]
utf16Decode
(char[] chars)
-
Field Details
-
SIGNATURE_HEADER_SIZE
static final int SIGNATURE_HEADER_SIZE- See Also:
-
DEFAULT_FILE_NAME
- See Also:
-
fileName
-
channel
-
archive
-
currentEntryIndex
private int currentEntryIndex -
currentFolderIndex
private int currentFolderIndex -
currentFolderInputStream
-
password
private byte[] password -
options
-
compressedBytesReadFromCurrentEntry
private long compressedBytesReadFromCurrentEntry -
uncompressedBytesReadFromCurrentEntry
private long uncompressedBytesReadFromCurrentEntry -
deferredBlockStreams
-
sevenZSignature
static final byte[] sevenZSignature -
PASSWORD_ENCODER
-
-
Constructor Details
-
SevenZFile
Reads a file as 7z archive- Parameters:
fileName
- the file to readpassword
- optional password if the archive is encrypted- Throws:
IOException
- if reading the archive fails- Since:
- 1.17
-
SevenZFile
Reads a file as 7z archive with additional options.- Parameters:
fileName
- the file to readpassword
- optional password if the archive is encryptedoptions
- the options to apply- Throws:
IOException
- if reading the archive fails or the memory limit (if set) is too small- Since:
- 1.19
-
SevenZFile
Deprecated.use the char[]-arg version for the password insteadReads a file as 7z archive- Parameters:
fileName
- the file to readpassword
- optional password if the archive is encrypted - the byte array is supposed to be the UTF16-LE encoded representation of the password.- Throws:
IOException
- if reading the archive fails
-
SevenZFile
Reads a SeekableByteChannel as 7z archiveSeekableInMemoryByteChannel
allows you to read from an in-memory archive.- Parameters:
channel
- the channel to read- Throws:
IOException
- if reading the archive fails- Since:
- 1.13
-
SevenZFile
Reads a SeekableByteChannel as 7z archive with addtional options.SeekableInMemoryByteChannel
allows you to read from an in-memory archive.- Parameters:
channel
- the channel to readoptions
- the options to apply- Throws:
IOException
- if reading the archive fails or the memory limit (if set) is too small- Since:
- 1.19
-
SevenZFile
Reads a SeekableByteChannel as 7z archiveSeekableInMemoryByteChannel
allows you to read from an in-memory archive.- Parameters:
channel
- the channel to readpassword
- optional password if the archive is encrypted- Throws:
IOException
- if reading the archive fails- Since:
- 1.17
-
SevenZFile
public SevenZFile(SeekableByteChannel channel, char[] password, SevenZFileOptions options) throws IOException Reads a SeekableByteChannel as 7z archive with additional options.SeekableInMemoryByteChannel
allows you to read from an in-memory archive.- Parameters:
channel
- the channel to readpassword
- optional password if the archive is encryptedoptions
- the options to apply- Throws:
IOException
- if reading the archive fails or the memory limit (if set) is too small- Since:
- 1.19
-
SevenZFile
Reads a SeekableByteChannel as 7z archiveSeekableInMemoryByteChannel
allows you to read from an in-memory archive.- Parameters:
channel
- the channel to readfileName
- name of the archive - only used for error reportingpassword
- optional password if the archive is encrypted- Throws:
IOException
- if reading the archive fails- Since:
- 1.17
-
SevenZFile
public SevenZFile(SeekableByteChannel channel, String fileName, char[] password, SevenZFileOptions options) throws IOException Reads a SeekableByteChannel as 7z archive with addtional options.SeekableInMemoryByteChannel
allows you to read from an in-memory archive.- Parameters:
channel
- the channel to readfileName
- name of the archive - only used for error reportingpassword
- optional password if the archive is encryptedoptions
- the options to apply- Throws:
IOException
- if reading the archive fails or the memory limit (if set) is too small- Since:
- 1.19
-
SevenZFile
Reads a SeekableByteChannel as 7z archiveSeekableInMemoryByteChannel
allows you to read from an in-memory archive.- Parameters:
channel
- the channel to readfileName
- name of the archive - only used for error reporting- Throws:
IOException
- if reading the archive fails- Since:
- 1.17
-
SevenZFile
public SevenZFile(SeekableByteChannel channel, String fileName, SevenZFileOptions options) throws IOException Reads a SeekableByteChannel as 7z archive with additional options.SeekableInMemoryByteChannel
allows you to read from an in-memory archive.- Parameters:
channel
- the channel to readfileName
- name of the archive - only used for error reportingoptions
- the options to apply- Throws:
IOException
- if reading the archive fails or the memory limit (if set) is too small- Since:
- 1.19
-
SevenZFile
Deprecated.use the char[]-arg version for the password insteadReads a SeekableByteChannel as 7z archiveSeekableInMemoryByteChannel
allows you to read from an in-memory archive.- Parameters:
channel
- the channel to readpassword
- optional password if the archive is encrypted - the byte array is supposed to be the UTF16-LE encoded representation of the password.- Throws:
IOException
- if reading the archive fails- Since:
- 1.13
-
SevenZFile
@Deprecated public SevenZFile(SeekableByteChannel channel, String fileName, byte[] password) throws IOException Deprecated.use the char[]-arg version for the password insteadReads a SeekableByteChannel as 7z archiveSeekableInMemoryByteChannel
allows you to read from an in-memory archive.- Parameters:
channel
- the channel to readfileName
- name of the archive - only used for error reportingpassword
- optional password if the archive is encrypted - the byte array is supposed to be the UTF16-LE encoded representation of the password.- Throws:
IOException
- if reading the archive fails- Since:
- 1.13
-
SevenZFile
private SevenZFile(SeekableByteChannel channel, String filename, byte[] password, boolean closeOnError, SevenZFileOptions options) throws IOException - Throws:
IOException
-
SevenZFile
Reads a file as unencrypted 7z archive- Parameters:
fileName
- the file to read- Throws:
IOException
- if reading the archive fails
-
SevenZFile
Reads a file as unencrypted 7z archive- Parameters:
fileName
- the file to readoptions
- the options to apply- Throws:
IOException
- if reading the archive fails or the memory limit (if set) is too small- Since:
- 1.19
-
-
Method Details
-
close
Closes the archive.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
- if closing the file fails
-
getNextEntry
Returns the next Archive Entry in this archive.- Returns:
- the next entry,
or
null
if there are no more entries - Throws:
IOException
- if the next entry could not be read
-
getEntries
Returns a copy of meta-data of all archive entries.This method only provides meta-data, the entries can not be used to read the contents, you still need to process all entries in order using
getNextEntry()
for that.The content methods are only available for entries that have already been reached via
getNextEntry()
.- Returns:
- a copy of meta-data of all archive entries.
- Since:
- 1.11
-
readHeaders
- Throws:
IOException
-
tryToLocateEndHeader
- Throws:
IOException
-
initializeArchive
private Archive initializeArchive(StartHeader startHeader, byte[] password, boolean verifyCrc) throws IOException - Throws:
IOException
-
readStartHeader
- Throws:
IOException
-
readHeader
- Throws:
IOException
-
sanityCheckAndCollectStatistics
private SevenZFile.ArchiveStatistics sanityCheckAndCollectStatistics(ByteBuffer header) throws IOException - Throws:
IOException
-
readArchiveProperties
- Throws:
IOException
-
sanityCheckArchiveProperties
- Throws:
IOException
-
readEncodedHeader
private ByteBuffer readEncodedHeader(ByteBuffer header, Archive archive, byte[] password) throws IOException - Throws:
IOException
-
sanityCheckStreamsInfo
private void sanityCheckStreamsInfo(ByteBuffer header, SevenZFile.ArchiveStatistics stats) throws IOException - Throws:
IOException
-
readStreamsInfo
- Throws:
IOException
-
sanityCheckPackInfo
private void sanityCheckPackInfo(ByteBuffer header, SevenZFile.ArchiveStatistics stats) throws IOException - Throws:
IOException
-
readPackInfo
- Throws:
IOException
-
sanityCheckUnpackInfo
private void sanityCheckUnpackInfo(ByteBuffer header, SevenZFile.ArchiveStatistics stats) throws IOException - Throws:
IOException
-
readUnpackInfo
- Throws:
IOException
-
sanityCheckSubStreamsInfo
private void sanityCheckSubStreamsInfo(ByteBuffer header, SevenZFile.ArchiveStatistics stats) throws IOException - Throws:
IOException
-
readSubStreamsInfo
- Throws:
IOException
-
sanityCheckFolder
private int sanityCheckFolder(ByteBuffer header, SevenZFile.ArchiveStatistics stats) throws IOException - Throws:
IOException
-
readFolder
- Throws:
IOException
-
readAllOrBits
- Throws:
IOException
-
readBits
- Throws:
IOException
-
sanityCheckFilesInfo
private void sanityCheckFilesInfo(ByteBuffer header, SevenZFile.ArchiveStatistics stats) throws IOException - Throws:
IOException
-
readFilesInfo
- Throws:
IOException
-
checkEntryIsInitialized
-
calculateStreamMap
- Throws:
IOException
-
buildDecodingStream
Build the decoding stream for the entry to be read. This method may be called from a random access(getInputStream) or sequential access(getNextEntry). If this method is called from a random access, some entries may need to be skipped(we put them to the deferredBlockStreams and skip them when actually needed to improve the performance)- Parameters:
entryIndex
- the index of the entry to be readisRandomAccess
- is this called in a random access- Throws:
IOException
- if there are exceptions when reading the file
-
reopenFolderInputStream
Discard any queued streams/ folder stream, and reopen the current folder input stream.- Parameters:
folderIndex
- the index of the folder to reopenfile
- the 7z entry to read- Throws:
IOException
- if exceptions occur when reading the 7z file
-
skipEntriesWhenNeeded
private boolean skipEntriesWhenNeeded(int entryIndex, boolean isInSameFolder, int folderIndex) throws IOException Skip all the entries if needed. Entries need to be skipped when:1. it's a random access 2. one of these 2 condition is meet :
2.1 currentEntryIndex != entryIndex : this means there are some entries to be skipped(currentEntryIndex < entryIndex) or the entry has already been read(currentEntryIndex > entryIndex)
2.2 currentEntryIndex == entryIndex && !hasCurrentEntryBeenRead: if the entry to be read is the current entry, but some data of it has been read before, then we need to reopen the stream of the folder and skip all the entries before the current entries
- Parameters:
entryIndex
- the entry to be readisInSameFolder
- are the entry to be read and the current entry in the same folderfolderIndex
- the index of the folder which contains the entry- Returns:
- true if there are entries actually skipped
- Throws:
IOException
- there are exceptions when skipping entries- Since:
- 1.21
-
hasCurrentEntryBeenRead
private boolean hasCurrentEntryBeenRead()Find out if any data of current entry has been read or not. This is achieved by comparing the bytes remaining to read and the size of the file.- Returns:
- true if any data of current entry has been read
- Since:
- 1.21
-
buildDecoderStack
private InputStream buildDecoderStack(Folder folder, long folderOffset, int firstPackStreamIndex, SevenZArchiveEntry entry) throws IOException - Throws:
IOException
-
read
Reads a byte of data.- Returns:
- the byte read, or -1 if end of input is reached
- Throws:
IOException
- if an I/O error has occurred
-
getCurrentStream
- Throws:
IOException
-
getInputStream
Returns an InputStream for reading the contents of the given entry.For archives using solid compression randomly accessing entries will be significantly slower than reading the archive sequentially.
- Parameters:
entry
- the entry to get the stream for.- Returns:
- a stream to read the entry from.
- Throws:
IOException
- if unable to create an input stream from the zipentry- Since:
- Compress 1.20
-
read
Reads data into an array of bytes.- Parameters:
b
- the array to write data to- Returns:
- the number of bytes read, or -1 if end of input is reached
- Throws:
IOException
- if an I/O error has occurred
-
read
Reads data into an array of bytes.- Parameters:
b
- the array to write data tooff
- offset into the buffer to start filling atlen
- of bytes to read- Returns:
- the number of bytes read, or -1 if end of input is reached
- Throws:
IOException
- if an I/O error has occurred
-
getStatisticsForCurrentEntry
Provides statistics for bytes read from the current entry.- Returns:
- statistics for bytes read from the current entry
- Since:
- 1.17
-
readUint64
- Throws:
IOException
-
getChar
- Throws:
IOException
-
getInt
- Throws:
IOException
-
getLong
- Throws:
IOException
-
get
- Throws:
IOException
-
getUnsignedByte
- Throws:
IOException
-
matches
public static boolean matches(byte[] signature, int length) Checks if the signature matches what is expected for a 7z file.- Parameters:
signature
- the bytes to checklength
- the number of bytes to check- Returns:
- true, if this is the signature of a 7z archive.
- Since:
- 1.8
-
skipBytesFully
- Throws:
IOException
-
readFully
- Throws:
IOException
-
toString
-
getDefaultName
Derives a default file name from the archive name - if known.This implements the same heuristics the 7z tools use. In 7z's case if an archive contains entries without a name - i.e.
SevenZArchiveEntry.getName()
returnsnull
- then its command line and GUI tools will use this default name when extracting the entries.- Returns:
- null if the name of the archive is unknown. Otherwise
if the name of the archive has got any extension, it is
stripped and the remainder returned. Finally if the name of the
archive hasn't got any extension then a
~
character is appended to the archive name. - Since:
- 1.19
-
utf16Decode
- Throws:
IOException
-
assertFitsIntoNonNegativeInt
- Throws:
IOException
-