java解压缩zip、rar

简介: java解压缩zip、rar

解压缩zip

  1. 使用hutool工具包中ZipUtil工具类
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.7.2</version>
</dependency>
// 参数是压缩包路径和编码
// GBK是为了解决中文解压缩乱码的问题
ZipUtil.unzip(f.getAbsolutePath(), Charset.forName("GBK"));
  1. 使用zip4j工具包
<dependency>
    <groupId>net.lingala.zip4j</groupId>
    <artifactId>zip4j</artifactId>
    <version>2.9.1</version>
</dependency>
ZipFile zipFile = new ZipFile(f.getAbsolutePath());
// 中文乱码处理
zipFile.setCharset(Charset.forName("GBK"));
File zdir = new File(f.getAbsolutePath().substring(0, f.getAbsolutePath().indexOf(".")));
if (!zdir.isDirectory()) {
    zdir.mkdir();
}
try {
  // 解压到指定文件夹
    zipFile.extractAll(zdir.getAbsolutePath());
} catch (ZipException e) {
    e.printStackTrace();
}

解压缩rar

  1. 使用junrar工具包
<dependency>
    <groupId>com.github.junrar</groupId>
    <artifactId>junrar</artifactId>
    <version>7.4.0</version>
</dependency>
File zdir = new File(f.getAbsolutePath().substring(0, f.getAbsolutePath().indexOf(".")));
if (!zdir.isDirectory()) {
    zdir.mkdir();
}
try {
    Junrar.extract(f.getAbsolutePath(), zdir.getAbsolutePath());
} catch (IOException e) {
    e.printStackTrace();
} catch (RarException e) {
    e.printStackTrace();
}
  1. 使用sevenzipjbinding
<dependency>
    <groupId>net.sf.sevenzipjbinding</groupId>
    <artifactId>sevenzipjbinding</artifactId>
    <version>16.02-2.01</version>
</dependency>
<dependency>
    <groupId>net.sf.sevenzipjbinding</groupId>
    <artifactId>sevenzipjbinding-all-platforms</artifactId>
    <version>16.02-2.01</version>
</dependency>
try {
   // f -  压缩文件
   RandomAccessFile randomAccessFile = new RandomAccessFile(f.getAbsolutePath(), "r");
   IInArchive archive = SevenZip.openInArchive(ArchiveFormat.RAR5,  new RandomAccessFileInStream(randomAccessFile));
   // 解压文件路径
   File zdir = new File( f.getAbsolutePath().substring(0,  f.getAbsolutePath() .indexOf(".")));
   if (!zdir.isDirectory()) {
       zdir.mkdir();
   }
   int[] in = new int[archive.getNumberOfItems()];
   for(int i=0;i<in.length;i++){
       in[i] = i;
   }
   archive.extract(in, false, new ExtractCallback(archive, zdir.getAbsolutePath()));
   archive.close();
   randomAccessFile.close();
} catch (FileNotFoundException | SevenZipException e) {
   e.printStackTrace();
} catch (IOException e) {
   e.printStackTrace();
}
private static class ExtractCallback implements IArchiveExtractCallback {
    private final IInArchive inArchive;
    private final String extractPath;
    public ExtractCallback(IInArchive inArchive, String extractPath) {
        this.inArchive = inArchive;
        if (!extractPath.endsWith("/") && !extractPath.endsWith("\\")) {
            extractPath += File.separator;
        }
        this.extractPath = extractPath;
    }
    @Override
    public void setTotal(long total) {
    }
    @Override
    public void setCompleted(long complete) {
    }
    @Override
    public ISequentialOutStream getStream(int index, ExtractAskMode extractAskMode) throws SevenZipException {
        return data -> {
            String filePath = inArchive.getStringProperty(index, PropID.PATH);
            FileOutputStream fos = null;
            try {
                File path = new File(extractPath + filePath);
                if(!path.getParentFile().exists()){
                    path.getParentFile().mkdirs();
                }
        if(!path.exists()){
            path.createNewFile();
        }
        fos = new FileOutputStream(path, true);
        fos.write(data);
            } catch (IOException e) {
        log.info("IOException while extracting " + filePath);
        } finally{
            try {
            if(fos != null){
            fos.flush();
            fos.close();
            }
        } catch (IOException e) {
            log.error("Could not close FileOutputStream", e);
        }
        }
        return data.length;
    };
    }
    @Override
    public void prepareOperation(ExtractAskMode extractAskMode) {
    }
    @Override
    public void setOperationResult(ExtractOperationResult extractOperationResult) {
    }
}


相关文章
|
1月前
|
存储 Java 文件存储
如何用 Java 压缩 ZIP 文件?
【2月更文挑战第21天】
36 1
|
8月前
|
Java Maven Android开发
解决警告: Failed to scan JAR[...] java.util.zip.ZipException: error in opening zip file
解决警告: Failed to scan JAR[...] java.util.zip.ZipException: error in opening zip file
|
5月前
|
Java
Java实现zip文件压缩:单个文件、文件夹以及文件和文件夹的压缩
Java实现zip文件压缩:单个文件、文件夹以及文件和文件夹的压缩
|
6月前
|
JavaScript 前端开发 Java
Java 生成Zip压缩文件,并下载功能
当文件比较大时,为了提高性能生成 压缩包,再下载提高效率。
82 0
|
9月前
|
Java
java(ZipOutputStream)将多个文件打成zip
当想要将两个文件打包成ZIP文件时,可以使用Java的ZipOutputStream类。
378 0
|
11月前
java202303java学习笔记第三十八天解压缩流2
java202303java学习笔记第三十八天解压缩流2
31 0
|
11月前
Java-工具类之ZIP压缩解压
Java-工具类之ZIP压缩解压
307 0
|
11月前
|
Java Linux 数据安全/隐私保护
java zip压缩包并加密
java zip压缩包并加密
735 0
|
11月前
|
Java
java 实现zip 压缩 解压 (目录控制)
java 实现zip 压缩 解压 (目录控制)
269 0