?? archive.java
字號:
/*
* Copyright (c) 2007 innoSysTec (R) GmbH, Germany. All rights reserved.
* Original author: Edmund Wagner
* Creation date: 22.05.2007
*
* Source: $HeadURL$
* Last changed: $LastChangedDate$
*
* the unrar licence applies to all junrar source and binary distributions
* you are not allowed to use this source to re-create the RAR compression
* algorithm
*
* Here some html entities which can be used for escaping javadoc tags:
* "&": "&" or "&"
* "<": "<" or "<"
* ">": ">" or ">"
* "@": "@"
*/
package de.innosystec.unrar;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import de.innosystec.unrar.exception.RarException;
import de.innosystec.unrar.exception.RarException.RarExceptionType;
import de.innosystec.unrar.io.IReadOnlyAccess;
import de.innosystec.unrar.io.ReadOnlyAccessFile;
import de.innosystec.unrar.rarfile.AVHeader;
import de.innosystec.unrar.rarfile.BaseBlock;
import de.innosystec.unrar.rarfile.BlockHeader;
import de.innosystec.unrar.rarfile.CommentHeader;
import de.innosystec.unrar.rarfile.EAHeader;
import de.innosystec.unrar.rarfile.EndArcHeader;
import de.innosystec.unrar.rarfile.FileHeader;
import de.innosystec.unrar.rarfile.MacInfoHeader;
import de.innosystec.unrar.rarfile.MainHeader;
import de.innosystec.unrar.rarfile.MarkHeader;
import de.innosystec.unrar.rarfile.ProtectHeader;
import de.innosystec.unrar.rarfile.SignHeader;
import de.innosystec.unrar.rarfile.SubBlockHeader;
import de.innosystec.unrar.rarfile.UnixOwnersHeader;
import de.innosystec.unrar.rarfile.UnrarHeadertype;
import de.innosystec.unrar.unpack.ComprDataIO;
import de.innosystec.unrar.unpack.Unpack;
/**
* DOCUMENT ME
*
* @author $LastChangedBy$
* @version $LastChangedRevision$
*/
public class Archive implements Closeable {
private static Logger logger = Logger.getLogger(Archive.class.getName());
private File file;
private IReadOnlyAccess rof;
private final UnrarCallback unrarCallback;
private final ComprDataIO dataIO;
private final List<BaseBlock> headers = new ArrayList<BaseBlock>();
private MarkHeader markHead = null;
private MainHeader newMhd = null;
private EndArcHeader endHeader = null;
private Unpack unpack;
/** Archive data CRC. */
private long arcDataCRC = 0xffffffff;
private int currentHeaderIndex;
private boolean encrypted = false;
private int sfxSize = 0;
/** Size of packed data in current file. */
private long totalPackedSize = 0L;
/** Number of bytes of compressed data read from current file. */
private long totalPackedRead = 0L;
public Archive(File file) throws RarException, IOException {
this(file, null);
}
/**
* create a new archive object using the given file
* @param file the file to extract
* @throws RarException
*/
public Archive(File file, UnrarCallback unrarCallback)
throws RarException, IOException {
setFile(file);
this.unrarCallback = unrarCallback;
dataIO = new ComprDataIO(this);
}
public File getFile() {
return file;
}
void setFile(File file) throws IOException {
this.file = file;
totalPackedSize = 0L;
totalPackedRead = 0L;
close();
rof = new ReadOnlyAccessFile(file);
try {
readHeaders();
}
catch (Exception e) {
logger.log(Level.WARNING,
"exception in archive constructor maybe file is encrypted " +
"or currupt", e);
//ignore exceptions to allow exraction of working files in
//corrupt archive
}
// Calculate size of packed data
for (BaseBlock block : headers) {
if (block.getHeaderType() == UnrarHeadertype.FileHeader) {
totalPackedSize += ((FileHeader)block).getFullPackSize();
}
}
if (unrarCallback != null) {
unrarCallback.volumeProgressChanged(totalPackedRead,
totalPackedSize);
}
}
public void bytesReadRead(int count) {
if (count > 0) {
totalPackedRead += count;
if (unrarCallback != null) {
unrarCallback.volumeProgressChanged(totalPackedRead,
totalPackedSize);
}
}
}
public IReadOnlyAccess getRof() {
return rof;
}
/**
* @return returns all file headers of the archive
*/
public List<FileHeader> getFileHeaders(){
List<FileHeader> list = new ArrayList<FileHeader>();
for (BaseBlock block: headers) {
if(block.getHeaderType().equals(UnrarHeadertype.FileHeader)){
list.add((FileHeader)block);
}
}
return list;
}
public FileHeader nextFileHeader() {
int n = headers.size();
while (currentHeaderIndex < n) {
BaseBlock block = headers.get(currentHeaderIndex++);
if (block.getHeaderType() == UnrarHeadertype.FileHeader) {
return (FileHeader)block;
}
}
return null;
}
public UnrarCallback getUnrarCallback() {
return unrarCallback;
}
/**
*
* @return whether the archive is encrypted
*/
public boolean isEncrypted() {
if(newMhd!=null){
return newMhd.isEncrypted();
}else{
throw new NullPointerException("mainheader is null");
}
}
/**
* Read the headers of the archive
* @throws RarException
*/
private void readHeaders() throws IOException, RarException{
markHead = null;
newMhd = null;
endHeader = null;
headers.clear();
currentHeaderIndex = 0;
int toRead = 0;
long fileLength = this.file.length();
while(true){
int size = 0;
long newpos = 0;
byte[] baseBlockBuffer =new byte[BaseBlock.BaseBlockSize];
long position = rof.getPosition();
// Weird, but is trying to read beyond the end of the file
if (position >= fileLength) {
break;
}
// logger.info("\n--------reading header--------");
size = rof.readFully(baseBlockBuffer, BaseBlock.BaseBlockSize);
if (size == 0){
break;
}
BaseBlock block = new BaseBlock(baseBlockBuffer);
block.setPositionInFile(position);
switch(block.getHeaderType()) {
case MarkHeader:
markHead = new MarkHeader(block);
if (!markHead.isSignature()) {
throw new RarException(
RarException.RarExceptionType.badRarArchive);
}
headers.add(markHead);
// markHead.print();
break;
case MainHeader:
int mainHeaderSize = 0;
toRead = block.hasEncryptVersion() ?
MainHeader.mainHeaderSizeWithEnc :
MainHeader.mainHeaderSize;
byte[] mainbuff = new byte[toRead];
mainHeaderSize = rof.readFully(mainbuff, toRead);
MainHeader mainhead =new MainHeader(block,mainbuff);
headers.add(mainhead);
this.newMhd = mainhead;
if(newMhd.isEncrypted()){
throw new RarException(
RarExceptionType.rarEncryptedException);
}
// mainhead.print();
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -