解压缩zip
- 使用hutool工具包中ZipUtil工具类
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.2</version>
</dependency>
ZipUtil.unzip(f.getAbsolutePath(), Charset.forName("GBK"));
- 使用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
- 使用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();
}
- 使用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 {
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) {
}
}