java解压缩文件
- zip文件
Java 解压缩zip文件,可以不借助于其他的第三方组件,直接使用java提供的方法通过IO流操作zip文件。代码如下:
import java.io.*;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public static void unzip(String zipFilePath,String destDir)throws IOException {
File destDirectory =new File(destDir);
if(!destDirectory.exists()){
destDirectory.mkdir();
}
try(ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFilePath), Charset.forName("gbk"))){//支持中文名
ZipEntry entry = zipInputStream.getNextEntry();
while(entry!=null){
String filePath = destDir + File.separator + entry.getName();
if(!entry.isDirectory()){
extractFile(zipInputStream,filePath);
}else{
File dir = new File(filePath);
dir.mkdir();
}
zipInputStream.closeEntry();
entry = zipInputStream.getNextEntry();
}
}
}
private static void extractFile(ZipInputStream zipIn,String filePath)throws IOException{
try(FileOutputStream fos = new FileOutputStream(filePath)){
byte[] bytesIn = new byte[4096];
int read = 0;
while((read = zipIn.read(bytesIn)) != -1){
fos.write(bytesIn,0,read);
}
}
}
此外,还可以使用第三方工具包来实现
- 使用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"));
- 使用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文件
java自带的java.util.zip包只支持解压zip格式的压缩文件,不支持解压rar格式的压缩文件,需要使用第三方程序包才能实现,
一些文章中提到,可以使用Apache Commons Compress库来解压rar文件,但是程序包中并没有找到处理rar的类 import org.apache.commons.compress.archivers.rar.RarArchiveEntry;
- 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();
}
- savenzipbinding
<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>
TODO...
- 解压缩工具支持命令行执行时,可以绕开代码实现,通过程序调用解压缩工具,传入相应参数,即可实现相应的解压缩操作,java中的Runtime可以启动程序。需要在服务器中安装解压缩工具。具体更多操作,可查看工具相关资料。
以WinRAR为例:
/**
srcPath:压缩文件路径
unPath:目标路径
*/
public static void decompression(String srcPath,String unPath){
File dir = new File(unPath);
if(!dir.exists()){
dir.mkdirs();
}
StringBuilder sb = new StringBuilder();
try{
//x:,-o+:覆盖
String e = String.format("cmd /c \"%s\\WinRAR.exe\" x -o+ %s %s",new Object[]{rarPath,srcPath,unPath});
Process p=Runtime.getRuntime().exec(e);
BufferedInputStream in = new BufferedInputStream(p.getInputStream());
BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
String tmpStr;
while((tmpStr = inBr.readLine())!=null){
sb.append(tmpStr);
}
if(p.waitFor()!=0){
if(p.exitValue()==1){
System.out.print("commend exe failed!");
}
inBr.close();
in.close();
}
}catch (Exception var4){
var4.printStackTrace();
}
}
Tips:当项目部署在Tomcat生产环境中,可能会出现解压缩不稳定的情况,但是在调试状态下没有问题,这可能是由于tomcat权限不足引起的,调试时,程序是以登陆者的权限运行,通常是admin。当把tomcat注册成服务,部署web应用时,权限可能不足,tomcat运行时,默认使用自己的用户,造成exec程序无法执行。
可在服务器中设置登录身份,提高权限,并允许服务与桌面交互即可。