Search in sources :

Example 6 with Archive

use of com.github.junrar.Archive in project AozoraEpub3 by hmdev.

the class AozoraEpub3 method getTextInputStream.

/** 入力ファイルからStreamオープン
	 * 
	 * @param srcFile
	 * @param ext
	 * @param imageInfoReader
	 * @param txtIdx テキストファイルのZip内の位置
	 * @return テキストファイルのストリーム (close()は呼び出し側ですること)
	 * @throws RarException 
	 */
public static InputStream getTextInputStream(File srcFile, String ext, ImageInfoReader imageInfoReader, String[] textEntryName, int txtIdx) throws IOException, RarException {
    if ("txt".equals(ext)) {
        return new FileInputStream(srcFile);
    } else if ("zip".equals(ext) || "txtz".equals(ext)) {
        //Zipなら最初のtxt
        ZipArchiveInputStream zis = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(srcFile), 65536), "MS932", false);
        ArchiveEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            String entryName = entry.getName();
            if (entryName.substring(entryName.lastIndexOf('.') + 1).equalsIgnoreCase("txt") && txtIdx-- == 0) {
                if (imageInfoReader != null)
                    imageInfoReader.setArchiveTextEntry(entryName);
                if (textEntryName != null)
                    textEntryName[0] = entryName;
                return zis;
            }
        }
        LogAppender.append("zip内にtxtファイルがありません: ");
        LogAppender.println(srcFile.getName());
        return null;
    } else if ("rar".equals(ext)) {
        //tempのtxtファイル作成
        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 (entryName.substring(entryName.lastIndexOf('.') + 1).equalsIgnoreCase("txt") && txtIdx-- == 0) {
                        if (imageInfoReader != null)
                            imageInfoReader.setArchiveTextEntry(entryName);
                        if (textEntryName != null)
                            textEntryName[0] = entryName;
                        //tmpファイルにコピーして終了時に削除
                        File tmpFile = File.createTempFile("rarTmp", "txt");
                        tmpFile.deleteOnExit();
                        FileOutputStream fos = new FileOutputStream(tmpFile);
                        InputStream is = archive.getInputStream(fileHeader);
                        try {
                            IOUtils.copy(is, fos);
                        } finally {
                            is.close();
                            fos.close();
                        }
                        return new BufferedInputStream(new FileInputStream(tmpFile), 65536);
                    }
                }
                fileHeader = archive.nextFileHeader();
            }
        } finally {
            archive.close();
        }
        LogAppender.append("rar内にtxtファイルがありません: ");
        LogAppender.println(srcFile.getName());
        return null;
    } else {
        LogAppender.append("txt, zip, rar, txtz, cbz のみ変換可能です: ");
        LogAppender.println(srcFile.getPath());
    }
    return null;
}
Also used : Archive(com.github.junrar.Archive) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) FileHeader(com.github.junrar.rarfile.FileHeader) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 7 with Archive

use of com.github.junrar.Archive in project AozoraEpub3 by hmdev.

the class AozoraEpub3 method countRarText.

/** Ripファイル内のテキストファイルの数を取得 */
public static int countRarText(File rarFile) throws IOException, RarException {
    int txtCount = 0;
    Archive archive = new Archive(rarFile);
    try {
        for (FileHeader fileHeader : archive.getFileHeaders()) {
            if (!fileHeader.isDirectory()) {
                String entryName = fileHeader.getFileNameW();
                if (entryName.length() == 0)
                    entryName = fileHeader.getFileNameString();
                entryName = entryName.replace('\\', '/');
                if (entryName.substring(entryName.lastIndexOf('.') + 1).equalsIgnoreCase("txt"))
                    txtCount++;
            }
        }
    } finally {
        archive.close();
    }
    return txtCount;
}
Also used : Archive(com.github.junrar.Archive) FileHeader(com.github.junrar.rarfile.FileHeader)

Aggregations

Archive (com.github.junrar.Archive)7 FileHeader (com.github.junrar.rarfile.FileHeader)7 InputStream (java.io.InputStream)6 BufferedInputStream (java.io.BufferedInputStream)5 FileInputStream (java.io.FileInputStream)5 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)5 RarException (com.github.junrar.exception.RarException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 File (java.io.File)3 IOException (java.io.IOException)3 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)3 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)3 ImageInfo (com.github.hmdev.info.ImageInfo)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 ChapterInfo (com.github.hmdev.info.ChapterInfo)1 ChapterLineInfo (com.github.hmdev.info.ChapterLineInfo)1 GaijiInfo (com.github.hmdev.info.GaijiInfo)1 SectionInfo (com.github.hmdev.info.SectionInfo)1 BufferedOutputStream (java.io.BufferedOutputStream)1