org.apache.commons.compress.archivers.sevenz.SevenZFile Java Examples

The following examples show how to use org.apache.commons.compress.archivers.sevenz.SevenZFile. 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 File: PressUtility.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
public static void decompress7z(File fromFile, File toDirectory) {
	try (SevenZFile archiveInputStream = new SevenZFile(fromFile)) {
		byte[] buffer = new byte[BUFFER_SIZE];
		ArchiveEntry archiveEntry;
		while (null != (archiveEntry = archiveInputStream.getNextEntry())) {
			File file = new File(toDirectory, archiveEntry.getName());
			try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
				int length = -1;
				while ((length = archiveInputStream.read(buffer)) != -1) {
					fileOutputStream.write(buffer, 0, length);
				}
			}
		}
	} catch (IOException exception) {
		throw new IllegalStateException("解压7Z异常:", exception);
	}
}
 
Example #2
Source File: SevenZParser.java    From bubble with MIT License 6 votes vote down vote up
@Override
public void parse(File file) throws IOException {
    mEntries = new ArrayList<>();
    SevenZFile sevenZFile = new SevenZFile(file);

    SevenZArchiveEntry entry = sevenZFile.getNextEntry();
    while (entry != null) {
        if (entry.isDirectory()) {
            continue;
        }
        if (Utils.isImage(entry.getName())) {
            byte[] content = new byte[(int)entry.getSize()];
            sevenZFile.read(content);
            mEntries.add(new SevenZEntry(entry, content));
        }
        entry = sevenZFile.getNextEntry();
    }

    Collections.sort(mEntries, new NaturalOrderComparator() {
        @Override
        public String stringValue(Object o) {
            return ((SevenZEntry) o).entry.getName();
        }
    });
}
 
Example #3
Source File: ZipReader.java    From kkFileView with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    try {
        SevenZFile sevenZFile = new SevenZFile(new File(filePath));
        SevenZArchiveEntry entry = sevenZFile.getNextEntry();
        while (entry != null) {
            if (entry.isDirectory()) {
                entry = sevenZFile.getNextEntry();
                continue;
            }
            String childName = "default_file";
            SevenZArchiveEntry entry1;
            for (Map<String, SevenZArchiveEntry> entryMap : entriesToBeExtracted) {
                childName = entryMap.keySet().iterator().next();
                entry1 = entryMap.values().iterator().next();
                if (entry.getName().equals(entry1.getName())) {
                    break;
                }
            }
            FileOutputStream out = new FileOutputStream(fileDir + childName);
            byte[] content = new byte[(int) entry.getSize()];
            sevenZFile.read(content, 0, content.length);
            out.write(content);
            out.close();
            entry = sevenZFile.getNextEntry();
        }
        sevenZFile.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (new File(filePath).exists()) {
        new File(filePath).delete();
    }
}
 
Example #4
Source File: FilesDecompressUnarchiveBatchController.java    From MyBox with Apache License 2.0 4 votes vote down vote up
protected void unarchive7z(File srcFile) {
    try {
        try ( SevenZFile sevenZFile = new SevenZFile(srcFile)) {
            SevenZArchiveEntry entry;
            while ((entry = sevenZFile.getNextEntry()) != null) {
                if (entry.isDirectory()) {
                    continue;
                }
                if (verboseCheck == null || verboseCheck.isSelected()) {
                    updateLogs(message("Handling...") + ":   " + entry.getName());
                }
                File file = makeTargetFile(entry.getName(), targetPath);
                if (file == null) {
                    continue;
                }
                File parent = file.getParentFile();
                if (!parent.isDirectory() && !parent.mkdirs()) {
                    archiveFail++;
                    if (verboseCheck == null || verboseCheck.isSelected()) {
                        updateLogs(message("FailOpenFile" + ":" + file));
                    }
                    continue;
                }
                try ( FileOutputStream out = new FileOutputStream(file)) {
                    int length;
                    byte[] buf = new byte[CommonValues.IOBufferLength];
                    while ((length = sevenZFile.read(buf)) != -1) {
                        out.write(buf, 0, length);
                    }
                }
                archiveSuccess++;
                targetFileGenerated(file);
            }
        }
    } catch (Exception e) {
        archiveFail++;
        String s = e.toString();
        updateLogs(s);
        if (s.contains("java.nio.charset.MalformedInputException")
                || s.contains("Illegal char")) {
            updateLogs(message("CharsetIncorrect"));
            charsetIncorrect = true;
        }
    }
}
 
Example #5
Source File: FileUnarchiveController.java    From MyBox with Apache License 2.0 4 votes vote down vote up
protected void unarchive7z() {
        try {
            if (selected == null || selected.isEmpty()) {
                return;
            }
            try ( SevenZFile sevenZFile = new SevenZFile(sourceFile)) {
                SevenZArchiveEntry entry;
                while ((entry = sevenZFile.getNextEntry()) != null) {
                    if (entry.isDirectory() || !selected.contains(entry.getName())) {
                        continue;
                    }
                    File file = makeTargetFile(entry.getName(), targetPath);
                    if (file == null) {
                        continue;
                    }
                    File parent = file.getParentFile();
                    if (!parent.isDirectory() && !parent.mkdirs()) {
                        archiveFail++;
//                        logger.debug(message("FailOpenFile" + ":" + file));
                        continue;
                    }
                    try ( FileOutputStream out = new FileOutputStream(file)) {
                        int length;
                        byte[] buf = new byte[CommonValues.IOBufferLength];
                        while ((length = sevenZFile.read(buf)) != -1) {
                            out.write(buf, 0, length);
                        }
                    }
                    archiveSuccess++;
                }
            }
        } catch (Exception e) {
            archiveFail++;
            archiveFail++;
            error = e.toString();
            if (error.contains("java.nio.charset.MalformedInputException")
                    || error.contains("Illegal char")) {
                charsetIncorrect = true;
            }
        }
    }
 
Example #6
Source File: ZipReader.java    From kkFileView with Apache License 2.0 4 votes vote down vote up
public String read7zFile(String filePath,String fileKey) {
    String archiveSeparator = "/";
    Map<String, FileNode> appender = Maps.newHashMap();
    List<String> imgUrls = Lists.newArrayList();
    String baseUrl= BaseUrlFilter.getBaseUrl();
    String archiveFileName = fileUtils.getFileNameFromPath(filePath);
    try {
        SevenZFile zipFile = new SevenZFile(new File(filePath));
        Iterable<SevenZArchiveEntry> entries = zipFile.getEntries();
        // 排序
        Enumeration<SevenZArchiveEntry> newEntries = sortSevenZEntries(entries);
        List<Map<String, SevenZArchiveEntry>> entriesToBeExtracted = Lists.newArrayList();
        while (newEntries.hasMoreElements()){
            SevenZArchiveEntry entry = newEntries.nextElement();
            String fullName = entry.getName();
            int level = fullName.split(archiveSeparator).length;
            // 展示名
            String originName = getLastFileName(fullName, archiveSeparator);
            String childName = level + "_" + originName;
            boolean directory = entry.isDirectory();
            if (!directory) {
                childName = archiveFileName + "_" + originName;
                entriesToBeExtracted.add(Collections.singletonMap(childName, entry));
            }
            String parentName = getLast2FileName(fullName, archiveSeparator, archiveFileName);
            parentName = (level-1) + "_" + parentName;
            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 SevenZExtractorWorker(entriesToBeExtracted, filePath));
        fileUtils.putImgCache(fileKey,imgUrls);
        return new ObjectMapper().writeValueAsString(appender.get(""));
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example #7
Source File: CompressFileOperationsImpl.java    From gradle-plugins with MIT License 4 votes vote down vote up
@Override
public FileTree sevenZipTree(Object sevenZipFile) {
    return sevenZipTree(sevenZipFile, f -> new SevenZipArchiveInputStream(new SevenZFile(f)));
}
 
Example #8
Source File: CompressFileOperationsImpl.java    From gradle-plugins with MIT License 4 votes vote down vote up
@Override
public FileTree sevenZipTree(Object sevenZipFile, char[] password) {
    return sevenZipTree(sevenZipFile, f -> new SevenZipArchiveInputStream(new SevenZFile(f, password)));
}
 
Example #9
Source File: CompressFileOperationsImpl.java    From gradle-plugins with MIT License 4 votes vote down vote up
@Override
public FileTree sevenZipTree(Object sevenZipFile) {
    return sevenZipTree(sevenZipFile, f -> new SevenZipArchiveInputStream(new SevenZFile(f)));
}
 
Example #10
Source File: CompressFileOperationsImpl.java    From gradle-plugins with MIT License 4 votes vote down vote up
@Override
public FileTree sevenZipTree(Object sevenZipFile, char[] password) {
    return sevenZipTree(sevenZipFile, f -> new SevenZipArchiveInputStream(new SevenZFile(f, password)));
}
 
Example #11
Source File: SevenZArchiver.java    From jarchivelib with Apache License 2.0 4 votes vote down vote up
@Override
protected ArchiveInputStream createArchiveInputStream(File archive) throws IOException {
    return new SevenZInputStream(new SevenZFile(archive));
}
 
Example #12
Source File: SevenZArchiver.java    From jarchivelib with Apache License 2.0 4 votes vote down vote up
public SevenZInputStream(SevenZFile file) {
    this.file = file;
}