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

The following examples show how to use org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry. 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: 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 #2
Source File: ZipReader.java    From kkFileView with Apache License 2.0 5 votes vote down vote up
private Enumeration<SevenZArchiveEntry> sortSevenZEntries(Iterable<SevenZArchiveEntry> entries) {
    List<SevenZArchiveEntry> sortedEntries = Lists.newArrayList();
    for (SevenZArchiveEntry entry : entries) {
        sortedEntries.add(entry);
    }
    return Collections.enumeration(sortedEntries);
}
 
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: PressUtility.java    From jstarcraft-core with Apache License 2.0 4 votes vote down vote up
public static void compress7z(File fromDirectory, File toFile) {
	try (SevenZOutputFile archiveOutputStream = new SevenZOutputFile(toFile)) {
		byte[] buffer = new byte[BUFFER_SIZE];
		for (File file : fromDirectory.listFiles()) {
			try (FileInputStream fileInputStream = new FileInputStream(file)) {
				SevenZArchiveEntry archiveEntry = archiveOutputStream.createArchiveEntry(fromDirectory, file.getName());
				archiveOutputStream.putArchiveEntry(archiveEntry);
				int length = -1;
				while ((length = fileInputStream.read(buffer)) != -1) {
					archiveOutputStream.write(buffer, 0, length);
				}
				archiveOutputStream.closeArchiveEntry();
			}
		}
	} catch (IOException exception) {
		throw new IllegalStateException("压缩7Z异常", exception);
	}
}
 
Example #7
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 #8
Source File: ZipReader.java    From kkFileView with Apache License 2.0 4 votes vote down vote up
public SevenZExtractorWorker(List<Map<String, SevenZArchiveEntry>> entriesToBeExtracted, String filePath) {
    this.entriesToBeExtracted = entriesToBeExtracted;
    this.filePath = filePath;
}
 
Example #9
Source File: SevenZParser.java    From bubble with MIT License 4 votes vote down vote up
public SevenZEntry(SevenZArchiveEntry entry, byte[] bytes) {
    this.entry = entry;
    this.bytes = bytes;
}