java 使用apache commmons compress提取zip文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 使用apache commmons compress提取zip文件相关的知识,希望对你有一定的参考价值。

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;

String dirName = path + File.separator + FilenameUtils.getBaseName(file.getName());
File dir = new File(dirName);
if (dir.exists()) {
    LOGGER.error("Il file {} è già stato estratto", file.getAbsolutePath());
    continue;
}
if (!dir.mkdir()) {
    LOGGER.error("Impossibile creare la directory {}", dir.getAbsolutePath());
    continue;
}

ZipFile zipFile = null;
try {

    zipFile = new ZipFile(file);
    Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
    while (entries.hasMoreElements()) {
        ZipArchiveEntry entry = (ZipArchiveEntry) entries.nextElement();
        InputStream in = zipFile.getInputStream(entry);
        OutputStream out = new FileOutputStream(dir);
        IOUtils.copy(in, out);
        IOUtils.closeQuietly(in);
        out.close();
    }
}
catch (ZipException e) {
    LOGGER.error(String.format("Errori durante l'estrazione del file %s", file.getAbsolutePath()), e);
}
catch (IOException e) {
    LOGGER.error(String.format("Errori durante l'estrazione del file %s", file.getAbsolutePath()), e);
}
finally {
    if (zipFile != null) {
        try {
            zipFile.close();
        }
        catch (IOException e) {
            LOGGER.error(String.format("Errori durante la chiusura del file zip %s", file.getAbsolutePath()), e);
        }
    }
}

以上是关于java 使用apache commmons compress提取zip文件的主要内容,如果未能解决你的问题,请参考以下文章

java_log4j

Exception in thread “main“ java.lang.NoClassDefFoundError: org/apache/flink/

Exception in thread “main“ java.lang.NoClassDefFoundError: org/apache/flink/

Exception in thread “main“ java.lang.NoClassDefFoundError: org/apache/flink/

Apache Commons 工具类介绍及简单使用

如何使用Java API读写HDFS