java实现对常用.ZIP , .TAR, .TAR.BZ2, .BZ2 ,.TAR.GZ ,.GZ格式文件的解压。
首先需要引入maven依赖,这里使用的是Apache的压缩工具包common-compress,改工具包支持解压、压缩,此代码中我列举出一个zip的压缩示例,其他格式的只需切换改格式对应的流即可。
对于RAR格式文件的解压,目前该工具包还不支持,希望大家做过的可以多多交流。
org.apache.commons commons-compress1.19
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.utils.IOUtils;
import java.io.*;
public class CompressionFileUtil {
public static boolean zipUnCompress(String filePath, String unzipPath) throws IOException {
System.out.println("开始解压ZIP..........");
FileInputStream fis = null;
ZipArchiveInputStream zis = null;
try {
File file = new File(filePath);
fis = new FileInputStream(file);
zis = new ZipArchiveInputStream(fis);
ZipArchiveEntry nze = null;
while ((nze = zis.getNextZipEntry()) != null) {
FileOutputStream os = null;
BufferedOutputStream bos = null;
try {
System.out.println("正在解压....." + nze.getName());
//自动添加File.separator文件路径的分隔符,根据系统判断是\还是/
String dir = unzipPath + File.separator + nze.getName(); //解压全路径
System.out.println("dir---" + dir);
File file1 = null;
if (nze.isDirectory()) {
file1 = new File(dir);
file1.mkdirs();
} else {
file1 = new File(dir);
os = new FileOutputStream(file1);
bos = new BufferedOutputStream(os);
IOUtils.copy(zis, bos); //作用与上面注释代码一样
}
System.out.println("解压完成......");
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} finally {
if (bos != null) {
bos.close();
}
if (os != null) {
os.close();
}
}
}
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (zis != null) {
zis.close();
}
if (fis != null) {
fis.close();
}
}
return true;
}
public static boolean zipCompression(String[] filesPathArray, String zipFilePath) throws Exception {
System.out.println("开始压缩ZIP文件");
ZipArchiveOutputStream zos = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(zipFilePath));
zos = new ZipArchiveOutputStream(fos);
for (String filePath : filesPathArray) {
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
File file = new File(filePath);
// 第二个参数如果是文件全路径名,那么压缩时也会将路径文件夹也缩进去;
// 我们只压缩目标文件,而不压缩该文件所处位置的相关文件夹,所以这里我们用file.getName()
System.out.println("开始压缩..." + file.getName());
ZipArchiveEntry zae = new ZipArchiveEntry(file, file.getName());
zos.putArchiveEntry(zae);
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
int count;
byte[] bt = new byte[1024];
while ((count = bis.read(bt, 0, 1024)) != -1) {
zos.write(bt, 0, count);
}
} finally {
zos.closeArchiveEntry();
if (bis != null)
bis.close();
if (fis != null)
fis.close();
}
}
} finally {
if (zos != null)
zos.close();
if (fos != null)
fos.close();
}
System.out.println("压缩完成......");
return true;
}
public static void unTar(InputStream inputStream, String unTarPath) throws IOException {
FileInputStream fis = null;
TarArchiveInputStream tis = null;
try {
tis = new TarArchiveInputStream(inputStream);
TarArchiveEntry nte = null;
System.out.println("开始解压......");
while ((nte = tis.getNextTarEntry()) != null) {
String dir = unTarPath + File.separator + nte.getName();
System.out.println("正在解压......" + dir);
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
if (nte.isDirectory()) {
File file1 = new File(dir);
file1.mkdirs();
} else {
File file2 = new File(dir);
fos = new FileOutputStream(file2);
bos = new BufferedOutputStream(fos);
IOUtils.copy(tis, bos);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
bos.close();
}
if (fos != null) {
fos.close();
}
}
}
System.out.println("解压完成......");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (tis != null) {
tis.close();
}
if (fis != null) {
fis.close();
}
}
}
public static boolean unCompress(String filePath,String unCompressPath) throws Exception {
String fileType = filePath.toUpperCase();
if(fileType.endsWith(".TAR")){
System.out.println("解压的.TAR包");
//.TAR包用一般的FileInputStream流读取
unTar(new FileInputStream(filePath),unCompressPath);
}
else if(fileType.endsWith(".TAR.GZ")){
System.out.println("解压的.TAR.GZ包");
//.TAR.GZ包要用GzipCompressorInputStream读取
unTar(new GzipCompressorInputStream(new FileInputStream(filePath)),unCompressPath);
}
else if(fileType.endsWith(".TAR.BZ2")){
System.out.println("解压的.TAR.BZ2包");
unTar(new BZip2CompressorInputStream(new FileInputStream(filePath)),unCompressPath);
}
else if(fileType.endsWith(".ZIP")){
System.out.println("解压的.ZIP包");
zipUnCompress(filePath,unCompressPath);
}
else{
System.out.println("暂不支持该种格式文件的解压");
}
return true;
}
public static void main(String[] args) throws Exception {
unCompress("D:\test\zip\nginx-1.18.0.rar","D:\test\zip");
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



