Package com.github.junrar
Class Archive
- java.lang.Object
-
- com.github.junrar.Archive
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,java.lang.Iterable<FileHeader>
public class Archive extends java.lang.Object implements java.io.Closeable, java.lang.Iterable<FileHeader>
The Main Rar Class; represents a rar Archive- Version:
- $LastChangedRevision$
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classArchive.EmptyInputStreamAn emptyInputStream.private static classArchive.ExtractorExecutorHolderClass to ensure the lazy initialization of theThreadPoolExecutorupon first usage.
-
Field Summary
Fields Modifier and Type Field Description private SeekableReadOnlyByteChannelchannelprivate intcurrentHeaderIndexprivate ComprDataIOdataIOprivate java.util.List<BaseBlock>headersprivate static org.slf4j.Loggerloggerprivate MarkHeadermarkHeadprivate static intMAX_HEADER_SIZEprivate MainHeadernewMhdprivate FileHeadernextFileHeaderprivate java.lang.Stringpasswordprivate static intPIPE_BUFFER_SIZEprivate longtotalPackedReadNumber of bytes of compressed data read from current file.private longtotalPackedSizeSize of packed data in current file.private Unpackunpackprivate UnrarCallbackunrarCallbackprivate static booleanUSE_EXECUTORprivate Volumevolumeprivate VolumeManagervolumeManager
-
Constructor Summary
Constructors Constructor Description Archive(VolumeManager volumeManager, UnrarCallback unrarCallback, java.lang.String password)Archive(java.io.File firstVolume)Archive(java.io.File firstVolume, UnrarCallback unrarCallback)Archive(java.io.File firstVolume, UnrarCallback unrarCallback, java.lang.String password)Archive(java.io.File firstVolume, java.lang.String password)Archive(java.io.InputStream rarAsStream)Archive(java.io.InputStream rarAsStream, UnrarCallback unrarCallback)Archive(java.io.InputStream rarAsStream, UnrarCallback unrarCallback, java.lang.String password)Archive(java.io.InputStream rarAsStream, java.lang.String password)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbytesReadRead(int count)voidclose()Close the underlying compressed file.private voiddoExtractFile(FileHeader hd, java.io.OutputStream os)voidextractFile(FileHeader hd, java.io.OutputStream os)Extract the file specified by the given header and write it to the supplied output streamSeekableReadOnlyByteChannelgetChannel()java.util.List<FileHeader>getFileHeaders()java.util.List<BaseBlock>getHeaders()Gets all of the headers in the archive.java.io.InputStreamgetInputStream(FileHeader hd)Returns anInputStreamthat will allow to read the file and stream it.MainHeadergetMainHeader()java.lang.StringgetPassword()private static <T> TgetPropertyAs(java.lang.String key, java.util.function.Function<java.lang.String,T> function, T defaultValue)UnrarCallbackgetUnrarCallback()VolumegetVolume()VolumeManagergetVolumeManager()booleanisEncrypted()booleanisOldFormat()booleanisPasswordProtected()java.util.Iterator<FileHeader>iterator()FileHeadernextFileHeader()private voidreadHeaders(long fileLength)Read the headers of the archiveprivate static byte[]safelyAllocate(long len, int maxSize)private voidsetChannel(SeekableReadOnlyByteChannel channel, long length)voidsetPassword(java.lang.String password)voidsetVolume(Volume volume)voidsetVolumeManager(VolumeManager volumeManager)
-
-
-
Field Detail
-
logger
private static final org.slf4j.Logger logger
-
MAX_HEADER_SIZE
private static final int MAX_HEADER_SIZE
- See Also:
- Constant Field Values
-
PIPE_BUFFER_SIZE
private static final int PIPE_BUFFER_SIZE
-
USE_EXECUTOR
private static final boolean USE_EXECUTOR
-
channel
private SeekableReadOnlyByteChannel channel
-
unrarCallback
private final UnrarCallback unrarCallback
-
dataIO
private final ComprDataIO dataIO
-
headers
private final java.util.List<BaseBlock> headers
-
markHead
private MarkHeader markHead
-
newMhd
private MainHeader newMhd
-
unpack
private Unpack unpack
-
currentHeaderIndex
private int currentHeaderIndex
-
totalPackedSize
private long totalPackedSize
Size of packed data in current file.
-
totalPackedRead
private long totalPackedRead
Number of bytes of compressed data read from current file.
-
volumeManager
private VolumeManager volumeManager
-
volume
private Volume volume
-
nextFileHeader
private FileHeader nextFileHeader
-
password
private java.lang.String password
-
-
Constructor Detail
-
Archive
public Archive(VolumeManager volumeManager, UnrarCallback unrarCallback, java.lang.String password) throws RarException, java.io.IOException
- Throws:
RarExceptionjava.io.IOException
-
Archive
public Archive(java.io.File firstVolume) throws RarException, java.io.IOException- Throws:
RarExceptionjava.io.IOException
-
Archive
public Archive(java.io.File firstVolume, UnrarCallback unrarCallback) throws RarException, java.io.IOException- Throws:
RarExceptionjava.io.IOException
-
Archive
public Archive(java.io.File firstVolume, java.lang.String password) throws RarException, java.io.IOException- Throws:
RarExceptionjava.io.IOException
-
Archive
public Archive(java.io.File firstVolume, UnrarCallback unrarCallback, java.lang.String password) throws RarException, java.io.IOException- Throws:
RarExceptionjava.io.IOException
-
Archive
public Archive(java.io.InputStream rarAsStream) throws RarException, java.io.IOException- Throws:
RarExceptionjava.io.IOException
-
Archive
public Archive(java.io.InputStream rarAsStream, UnrarCallback unrarCallback) throws RarException, java.io.IOException- Throws:
RarExceptionjava.io.IOException
-
Archive
public Archive(java.io.InputStream rarAsStream, java.lang.String password) throws java.io.IOException, RarException- Throws:
java.io.IOExceptionRarException
-
Archive
public Archive(java.io.InputStream rarAsStream, UnrarCallback unrarCallback, java.lang.String password) throws java.io.IOException, RarException- Throws:
java.io.IOExceptionRarException
-
-
Method Detail
-
setChannel
private void setChannel(SeekableReadOnlyByteChannel channel, long length) throws java.io.IOException, RarException
- Throws:
java.io.IOExceptionRarException
-
bytesReadRead
public void bytesReadRead(int count)
-
getChannel
public SeekableReadOnlyByteChannel getChannel()
-
getHeaders
public java.util.List<BaseBlock> getHeaders()
Gets all of the headers in the archive.- Returns:
- returns the headers.
-
getFileHeaders
public java.util.List<FileHeader> getFileHeaders()
- Returns:
- returns all file headers of the archive
-
nextFileHeader
public FileHeader nextFileHeader()
-
getUnrarCallback
public UnrarCallback getUnrarCallback()
-
isEncrypted
public boolean isEncrypted() throws RarException- Returns:
- whether the archive is encrypted
- Throws:
RarException- when the main header is not present
-
isPasswordProtected
public boolean isPasswordProtected() throws RarException- Returns:
- whether the archive content is password protected
- Throws:
RarException- when the main header is not present
-
readHeaders
private void readHeaders(long fileLength) throws java.io.IOException, RarExceptionRead the headers of the archive- Parameters:
fileLength- Length of file.- Throws:
java.io.IOExceptionRarException
-
safelyAllocate
private static byte[] safelyAllocate(long len, int maxSize) throws RarException- Throws:
RarException
-
extractFile
public void extractFile(FileHeader hd, java.io.OutputStream os) throws RarException
Extract the file specified by the given header and write it to the supplied output stream- Parameters:
hd- the header to be extractedos- the outputstream- Throws:
RarException- .
-
getPropertyAs
private static <T> T getPropertyAs(java.lang.String key, java.util.function.Function<java.lang.String,T> function, T defaultValue)
-
getInputStream
public java.io.InputStream getInputStream(FileHeader hd) throws java.io.IOException
Returns anInputStreamthat will allow to read the file and stream it.
Please note that this method will create a pair of Pipe streams and either:
- delegate the work to a
ThreadPoolExecutor, viaArchive.ExtractorExecutorHolder; or - delegate the work to a newly created thread on each call
You can choose which strategy to use by setting the
junrar.extractor.use-executorsystem property.
Defaults to using theThreadPoolExecutor.- Parameters:
hd- the header to be extracted- Returns:
- an
InputStreamfrom which you can read the uncompressed bytes - Throws:
java.io.IOException- if any I/O error occur- See Also:
Archive.ExtractorExecutorHolder
- delegate the work to a
-
doExtractFile
private void doExtractFile(FileHeader hd, java.io.OutputStream os) throws RarException, java.io.IOException
- Throws:
RarExceptionjava.io.IOException
-
getMainHeader
public MainHeader getMainHeader()
- Returns:
- returns the main header of this archive
-
isOldFormat
public boolean isOldFormat()
- Returns:
- whether the archive is old format
-
close
public void close() throws java.io.IOExceptionClose the underlying compressed file.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
getVolumeManager
public VolumeManager getVolumeManager()
- Returns:
- the volumeManager
-
setVolumeManager
public void setVolumeManager(VolumeManager volumeManager)
- Parameters:
volumeManager- the volumeManager to set
-
getVolume
public Volume getVolume()
- Returns:
- the volume
-
getPassword
public java.lang.String getPassword()
-
setPassword
public void setPassword(java.lang.String password)
-
setVolume
public void setVolume(Volume volume) throws java.io.IOException, RarException
- Parameters:
volume- the volume to set- Throws:
java.io.IOException- .RarException- .
-
iterator
public java.util.Iterator<FileHeader> iterator()
- Specified by:
iteratorin interfacejava.lang.Iterable<FileHeader>
-
-