@Override public int read() throws IOException { if (curPos == endPos) { return -1; } else { int b = file.read(); curPos++; return b; } }
public RarStream(final File file, final Executor executor) throws Exception { rarArchive = new Archive(file); this.executor = executor; }
@Override public void close() throws IOException { rarArchive.close(); }
public static boolean mergeArchive(Archive archive, ComprDataIO dataIO) throws IOException { FileHeader hd = dataIO.getSubHeader(); if (hd.getUnpVersion()>=20 && hd.getFileCRC()!=0xffffffff && dataIO.getPackedCRC()!=~hd.getFileCRC()) { System.err.println("Data Bad CRC"); } boolean oldNumbering = !archive.getMainHeader().isNewNumbering() || archive.isOldFormat(); String nextName = nextVolumeName(archive.getFile().getPath(), oldNumbering); NativeStorage nextVolume = archive.getFile().getParent().open(nextName); UnrarCallback callback = archive.getUnrarCallback(); if ((callback != null) && !callback.isNextVolumeReady(nextVolume)) { return false; } if (!nextVolume.exists()) { return false; } archive.setFile(nextVolume); hd = archive.nextFileHeader(); if (hd == null) { return false; } dataIO.init(hd); return true; }
public void parse( InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws SAXException, IOException, TikaException { EmbeddedDocumentExtractor extractor = new EmbeddedDocumentExtractor(context); try { File file = TikaInputStream.get(stream).getFile(); Archive archive = new Archive(new NativeStorage(file)); metadata.set(Metadata.CONTENT_TYPE, TYPE.toString()); XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); xhtml.startDocument(); for (FileHeader header : archive.getFileHeaders()) { Metadata entrydata = new Metadata(); entrydata.set( Metadata.RESOURCE_NAME_KEY, header.getFileNameString()); if (extractor.shouldParseEmbedded(entrydata)) { extractor.parseEmbedded(stream, xhtml, entrydata, true); } } xhtml.endDocument(); } catch (RarException e) { throw new TikaException("Unable to parse a RAR archive", e); } }
@Override public ArchiveEntry getNextEntry(boolean calculateSize) { header = rarArchive.nextFileHeader(); if (header == null) { return null; } return new DefaultArchiveEntry(header.getFileNameString(), header.getFullUnpackSize(), header.getMTime().getTime()); }
void setFile(NativeStorage file) throws IOException { this.file = file; totalPackedSize = 0L; totalPackedRead = 0L; close(); rof =file.read(); 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 init(FileHeader hd) throws IOException { long startPos = hd.getPositionInFile() + hd.getHeaderSize(); unpPackedSize = hd.getFullPackSize(); inputStream = new ReadOnlyAccessInputStream( archive.getRof(), startPos, startPos + unpPackedSize); subHead = hd; curUnpRead = 0; curPackWrite = 0; packedCRC = 0xFFffFFff; }
throw new EOFException(); if (subHead.isSplitAfter()){ packedCRC = RarCRC.checkCrc( (int)packedCRC, addr, offset, retCode); count -= retCode; unpPackedSize -= retCode; archive.bytesReadRead(retCode); if (unpPackedSize == 0 && subHead.isSplitAfter()) { if (!Volume.mergeArchive(archive, this)) { nextVolumeMissing=true; return -1;
public void unpWrite(byte[] addr, int offset, int count) throws IOException { if (!testMode) { // DestFile->Write(Addr,Count); outputStream.write(addr, offset, count); } curUnpWrite += count; if (!skipUnpCRC){ if (archive.isOldFormat()){ unpFileCRC = RarCRC.checkOldCrc( (short)unpFileCRC, addr, count); } else{ unpFileCRC = RarCRC.checkCrc( (int)unpFileCRC, addr,offset, count); } } // if (!skipArcCRC) { // archive.updateDataCRC(Addr, offset, ReadSize); // } }
@Override public void run() { try { latch.countDown(); rarArchive.extractFile(header, pOut); } catch (final RarException e) { LOGGER.error("Unknown exception:", e); } finally { try { pOut.close(); } catch (final IOException e) { LOGGER.warn("Unknown exception closing reader.", e); } } } }
public NativeStorage getParent() { return new NativeStorage(f.getParentFile()); }
/** * create a new archive object using the given file * * @param file * the file to extract * @throws RarException */ public Archive(NativeStorage file, UnrarCallback unrarCallback) throws RarException, IOException { setFile(file); this.unrarCallback = unrarCallback; dataIO = new ComprDataIO(this); }
/** * Extract the file specified by the given header and write it to the * supplied output stream * * @param header * the header to be extracted * @param os * the outputstream * @throws RarException */ public void extractFile(FileHeader hd, OutputStream os) throws RarException { if (!headers.contains(hd)) { throw new RarException(RarExceptionType.headerNotInArchive); } try { doExtractFile(hd, os); } catch (Exception e) { if (e instanceof RarException) { throw (RarException) e; } else { throw new RarException(e); } } }
public ReadOnlyAccessInputStream(NativeFile file, long startPos, long endPos) throws IOException { super(); this.file = file; this.startPos = startPos; curPos = startPos; this.endPos = endPos; file.setPosition(curPos); }
public void bytesReadRead(int count) { if (count > 0) { totalPackedRead += count; if (unrarCallback != null) { unrarCallback.volumeProgressChanged(totalPackedRead, totalPackedSize); } } }
public NativeFile read() throws FileNotFoundException { return new NativeFile(f, "r"); }
public NativeStorage open(String name) { return new NativeStorage(new File(f, name)); }
@Override public int read(byte[] b, int off, int len) throws IOException { if (len == 0) { return 0; } if (curPos == endPos) { return -1; } int bytesRead = file.read(b, off, (int)Math.min(len, endPos - curPos)); curPos += bytesRead; return bytesRead; }