Top Description Inners Fields Constructors Methods
org.apache.commons.compress.archivers.sevenz

public Class SevenZFile

extends Object
implements Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable
Static Imports
java.nio.charset.StandardCharsets.UTF_16LE

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

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
public static class
SevenZFile.Builder

Builds new instances of SevenZFile.

Field Summary

Modifier and TypeField and Description
private 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 int
private byte[]
pack-priv static final byte[]
sevenZSignature

Shared with SevenZOutputFile and tests, neither mutates it.

pack-priv static final int
private final boolean
private long
private final boolean

Constructor Summary

AccessConstructor and Description
public
SevenZFile(final File
the file to read.
fileName
)

Deprecated Use Builder#get().
Reads a file as unencrypted 7z archive.
public
SevenZFile(final File
the file to read
file
,
final byte[]
optional password if the archive is encrypted - the byte array is supposed to be the UTF16-LE encoded representation of the password.
password
)

Deprecated Use Builder#get().
Reads a file as 7z archive
public
SevenZFile(final File
the file to read
file
,
final char[]
optional password if the archive is encrypted
password
)

Deprecated Use Builder#get().
Reads a file as 7z archive
public
SevenZFile(final File
the file to read
file
,
final char[]
optional password if the archive is encrypted
password
,
final SevenZFileOptions
the options to apply
options
)

Deprecated Use Builder#get().
Reads a file as 7z archive with additional options.
public
SevenZFile(final File
the file to read
file
,
final SevenZFileOptions
the options to apply
options
)

Deprecated Use Builder#get().
Reads a file as unencrypted 7z archive
public
SevenZFile(final SeekableByteChannel
the channel to read
channel
)

Deprecated Use Builder#get().
Reads a SeekableByteChannel as 7z archive

org.apache.commons.compress.utils.SeekableInMemoryByteChannel allows you to read from an in-memory archive.

public
SevenZFile(final SeekableByteChannel
the channel to read
channel
,
final byte[]
optional password if the archive is encrypted - the byte array is supposed to be the UTF16-LE encoded representation of the password.
password
)

Deprecated Use Builder#get().
Reads a SeekableByteChannel as 7z archive

org.apache.commons.compress.utils.SeekableInMemoryByteChannel allows you to read from an in-memory archive.

public
SevenZFile(final SeekableByteChannel
the channel to read
channel
,
final char[]
optional password if the archive is encrypted
password
)

Deprecated Use Builder#get().
Reads a SeekableByteChannel as 7z archive

org.apache.commons.compress.utils.SeekableInMemoryByteChannel allows you to read from an in-memory archive.

public
SevenZFile(final SeekableByteChannel
the channel to read
channel
,
final char[]
optional password if the archive is encrypted
password
,
final SevenZFileOptions
the options to apply
options
)

Deprecated Use Builder#get().
Reads a SeekableByteChannel as 7z archive with additional options.
public
SevenZFile(final SeekableByteChannel
the channel to read
channel
,
final SevenZFileOptions
the options to apply
options
)

Deprecated Use Builder#get().
Reads a SeekableByteChannel as 7z archive with additional options.
public
SevenZFile(final SeekableByteChannel
the channel to read
channel
,
final String
name of the archive - only used for error reporting
fileName
)

Deprecated Use Builder#get().
Reads a SeekableByteChannel as 7z archive

org.apache.commons.compress.utils.SeekableInMemoryByteChannel allows you to read from an in-memory archive.

public
SevenZFile(final SeekableByteChannel
the channel to read
channel
,
final String
name of the archive - only used for error reporting
fileName
,
final byte[]
optional password if the archive is encrypted - the byte array is supposed to be the UTF16-LE encoded representation of the password.
password
)

Deprecated Use Builder#get().
Reads a SeekableByteChannel as 7z archive

org.apache.commons.compress.utils.SeekableInMemoryByteChannel allows you to read from an in-memory archive.

private
SevenZFile(final SeekableByteChannel channel, final String fileName, final byte[] password, final boolean closeOnError, final int maxMemoryLimitKb, final boolean useDefaultNameForUnnamedEntries, final boolean tryToRecoverBrokenArchives)

private
SevenZFile(final SeekableByteChannel
the channel to read.
channel
,
final String
name of the archive - only used for error reporting.
fileName
,
final byte[]
optional password if the archive is encrypted.
password
,
final boolean
closes the channel on error.
closeOnError
,
final SevenZFileOptions
options.
options
)

Deprecated Use Builder#get().
Constructs a new instance.
public
SevenZFile(final SeekableByteChannel
the channel to read
channel
,
final String
name of the archive - only used for error reporting
fileName
,
final char[]
optional password if the archive is encrypted
password
)

Deprecated Use Builder#get().
Reads a SeekableByteChannel as 7z archive

org.apache.commons.compress.utils.SeekableInMemoryByteChannel allows you to read from an in-memory archive.

public
SevenZFile(final SeekableByteChannel
the channel to read
channel
,
final String
name of the archive - only used for error reporting
fileName
,
final char[]
optional password if the archive is encrypted
password
,
final SevenZFileOptions
the options to apply
options
)

Deprecated Use Builder#get().
Reads a SeekableByteChannel as 7z archive with additional options.
public
SevenZFile(final SeekableByteChannel
the channel to read
channel
,
final String
name of the archive - only used for error reporting
fileName
,
final SevenZFileOptions
the options to apply
options
)

Deprecated Use Builder#get().
Reads a SeekableByteChannel as 7z archive with additional options.

Method Summary

Modifier and TypeMethod and Description
private static int
assertFitsIntoNonNegativeInt(final String what, final long value)

private InputStream
buildDecoderStack(final Folder folder, final long folderOffset, final int firstPackStreamIndex, final SevenZArchiveEntry entry)

private void
buildDecodingStream(final int
the index of the entry to be read
entryIndex
,
final boolean
is this called in a random access
isRandomAccess
)

Builds the decoding stream for the entry to be read.

public static SevenZFile.Builder

Returns:

a new Builder.
builder
()

Creates a new Builder.

private void
calculateStreamMap(final Archive archive)

private static ByteBuffer
checkEndOfFile(final ByteBuffer buf, final int expectRemaining)

private void
checkEntryIsInitialized(final Map<Integer, SevenZArchiveEntry> archiveEntries, final int index)

public void
close()

Implements java.io.Closeable.close.

Closes the archive.
private static void
get(final ByteBuffer buf, final byte[] to)

private static char
getChar(final ByteBuffer buf)

private InputStream
public String

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.
getDefaultName
()

Gets a default file name from the archive name - if known.

public Iterable<SevenZArchiveEntry>

Returns:

a copy of meta-data of all archive entries.
getEntries
()

Gets a copy of meta-data of all archive entries.

public InputStream

Returns:

a stream to read the entry from.
getInputStream
(final SevenZArchiveEntry
the entry to get the stream for.
entry
)

Gets an InputStream for reading the contents of the given entry.

private static int
getInt(final ByteBuffer buf)

private static long
getLong(final ByteBuffer buf)

public SevenZArchiveEntry

Returns:

the next entry, or null if there are no more entries
getNextEntry
()

Gets the next Archive Entry in this archive.

public InputStreamStatistics

Returns:

statistics for bytes read from the current entry
getStatisticsForCurrentEntry
()

Gets statistics for bytes read from the current entry.

private static int
private boolean

Returns:

true if any data of current entry has been read
hasCurrentEntryBeenRead
()

Tests if any data of current entry has been read or not.

private Archive
initializeArchive(final StartHeader startHeader, final byte[] password, final boolean verifyCrc)

public static boolean

Returns:

true, if this is the signature of a 7z archive.
matches
(final byte[]
the bytes to check
signature
,
final int
the number of bytes to check
length
)

Checks if the signature matches what is expected for a 7z file.

private static SeekableByteChannel
newByteChannel(final File file)

public int

Returns:

the byte read, or -1 if end of input is reached
read
()

Reads a byte of data.

public int

Returns:

the number of bytes read, or -1 if end of input is reached
read
(final byte[]
the array to write data to
b
)

Reads data into an array of bytes.

public int

Returns:

the number of bytes read, or -1 if end of input is reached
read
(final byte[]
the array to write data to
b
,
final int
offset into the buffer to start filling at
off
,
final int
of bytes to read
len
)

Reads data into an array of bytes.

private BitSet
readAllOrBits(final ByteBuffer header, final int size)

private void
private BitSet
readBits(final ByteBuffer header, final int size)

private ByteBuffer
readEncodedHeader(final ByteBuffer header, final Archive archive, final byte[] password)

private void
readFilesInfo(final ByteBuffer header, final Archive archive)

private Folder
readFolder(final ByteBuffer header)

private void
readFully(final ByteBuffer buf)

private void
readHeader(final ByteBuffer header, final Archive archive)

private Archive
readHeaders(final byte[] password)

private void
readPackInfo(final ByteBuffer header, final Archive archive)

private StartHeader
readStartHeader(final long startHeaderCrc)

private void
readStreamsInfo(final ByteBuffer header, final Archive archive)

private void
readSubStreamsInfo(final ByteBuffer header, final Archive archive)

private static long
private void
readUnpackInfo(final ByteBuffer header, final Archive archive)

private void
reopenFolderInputStream(final int
the index of the folder to reopen
folderIndex
,
final SevenZArchiveEntry
the 7z entry to read
file
)

Discard any queued streams/ folder stream, and reopen the current folder input stream.

private SevenZFile.ArchiveStatistics
private void
private void
private int
private void
private void
private void
private void
private static long
skipBytesFully(final ByteBuffer input, long bytesToSkip)

private boolean

Returns:

true if there are entries actually skipped
skipEntriesWhenNeeded
(final int
the entry to be read
entryIndex
,
final boolean
are the entry to be read and the current entry in the same folder
isInSameFolder
,
final int
the index of the folder which contains the entry
folderIndex
)

Skips all the entries if needed.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
private Archive
tryToLocateEndHeader(final byte[] password)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait