JAVA开发示例:ZIP文件、RAR文件的解压
添加依赖
RAR5的解压需要添加依赖
<dependency> <groupId>com.github.axet</groupId> <artifactId>java-unrar</artifactId> <version>1.7.0-8</version> </dependency> <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>
关闭流用到一个IOUtils类,需添加
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.19</version> </dependency>
代码示例
package com.bsmn.util; import net.sf.sevenzipjbinding.ExtractAskMode; import net.sf.sevenzipjbinding.ExtractOperationResult; import net.sf.sevenzipjbinding.IArchiveExtractCallback; import net.sf.sevenzipjbinding.IInArchive; import net.sf.sevenzipjbinding.ISequentialOutStream; import net.sf.sevenzipjbinding.PropID; import net.sf.sevenzipjbinding.SevenZip; import net.sf.sevenzipjbinding.SevenZipException; import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream; import org.apache.commons.compress.utils.IOUtils; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.RandomAccessFile; import java.nio.file.Files; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class FileUtil { public static List<File> unrar(File rarFile, File targetDir) { List<File> targetFiles = new ArrayList<>(); try { if(!targetDir.exists()){ targetDir.mkdirs(); } RandomAccessFile randomAccessFile = new RandomAccessFile(rarFile, "r"); IInArchive archive = SevenZip.openInArchive(null,new RandomAccessFileInStream(randomAccessFile)); int[] in = new int[archive.getNumberOfItems()]; for (int i = 0; i < in.length; i++) { in[i] = i; } archive.extract(in, false, getExtractCallback(archive,targetDir,targetFiles)); } catch (Exception e) { throw new RuntimeException(e); } return targetFiles; } private static IArchiveExtractCallback getExtractCallback(IInArchive inArchive,File targetDir,List<File> targetFiles){ return new IArchiveExtractCallback(){ @Override public void setTotal(long l) throws SevenZipException {} @Override public void setCompleted(long l) throws SevenZipException {} @Override public ISequentialOutStream getStream(int index, ExtractAskMode extractAskMode) throws SevenZipException { final String path = (String) inArchive.getProperty(index, PropID.PATH); final boolean isFolder = (boolean) inArchive.getProperty(index, PropID.IS_FOLDER); return data -> { try { if (!isFolder) { File file = new File(targetDir,path); Files.write(file.toPath(),data); targetFiles.add(file); }else{ File file = new File(targetDir , path); if(!file.exists()){ file.mkdirs(); } } } catch (Exception e) { e.printStackTrace(); } return data.length; }; } @Override public void prepareOperation(ExtractAskMode extractAskMode) throws SevenZipException {} @Override public void setOperationResult(ExtractOperationResult extractOperationResult) throws SevenZipException {} }; } public static List<File> unzip(InputStream inputStream, File targetDir) { List<File> targetFiles = new ArrayList<>(); ZipInputStream zipInputStream = null; try { if(!targetDir.exists()){ targetDir.mkdirs(); } zipInputStream = new ZipInputStream(inputStream); String entryName = null; byte[] buf = new byte[4096]; int readByte = 0; ZipEntry fentry=null; File tempFile; while((fentry=zipInputStream.getNextEntry())!=null){ entryName = fentry.getName(); if (fentry.isDirectory()) { tempFile = new File(targetDir,entryName); if(!tempFile.exists()){ tempFile.mkdirs(); } continue; } File file = new File(targetDir,entryName); OutputStream os = new FileOutputStream(file); while((readByte=zipInputStream.read(buf))!=-1){ os.write(buf, 0, readByte); } IOUtils.closeQuietly(os); targetFiles.add(file); } } catch (Exception e) { throw new RuntimeException("unzip file error",e); }finally { IOUtils.closeQuietly(zipInputStream); } return targetFiles; } }
参考资料
本文来自博客园,作者:白首码农,转载请注明原文链接:https://www.cnblogs.com/bsmn/p/16044562.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下