Search in sources :

Example 1 with RarException

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();
    }
}
Also used : Archive(com.github.junrar.Archive) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageInfo(com.github.hmdev.info.ImageInfo) FileHeader(com.github.junrar.rarfile.FileHeader) IOException(java.io.IOException) RarException(com.github.junrar.exception.RarException)

Example 2 with RarException

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();
}
Also used : Archive(com.github.junrar.Archive) EncryptedDocumentException(org.apache.tika.exception.EncryptedDocumentException) TikaException(org.apache.tika.exception.TikaException) EmbeddedDocumentExtractor(org.apache.tika.extractor.EmbeddedDocumentExtractor) TikaInputStream(org.apache.tika.io.TikaInputStream) InputStream(java.io.InputStream) TemporaryResources(org.apache.tika.io.TemporaryResources) Metadata(org.apache.tika.metadata.Metadata) TikaInputStream(org.apache.tika.io.TikaInputStream) RarException(com.github.junrar.exception.RarException) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler) FileHeader(com.github.junrar.rarfile.FileHeader)

Example 3 with RarException

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;
}
Also used : Archive(com.github.junrar.Archive) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) FileHeader(com.github.junrar.rarfile.FileHeader) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) RarException(com.github.junrar.exception.RarException)

Aggregations

Archive (com.github.junrar.Archive)3 RarException (com.github.junrar.exception.RarException)3 FileHeader (com.github.junrar.rarfile.FileHeader)3 InputStream (java.io.InputStream)3 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)2 ImageInfo (com.github.hmdev.info.ImageInfo)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)1 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)1 EncryptedDocumentException (org.apache.tika.exception.EncryptedDocumentException)1 TikaException (org.apache.tika.exception.TikaException)1 EmbeddedDocumentExtractor (org.apache.tika.extractor.EmbeddedDocumentExtractor)1 TemporaryResources (org.apache.tika.io.TemporaryResources)1 TikaInputStream (org.apache.tika.io.TikaInputStream)1 Metadata (org.apache.tika.metadata.Metadata)1