use of com.github.junrar.exception.RarException in project AozoraEpub3 by hmdev.
the class ImageInfoReader method loadRarImageInfos.
/** rar内の画像情報をすべて読み込み */
public void loadRarImageInfos(File srcFile, boolean addFileName) throws IOException, RarException {
Archive archive = new Archive(srcFile);
try {
int idx = 0;
for (FileHeader fileHeader : archive.getFileHeaders()) {
if (idx++ % 10 == 0)
LogAppender.append(".");
if (!fileHeader.isDirectory()) {
String entryName = fileHeader.getFileNameW();
if (entryName.length() == 0)
entryName = fileHeader.getFileNameString();
entryName = entryName.replace('\\', '/');
String lowerName = entryName.toLowerCase();
if (lowerName.endsWith(".png") || lowerName.endsWith(".jpg") || lowerName.endsWith(".jpeg") || lowerName.endsWith(".gif")) {
ImageInfo imageInfo = null;
InputStream is = null;
try {
//読めない場合があるので一旦バイト配列に読み込み
ByteArrayOutputStream baos = new ByteArrayOutputStream();
archive.extractFile(fileHeader, baos);
baos.close();
is = new ByteArrayInputStream(baos.toByteArray());
imageInfo = ImageInfo.getImageInfo(is);
if (imageInfo != null) {
this.imageFileInfos.put(entryName, imageInfo);
if (addFileName)
this.addImageFileName(entryName);
} else {
LogAppender.println();
LogAppender.error("画像が読み込めませんでした: " + entryName);
}
} catch (Exception e) {
LogAppender.println();
LogAppender.error("画像が読み込めませんでした: " + entryName);
e.printStackTrace();
} finally {
if (is != null)
is.close();
}
}
}
}
} finally {
LogAppender.println();
archive.close();
}
}
use of com.github.junrar.exception.RarException in project tika by apache.
the class RarParser method parse.
@Override
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
xhtml.startDocument();
EmbeddedDocumentExtractor extractor = EmbeddedDocumentUtil.getEmbeddedDocumentExtractor(context);
Archive rar = null;
try (TemporaryResources tmp = new TemporaryResources()) {
TikaInputStream tis = TikaInputStream.get(stream, tmp);
rar = new Archive(tis.getFile());
if (rar.isEncrypted()) {
throw new EncryptedDocumentException();
}
//Without this BodyContentHandler does not work
xhtml.element("div", " ");
FileHeader header = rar.nextFileHeader();
while (header != null && !Thread.currentThread().isInterrupted()) {
if (!header.isDirectory()) {
try (InputStream subFile = rar.getInputStream(header)) {
Metadata entrydata = PackageParser.handleEntryMetadata("".equals(header.getFileNameW()) ? header.getFileNameString() : header.getFileNameW(), header.getCTime(), header.getMTime(), header.getFullUnpackSize(), xhtml);
if (extractor.shouldParseEmbedded(entrydata)) {
extractor.parseEmbedded(subFile, handler, entrydata, true);
}
}
}
header = rar.nextFileHeader();
}
} catch (RarException e) {
throw new TikaException("RarParser Exception", e);
} finally {
if (rar != null)
rar.close();
}
xhtml.endDocument();
}
use of com.github.junrar.exception.RarException in project AozoraEpub3 by hmdev.
the class ImageInfoReader method getImage.
/** ファイル名から画像を取得
* 拡張子変更等は外側で修正しておく
* ファイルシステムまたはZipファイルから指定されたファイル名の画像を取得
* @param srcImageFileName ファイル名 Zipならエントリ名
* ※先頭からシークされるので遅い?
* @throws RarException */
public BufferedImage getImage(String srcImageFileName) throws IOException, RarException {
if (this.isFile) {
File file = new File(this.srcParentPath + srcImageFileName);
if (!file.exists()) {
//拡張子修正
srcImageFileName = this.correctExt(srcImageFileName);
file = new File(this.srcParentPath + srcImageFileName);
if (!file.exists())
return null;
}
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file), 8192);
try {
return ImageUtils.readImage(srcImageFileName.substring(srcImageFileName.lastIndexOf('.') + 1).toLowerCase(), bis);
} finally {
bis.close();
}
} else {
if (this.srcFile.getName().endsWith(".rar")) {
InputStream is = null;
Archive archive = new Archive(srcFile);
try {
FileHeader fileHeader = archive.nextFileHeader();
while (fileHeader != null) {
if (!fileHeader.isDirectory()) {
String entryName = fileHeader.getFileNameW();
if (entryName.length() == 0)
entryName = fileHeader.getFileNameString();
entryName = entryName.replace('\\', '/');
if (srcImageFileName.equals(entryName)) {
is = archive.getInputStream(fileHeader);
return ImageUtils.readImage(srcImageFileName.substring(srcImageFileName.lastIndexOf('.') + 1).toLowerCase(), is);
}
}
fileHeader = archive.nextFileHeader();
}
} finally {
if (is != null)
is.close();
archive.close();
}
} else {
ZipFile zf = new ZipFile(this.srcFile, "MS932");
ZipArchiveEntry entry = zf.getEntry(srcImageFileName);
if (entry == null) {
srcImageFileName = this.correctExt(srcImageFileName);
entry = zf.getEntry(srcImageFileName);
if (entry == null)
return null;
}
InputStream is = zf.getInputStream(entry);
try {
return ImageUtils.readImage(srcImageFileName.substring(srcImageFileName.lastIndexOf('.') + 1).toLowerCase(), is);
} catch (Exception e) {
e.printStackTrace();
} finally {
is.close();
zf.close();
}
}
}
return null;
}
Aggregations