解壓縮zip
1.使用hutool工具包中ZipUtil工具類
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.2</version>
</dependency>
使用比較簡單,下面只是其中一種用法(當然還有其他的使用方法帘饶,請自行查看源碼):
ZipUtil.unzip(f.getAbsolutePath(), Charset.forName("GBK"));
參數(shù)是壓縮包路徑和編碼,使用GBK
是為了解決中文解壓縮亂碼的問題群扶,測試使用了其他字符集及刻,解壓縮后中文都是亂碼。
2.使用zip4j工具包
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>2.9.1</version>
</dependency>
使用比hutool工具包稍復雜一點:
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();
}
字符集同樣使用GBK
竞阐,否則解壓后中文也是亂碼缴饭。zipFile.extractAll
是解壓到指定文件夾。
解壓縮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();
}
運行測試馁菜,居然報錯:
23:31:45.233 [main] WARN com.github.junrar.Archive - Support for rar version 5 is not yet implemented!
23:31:45.247 [main] WARN com.github.junrar.Archive - exception in archive constructor maybe file is encrypted, corrupt or support not yet implemented
com.github.junrar.exception.UnsupportedRarV5Exception: null
junrar不支持rar5的解壓縮\畋狻!汪疮!我曹峭火,無情!V侨隆卖丸!
繼續(xù)百度一番,沒有找到其他方案盏道,晚上在簡書上稍浆,努力翻找,找到了新的解決方案猜嘱,如下衅枫!
2.使用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>
原文作者說,某些情況朗伶,由于文件編碼問題弦撩,可能會出現(xiàn)亂碼的情況,但是API上沒有參數(shù)可以設置編碼论皆。但個人示例測試暫時沒有發(fā)現(xiàn)亂碼問題益楼,貼下示例代碼:
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) {
}
}
最后,有興趣還可以看一下Apache的common-compress点晴,http://commons.apache.org/proper/commons-compress/examples.html感凤,也可以對很多類型的壓縮文件進行操作,好像沒有rar格式的A6健E愀汀!