Java Code Examples for com.github.junrar.rarfile.FileHeader

The following examples show how to use com.github.junrar.rarfile.FileHeader. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: kkFileViewOfficeEdit   Source File: ZipReader.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    System.out.println("解析压缩文件开始《《《《《《《《《《《《《《《《《《《《《《《");
    for (Map<String, FileHeader> entryMap : headersToBeExtracted) {
        String childName = entryMap.keySet().iterator().next();
        extractRarFile(childName, entryMap.values().iterator().next(), archive);
    }
    try {
        archive.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (new File(filePath).exists()) {
        new File(filePath).delete();
    }
    System.out.println("解析压缩文件结束《《《《《《《《《《《《《《《《《《《《《《《");
}
 
Example 2
Source Project: bubble   Source File: RarParser.java    License: MIT License 6 votes vote down vote up
@Override
public InputStream getPage(int num) throws IOException {
    if (mArchive.getMainHeader().isSolid()) {
        // solid archives require special treatment
        synchronized (this) {
            if (!mSolidFileExtracted) {
                for (FileHeader h : mArchive.getFileHeaders()) {
                    if (!h.isDirectory() && Utils.isImage(getName(h))) {
                        getPageStream(h);
                    }
                }
                mSolidFileExtracted = true;
            }
        }
    }
    return getPageStream(mHeaders.get(num));
}
 
Example 3
Source Project: protools   Source File: ToolZip.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 解压rar
 *
 * @param rarFile
 *         带解压压缩包列表
 * @param destDir
 *         目标目录
 *
 * @return 成功与否
 *
 * @throws IOException
 *         IO错误抛出
 * @throws RarException
 *         IO错误抛出
 */
public static boolean unrarFile(File rarFile, File destDir) throws IOException, RarException {
    try (Archive archive = new Archive(rarFile)) {
        FileHeader fh = archive.nextFileHeader();
        while (fh != null) {
            String compressFileName = fh.getFileNameString().trim();
            File destFile = new File(destDir.getAbsolutePath() + StrConst.FILE_SEP + compressFileName);

            if (fh.isDirectory()) {
                if (!destFile.exists()) {
                    destFile.mkdirs();
                }
                fh = archive.nextFileHeader();
                continue;
            }

            if (!destFile.getParentFile().exists()) {
                destFile.getParentFile().mkdirs();
            }

            try (FileOutputStream fos = new FileOutputStream(destFile)) {
                archive.extractFile(fh, fos);
            }
            fh = archive.nextFileHeader();
        }
    }
    return true;
}
 
Example 4
Source Project: kkFileView   Source File: ZipReader.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    for (Map<String, FileHeader> entryMap : headersToBeExtracted) {
        String childName = entryMap.keySet().iterator().next();
        extractRarFile(childName, entryMap.values().iterator().next(), archive);
    }
    try {
        archive.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (new File(filePath).exists()) {
        new File(filePath).delete();
    }
}
 
Example 5
Source Project: kkFileView   Source File: ZipReader.java    License: Apache License 2.0 5 votes vote down vote up
private void extractRarFile(String childName, FileHeader header, Archive archive) {
    String outPath = fileDir + childName;
    try(OutputStream ot = new FileOutputStream(outPath)) {
        archive.extractFile(header, ot);
    } catch (IOException | RarException e) {
        e.printStackTrace();
    }
}
 
Example 6
Source Project: bubble   Source File: RarParser.java    License: MIT License 5 votes vote down vote up
@Override
public void parse(File file) throws IOException {
    try {
        mArchive = new Archive(file);
    }
    catch (RarException e) {
        throw new IOException("unable to open archive");
    }

    FileHeader header = mArchive.nextFileHeader();
    while (header != null) {
        if (!header.isDirectory()) {
            String name = getName(header);
            if (Utils.isImage(name)) {
                mHeaders.add(header);
            }
        }

        header = mArchive.nextFileHeader();
    }

    Collections.sort(mHeaders, new NaturalOrderComparator() {
        @Override
        public String stringValue(Object o) {
            return getName((FileHeader) o);
        }
    });
}
 
Example 7
Source Project: kkFileViewOfficeEdit   Source File: ZipReader.java    License: Apache License 2.0 4 votes vote down vote up
public RarExtractorWorker(List<Map<String, FileHeader>> headersToBeExtracted, Archive archive, String filePath) {
    this.headersToBeExtracted = headersToBeExtracted;
    this.archive = archive;
    this.filePath = filePath;
}
 
Example 8
Source Project: kkFileView   Source File: ZipReader.java    License: Apache License 2.0 4 votes vote down vote up
public String unRar(String filePath,String fileKey){
    Map<String, FileNode> appender = Maps.newHashMap();
    List<String> imgUrls = Lists.newArrayList();
    String baseUrl = BaseUrlFilter.getBaseUrl();
    try {
        Archive archive = new Archive(new FileInputStream(new File(filePath)));
        List<FileHeader> headers = archive.getFileHeaders();
        headers = sortedHeaders(headers);
        String archiveFileName = fileUtils.getFileNameFromPath(filePath);
        List<Map<String, FileHeader>> headersToBeExtracted = Lists.newArrayList();
        for (FileHeader header : headers) {
            String fullName;
            if (header.isUnicode()) {
                fullName = header.getFileNameW();
            }else {
                fullName = header.getFileNameString();
            }
            // 展示名
            String originName = getLastFileName(fullName, "\\");
            String childName = originName;
            boolean directory = header.isDirectory();
            if (!directory) {
                childName = archiveFileName + "_" + originName;
                headersToBeExtracted.add(Collections.singletonMap(childName, header));
            }
            String parentName = getLast2FileName(fullName, "\\", archiveFileName);
            FileType type = fileUtils.typeFromUrl(childName);
            if (type.equals(FileType.picture)){//添加图片文件到图片列表
                imgUrls.add(baseUrl+childName);
            }
            FileNode node = new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey);
            addNodes(appender, parentName, node);
            appender.put(childName, node);
        }
        executors.submit(new RarExtractorWorker(headersToBeExtracted, archive, filePath));
        fileUtils.putImgCache(fileKey,imgUrls);
        return new ObjectMapper().writeValueAsString(appender.get(""));
    } catch (RarException | IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 9
Source Project: kkFileView   Source File: ZipReader.java    License: Apache License 2.0 4 votes vote down vote up
public RarExtractorWorker(List<Map<String, FileHeader>> headersToBeExtracted, Archive archive, String filePath) {
    this.headersToBeExtracted = headersToBeExtracted;
    this.archive = archive;
    this.filePath = filePath;
}
 
Example 10
Source Project: bubble   Source File: RarParser.java    License: MIT License 4 votes vote down vote up
private String getName(FileHeader header) {
    return header.isUnicode() ? header.getFileNameW() : header.getFileNameString();
}