Class SevenZFile
- java.lang.Object
-
- org.apache.commons.compress.archivers.sevenz.SevenZFile
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public class SevenZFile extends java.lang.Object implements java.io.Closeable
Reads a 7z file, using SeekableByteChannel under the covers.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
-
-
Field Summary
Fields Modifier and Type Field Description private Archive
archive
private java.nio.channels.SeekableByteChannel
channel
private long
compressedBytesReadFromCurrentEntry
private int
currentEntryIndex
private int
currentFolderIndex
private java.io.InputStream
currentFolderInputStream
private static java.lang.String
DEFAULT_FILE_NAME
private java.util.ArrayList<java.io.InputStream>
deferredBlockStreams
private java.lang.String
fileName
private SevenZFileOptions
options
private byte[]
password
private static java.nio.charset.CharsetEncoder
PASSWORD_ENCODER
(package private) static byte[]
sevenZSignature
(package private) static int
SIGNATURE_HEADER_SIZE
private long
uncompressedBytesReadFromCurrentEntry
-
Constructor Summary
Constructors Modifier Constructor Description SevenZFile(java.io.File fileName)
Reads a file as unencrypted 7z archiveSevenZFile(java.io.File fileName, byte[] password)
Deprecated.use the char[]-arg version for the password insteadSevenZFile(java.io.File fileName, char[] password)
Reads a file as 7z archiveSevenZFile(java.io.File fileName, char[] password, SevenZFileOptions options)
Reads a file as 7z archive with additional options.SevenZFile(java.io.File fileName, SevenZFileOptions options)
Reads a file as unencrypted 7z archiveSevenZFile(java.nio.channels.SeekableByteChannel channel)
Reads a SeekableByteChannel as 7z archiveSevenZFile(java.nio.channels.SeekableByteChannel channel, byte[] password)
Deprecated.use the char[]-arg version for the password insteadSevenZFile(java.nio.channels.SeekableByteChannel channel, char[] password)
Reads a SeekableByteChannel as 7z archiveSevenZFile(java.nio.channels.SeekableByteChannel channel, char[] password, SevenZFileOptions options)
Reads a SeekableByteChannel as 7z archive with additional options.SevenZFile(java.nio.channels.SeekableByteChannel channel, java.lang.String fileName)
Reads a SeekableByteChannel as 7z archiveSevenZFile(java.nio.channels.SeekableByteChannel channel, java.lang.String fileName, byte[] password)
Deprecated.use the char[]-arg version for the password insteadprivate
SevenZFile(java.nio.channels.SeekableByteChannel channel, java.lang.String filename, byte[] password, boolean closeOnError, SevenZFileOptions options)
SevenZFile(java.nio.channels.SeekableByteChannel channel, java.lang.String fileName, char[] password)
Reads a SeekableByteChannel as 7z archiveSevenZFile(java.nio.channels.SeekableByteChannel channel, java.lang.String fileName, char[] password, SevenZFileOptions options)
Reads a SeekableByteChannel as 7z archive with addtional options.SevenZFile(java.nio.channels.SeekableByteChannel channel, java.lang.String fileName, SevenZFileOptions options)
Reads a SeekableByteChannel as 7z archive with additional options.SevenZFile(java.nio.channels.SeekableByteChannel channel, SevenZFileOptions options)
Reads a SeekableByteChannel as 7z archive with addtional options.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static void
assertFitsIntoInt(java.lang.String what, long value)
private java.io.InputStream
buildDecoderStack(Folder folder, long folderOffset, int firstPackStreamIndex, SevenZArchiveEntry entry)
private void
buildDecodingStream()
private void
calculateStreamMap(Archive archive)
void
close()
Closes the archive.private java.io.InputStream
getCurrentStream()
java.lang.String
getDefaultName()
Derives a default file name from the archive name - if known.java.lang.Iterable<SevenZArchiveEntry>
getEntries()
Returns meta-data of all archive entries.SevenZArchiveEntry
getNextEntry()
Returns the next Archive Entry in this archive.InputStreamStatistics
getStatisticsForCurrentEntry()
Provides statistics for bytes read from the current entry.private static int
getUnsignedByte(java.nio.ByteBuffer buf)
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 java.util.BitSet
readAllOrBits(java.nio.ByteBuffer header, int size)
private void
readArchiveProperties(java.nio.ByteBuffer input)
private java.util.BitSet
readBits(java.nio.ByteBuffer header, int size)
private java.nio.ByteBuffer
readEncodedHeader(java.nio.ByteBuffer header, Archive archive, byte[] password)
private void
readFilesInfo(java.nio.ByteBuffer header, Archive archive)
private Folder
readFolder(java.nio.ByteBuffer header)
private void
readFully(java.nio.ByteBuffer buf)
private void
readHeader(java.nio.ByteBuffer header, Archive archive)
private Archive
readHeaders(byte[] password)
private void
readPackInfo(java.nio.ByteBuffer header, Archive archive)
private StartHeader
readStartHeader(long startHeaderCrc)
private void
readStreamsInfo(java.nio.ByteBuffer header, Archive archive)
private void
readSubStreamsInfo(java.nio.ByteBuffer header, Archive archive)
private static long
readUint64(java.nio.ByteBuffer in)
private void
readUnpackInfo(java.nio.ByteBuffer header, Archive archive)
private static long
skipBytesFully(java.nio.ByteBuffer input, long bytesToSkip)
java.lang.String
toString()
private static byte[]
utf16Decode(char[] chars)
-
-
-
Field Detail
-
SIGNATURE_HEADER_SIZE
static final int SIGNATURE_HEADER_SIZE
- See Also:
- Constant Field Values
-
DEFAULT_FILE_NAME
private static final java.lang.String DEFAULT_FILE_NAME
- See Also:
- Constant Field Values
-
fileName
private final java.lang.String fileName
-
channel
private java.nio.channels.SeekableByteChannel channel
-
archive
private final Archive archive
-
currentEntryIndex
private int currentEntryIndex
-
currentFolderIndex
private int currentFolderIndex
-
currentFolderInputStream
private java.io.InputStream currentFolderInputStream
-
password
private byte[] password
-
options
private final SevenZFileOptions options
-
compressedBytesReadFromCurrentEntry
private long compressedBytesReadFromCurrentEntry
-
uncompressedBytesReadFromCurrentEntry
private long uncompressedBytesReadFromCurrentEntry
-
deferredBlockStreams
private final java.util.ArrayList<java.io.InputStream> deferredBlockStreams
-
sevenZSignature
static final byte[] sevenZSignature
-
PASSWORD_ENCODER
private static final java.nio.charset.CharsetEncoder PASSWORD_ENCODER
-
-
Constructor Detail
-
SevenZFile
public SevenZFile(java.io.File fileName, char[] password) throws java.io.IOException
Reads a file as 7z archive- Parameters:
fileName
- the file to readpassword
- optional password if the archive is encrypted- Throws:
java.io.IOException
- if reading the archive fails- Since:
- 1.17
-
SevenZFile
public SevenZFile(java.io.File fileName, char[] password, SevenZFileOptions options) throws java.io.IOException
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:
java.io.IOException
- if reading the archive fails or the memory limit (if set) is too small- Since:
- 1.19
-
SevenZFile
public SevenZFile(java.io.File fileName, byte[] password) throws java.io.IOException
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:
java.io.IOException
- if reading the archive fails
-
SevenZFile
public SevenZFile(java.nio.channels.SeekableByteChannel channel) throws java.io.IOException
Reads a SeekableByteChannel as 7z archiveSeekableInMemoryByteChannel
allows you to read from an in-memory archive.- Parameters:
channel
- the channel to read- Throws:
java.io.IOException
- if reading the archive fails- Since:
- 1.13
-
SevenZFile
public SevenZFile(java.nio.channels.SeekableByteChannel channel, SevenZFileOptions options) throws java.io.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 readoptions
- the options to apply- Throws:
java.io.IOException
- if reading the archive fails or the memory limit (if set) is too small- Since:
- 1.19
-
SevenZFile
public SevenZFile(java.nio.channels.SeekableByteChannel channel, char[] password) throws java.io.IOException
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:
java.io.IOException
- if reading the archive fails- Since:
- 1.17
-
SevenZFile
public SevenZFile(java.nio.channels.SeekableByteChannel channel, char[] password, SevenZFileOptions options) throws java.io.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:
java.io.IOException
- if reading the archive fails or the memory limit (if set) is too small- Since:
- 1.19
-
SevenZFile
public SevenZFile(java.nio.channels.SeekableByteChannel channel, java.lang.String fileName, char[] password) throws java.io.IOException
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:
java.io.IOException
- if reading the archive fails- Since:
- 1.17
-
SevenZFile
public SevenZFile(java.nio.channels.SeekableByteChannel channel, java.lang.String fileName, char[] password, SevenZFileOptions options) throws java.io.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:
java.io.IOException
- if reading the archive fails or the memory limit (if set) is too small- Since:
- 1.19
-
SevenZFile
public SevenZFile(java.nio.channels.SeekableByteChannel channel, java.lang.String fileName) throws java.io.IOException
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:
java.io.IOException
- if reading the archive fails- Since:
- 1.17
-
SevenZFile
public SevenZFile(java.nio.channels.SeekableByteChannel channel, java.lang.String fileName, SevenZFileOptions options) throws java.io.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:
java.io.IOException
- if reading the archive fails or the memory limit (if set) is too small- Since:
- 1.19
-
SevenZFile
public SevenZFile(java.nio.channels.SeekableByteChannel channel, byte[] password) throws java.io.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 readpassword
- optional password if the archive is encrypted - the byte array is supposed to be the UTF16-LE encoded representation of the password.- Throws:
java.io.IOException
- if reading the archive fails- Since:
- 1.13
-
SevenZFile
public SevenZFile(java.nio.channels.SeekableByteChannel channel, java.lang.String fileName, byte[] password) throws java.io.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:
java.io.IOException
- if reading the archive fails- Since:
- 1.13
-
SevenZFile
private SevenZFile(java.nio.channels.SeekableByteChannel channel, java.lang.String filename, byte[] password, boolean closeOnError, SevenZFileOptions options) throws java.io.IOException
- Throws:
java.io.IOException
-
SevenZFile
public SevenZFile(java.io.File fileName) throws java.io.IOException
Reads a file as unencrypted 7z archive- Parameters:
fileName
- the file to read- Throws:
java.io.IOException
- if reading the archive fails
-
SevenZFile
public SevenZFile(java.io.File fileName, SevenZFileOptions options) throws java.io.IOException
Reads a file as unencrypted 7z archive- Parameters:
fileName
- the file to readoptions
- the options to apply- Throws:
java.io.IOException
- if reading the archive fails or the memory limit (if set) is too small- Since:
- 1.19
-
-
Method Detail
-
close
public void close() throws java.io.IOException
Closes the archive.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
- if closing the file fails
-
getNextEntry
public SevenZArchiveEntry getNextEntry() throws java.io.IOException
Returns the next Archive Entry in this archive.- Returns:
- the next entry,
or
null
if there are no more entries - Throws:
java.io.IOException
- if the next entry could not be read
-
getEntries
public java.lang.Iterable<SevenZArchiveEntry> getEntries()
Returns 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:
- meta-data of all archive entries.
- Since:
- 1.11
-
readHeaders
private Archive readHeaders(byte[] password) throws java.io.IOException
- Throws:
java.io.IOException
-
readStartHeader
private StartHeader readStartHeader(long startHeaderCrc) throws java.io.IOException
- Throws:
java.io.IOException
-
readHeader
private void readHeader(java.nio.ByteBuffer header, Archive archive) throws java.io.IOException
- Throws:
java.io.IOException
-
readArchiveProperties
private void readArchiveProperties(java.nio.ByteBuffer input) throws java.io.IOException
- Throws:
java.io.IOException
-
readEncodedHeader
private java.nio.ByteBuffer readEncodedHeader(java.nio.ByteBuffer header, Archive archive, byte[] password) throws java.io.IOException
- Throws:
java.io.IOException
-
readStreamsInfo
private void readStreamsInfo(java.nio.ByteBuffer header, Archive archive) throws java.io.IOException
- Throws:
java.io.IOException
-
readPackInfo
private void readPackInfo(java.nio.ByteBuffer header, Archive archive) throws java.io.IOException
- Throws:
java.io.IOException
-
readUnpackInfo
private void readUnpackInfo(java.nio.ByteBuffer header, Archive archive) throws java.io.IOException
- Throws:
java.io.IOException
-
readSubStreamsInfo
private void readSubStreamsInfo(java.nio.ByteBuffer header, Archive archive) throws java.io.IOException
- Throws:
java.io.IOException
-
readFolder
private Folder readFolder(java.nio.ByteBuffer header) throws java.io.IOException
- Throws:
java.io.IOException
-
readAllOrBits
private java.util.BitSet readAllOrBits(java.nio.ByteBuffer header, int size) throws java.io.IOException
- Throws:
java.io.IOException
-
readBits
private java.util.BitSet readBits(java.nio.ByteBuffer header, int size) throws java.io.IOException
- Throws:
java.io.IOException
-
readFilesInfo
private void readFilesInfo(java.nio.ByteBuffer header, Archive archive) throws java.io.IOException
- Throws:
java.io.IOException
-
calculateStreamMap
private void calculateStreamMap(Archive archive) throws java.io.IOException
- Throws:
java.io.IOException
-
buildDecodingStream
private void buildDecodingStream() throws java.io.IOException
- Throws:
java.io.IOException
-
buildDecoderStack
private java.io.InputStream buildDecoderStack(Folder folder, long folderOffset, int firstPackStreamIndex, SevenZArchiveEntry entry) throws java.io.IOException
- Throws:
java.io.IOException
-
read
public int read() throws java.io.IOException
Reads a byte of data.- Returns:
- the byte read, or -1 if end of input is reached
- Throws:
java.io.IOException
- if an I/O error has occurred
-
getCurrentStream
private java.io.InputStream getCurrentStream() throws java.io.IOException
- Throws:
java.io.IOException
-
read
public int read(byte[] b) throws java.io.IOException
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:
java.io.IOException
- if an I/O error has occurred
-
read
public int read(byte[] b, int off, int len) throws java.io.IOException
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:
java.io.IOException
- if an I/O error has occurred
-
getStatisticsForCurrentEntry
public InputStreamStatistics getStatisticsForCurrentEntry()
Provides statistics for bytes read from the current entry.- Returns:
- statistics for bytes read from the current entry
- Since:
- 1.17
-
readUint64
private static long readUint64(java.nio.ByteBuffer in) throws java.io.IOException
- Throws:
java.io.IOException
-
getUnsignedByte
private static int getUnsignedByte(java.nio.ByteBuffer buf)
-
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
private static long skipBytesFully(java.nio.ByteBuffer input, long bytesToSkip) throws java.io.IOException
- Throws:
java.io.IOException
-
readFully
private void readFully(java.nio.ByteBuffer buf) throws java.io.IOException
- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
getDefaultName
public java.lang.String 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
private static byte[] utf16Decode(char[] chars) throws java.io.IOException
- Throws:
java.io.IOException
-
assertFitsIntoInt
private static void assertFitsIntoInt(java.lang.String what, long value) throws java.io.IOException
- Throws:
java.io.IOException
-
-