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/zhufanfan/p/15016717.html

posted on   白首码农  阅读(1085)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示