public class SevenZFile
extends java.lang.Object
implements java.io.Closeable
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.
Modifier and Type | Field and 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 java.util.ArrayList<java.io.InputStream> |
deferredBlockStreams |
private java.lang.String |
fileName |
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 |
Modifier | Constructor and Description |
---|---|
|
SevenZFile(java.io.File filename)
Reads a file as unencrypted 7z archive
|
|
SevenZFile(java.io.File filename,
byte[] password)
Deprecated.
use the char[]-arg version for the password instead
|
|
SevenZFile(java.io.File filename,
char[] password)
Reads a file as 7z archive
|
|
SevenZFile(java.nio.channels.SeekableByteChannel channel)
Reads a SeekableByteChannel as 7z archive
|
|
SevenZFile(java.nio.channels.SeekableByteChannel channel,
byte[] password)
Deprecated.
use the char[]-arg version for the password instead
|
|
SevenZFile(java.nio.channels.SeekableByteChannel channel,
char[] password)
Reads a SeekableByteChannel as 7z archive
|
|
SevenZFile(java.nio.channels.SeekableByteChannel channel,
java.lang.String filename)
Reads a SeekableByteChannel as 7z archive
|
|
SevenZFile(java.nio.channels.SeekableByteChannel channel,
java.lang.String filename,
byte[] password)
Deprecated.
use the char[]-arg version for the password instead
|
private |
SevenZFile(java.nio.channels.SeekableByteChannel channel,
java.lang.String filename,
byte[] password,
boolean closeOnError) |
|
SevenZFile(java.nio.channels.SeekableByteChannel channel,
java.lang.String filename,
char[] password)
Reads a SeekableByteChannel as 7z archive
|
Modifier and Type | Method and Description |
---|---|
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.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) |
static final int SIGNATURE_HEADER_SIZE
private final java.lang.String fileName
private java.nio.channels.SeekableByteChannel channel
private final Archive archive
private int currentEntryIndex
private int currentFolderIndex
private java.io.InputStream currentFolderInputStream
private byte[] password
private long compressedBytesReadFromCurrentEntry
private long uncompressedBytesReadFromCurrentEntry
private final java.util.ArrayList<java.io.InputStream> deferredBlockStreams
static final byte[] sevenZSignature
private static final java.nio.charset.CharsetEncoder PASSWORD_ENCODER
public SevenZFile(java.io.File filename, char[] password) throws java.io.IOException
filename
- the file to readpassword
- optional password if the archive is encryptedjava.io.IOException
- if reading the archive failspublic SevenZFile(java.io.File filename, byte[] password) throws java.io.IOException
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.java.io.IOException
- if reading the archive failspublic SevenZFile(java.nio.channels.SeekableByteChannel channel) throws java.io.IOException
SeekableInMemoryByteChannel
allows you to read from an in-memory archive.
channel
- the channel to readjava.io.IOException
- if reading the archive failspublic SevenZFile(java.nio.channels.SeekableByteChannel channel, char[] password) throws java.io.IOException
SeekableInMemoryByteChannel
allows you to read from an in-memory archive.
channel
- the channel to readpassword
- optional password if the archive is encryptedjava.io.IOException
- if reading the archive failspublic SevenZFile(java.nio.channels.SeekableByteChannel channel, java.lang.String filename, char[] password) throws java.io.IOException
SeekableInMemoryByteChannel
allows you to read from an in-memory archive.
channel
- the channel to readfilename
- name of the archive - only used for error reportingpassword
- optional password if the archive is encryptedjava.io.IOException
- if reading the archive failspublic SevenZFile(java.nio.channels.SeekableByteChannel channel, java.lang.String filename) throws java.io.IOException
SeekableInMemoryByteChannel
allows you to read from an in-memory archive.
channel
- the channel to readfilename
- name of the archive - only used for error reportingjava.io.IOException
- if reading the archive failspublic SevenZFile(java.nio.channels.SeekableByteChannel channel, byte[] password) throws java.io.IOException
SeekableInMemoryByteChannel
allows you to read from an in-memory archive.
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.java.io.IOException
- if reading the archive failspublic SevenZFile(java.nio.channels.SeekableByteChannel channel, java.lang.String filename, byte[] password) throws java.io.IOException
SeekableInMemoryByteChannel
allows you to read from an in-memory archive.
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.java.io.IOException
- if reading the archive failsprivate SevenZFile(java.nio.channels.SeekableByteChannel channel, java.lang.String filename, byte[] password, boolean closeOnError) throws java.io.IOException
java.io.IOException
public SevenZFile(java.io.File filename) throws java.io.IOException
filename
- the file to readjava.io.IOException
- if reading the archive failspublic void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException
- if closing the file failspublic SevenZArchiveEntry getNextEntry() throws java.io.IOException
null
if there are no more entriesjava.io.IOException
- if the next entry could not be readpublic java.lang.Iterable<SevenZArchiveEntry> getEntries()
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()
.
private Archive readHeaders(byte[] password) throws java.io.IOException
java.io.IOException
private StartHeader readStartHeader(long startHeaderCrc) throws java.io.IOException
java.io.IOException
private void readHeader(java.nio.ByteBuffer header, Archive archive) throws java.io.IOException
java.io.IOException
private void readArchiveProperties(java.nio.ByteBuffer input) throws java.io.IOException
java.io.IOException
private java.nio.ByteBuffer readEncodedHeader(java.nio.ByteBuffer header, Archive archive, byte[] password) throws java.io.IOException
java.io.IOException
private void readStreamsInfo(java.nio.ByteBuffer header, Archive archive) throws java.io.IOException
java.io.IOException
private void readPackInfo(java.nio.ByteBuffer header, Archive archive) throws java.io.IOException
java.io.IOException
private void readUnpackInfo(java.nio.ByteBuffer header, Archive archive) throws java.io.IOException
java.io.IOException
private void readSubStreamsInfo(java.nio.ByteBuffer header, Archive archive) throws java.io.IOException
java.io.IOException
private Folder readFolder(java.nio.ByteBuffer header) throws java.io.IOException
java.io.IOException
private java.util.BitSet readAllOrBits(java.nio.ByteBuffer header, int size) throws java.io.IOException
java.io.IOException
private java.util.BitSet readBits(java.nio.ByteBuffer header, int size) throws java.io.IOException
java.io.IOException
private void readFilesInfo(java.nio.ByteBuffer header, Archive archive) throws java.io.IOException
java.io.IOException
private void calculateStreamMap(Archive archive) throws java.io.IOException
java.io.IOException
private void buildDecodingStream() throws java.io.IOException
java.io.IOException
private java.io.InputStream buildDecoderStack(Folder folder, long folderOffset, int firstPackStreamIndex, SevenZArchiveEntry entry) throws java.io.IOException
java.io.IOException
public int read() throws java.io.IOException
java.io.IOException
- if an I/O error has occurredprivate java.io.InputStream getCurrentStream() throws java.io.IOException
java.io.IOException
public int read(byte[] b) throws java.io.IOException
b
- the array to write data tojava.io.IOException
- if an I/O error has occurredpublic int read(byte[] b, int off, int len) throws java.io.IOException
b
- the array to write data tooff
- offset into the buffer to start filling atlen
- of bytes to readjava.io.IOException
- if an I/O error has occurredpublic InputStreamStatistics getStatisticsForCurrentEntry()
private static long readUint64(java.nio.ByteBuffer in) throws java.io.IOException
java.io.IOException
private static int getUnsignedByte(java.nio.ByteBuffer buf)
public static boolean matches(byte[] signature, int length)
signature
- the bytes to checklength
- the number of bytes to checkprivate static long skipBytesFully(java.nio.ByteBuffer input, long bytesToSkip) throws java.io.IOException
java.io.IOException
private void readFully(java.nio.ByteBuffer buf) throws java.io.IOException
java.io.IOException
public java.lang.String toString()
toString
in class java.lang.Object
private static byte[] utf16Decode(char[] chars) throws java.io.IOException
java.io.IOException