如何基于java实现解压ZIP TAR等文件
java实现对常用.ZIP,.TAR,.TAR.BZ2,.BZ2,.TAR.GZ,.GZ格式文件的解压。
首先需要引入maven依赖,这里使用的是Apache的压缩工具包common-compress,改工具包支持解压、压缩,此代码中我列举出一个zip的压缩示例,其他格式的只需切换改格式对应的流即可。
对于RAR格式文件的解压,目前该工具包还不支持,希望大家做过的可以多多交流。
org.apache.commons commons-compress 1.19
importorg.apache.commons.compress.archivers.tar.TarArchiveEntry;
importorg.apache.commons.compress.archivers.tar.TarArchiveInputStream;
importorg.apache.commons.compress.archivers.zip.ZipArchiveEntry;
importorg.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
importorg.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
importorg.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
importorg.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
importorg.apache.commons.compress.utils.IOUtils;
importjava.io.*;
/**
*@author:zhangzhiyong
*@description:java实现常见文件格式的解压与压缩
*支持.ZIP.TAR.TAR.BZ2.BZ2.TAR.GZ.GZ
*其他格式compress包也支持,在此基础上开发即可
*另外压缩文件只写了ZIP压缩的方法zipCompression,其他的格式类似,换成对应的文件流即可。
*暂不支持RAR压缩格式,RAR可以用junrar的工具包,但是有缺陷:
*其一:如果压缩文件中有中文名字的文件夹,解压以后文件夹名字是乱码,但是不影响文件夹里面的文件;
*其二:最新WinRar压缩产生的.rar文件可能会无法解压。
*缺陷原因:rar有版权,有些东西没有公开,对解压有一些限制,即使其他解压包也可能有问题,但是建议尝试。
*@date:2020/7/120:44
*/
publicclassCompressionFileUtil{
/**
*@paramfilePath需要解压的zip文件的完成路径。
*@paramunzipPath解压过后生成文件的存放路径
*@description:对zip文件进行解压。
*@return:boolean
*@author:ZZY
*@time:2020/7/214:47
*/
publicstaticbooleanzipUnCompress(StringfilePath,StringunzipPath)throwsIOException{
System.out.println("开始解压ZIP..........");
FileInputStreamfis=null;
ZipArchiveInputStreamzis=null;
try{
Filefile=newFile(filePath);
fis=newFileInputStream(file);
zis=newZipArchiveInputStream(fis);
ZipArchiveEntrynze=null;
while((nze=zis.getNextZipEntry())!=null){
FileOutputStreamos=null;
BufferedOutputStreambos=null;
try{
System.out.println("正在解压....."+nze.getName());
//自动添加File.separator文件路径的分隔符,根据系统判断是\\还是/
Stringdir=unzipPath+File.separator+nze.getName();//解压全路径
System.out.println("dir---"+dir);
Filefile1=null;
if(nze.isDirectory()){
file1=newFile(dir);
file1.mkdirs();
}else{
file1=newFile(dir);
os=newFileOutputStream(file1);
bos=newBufferedOutputStream(os);
/*byte[]bt=newbyte[1024];
intlen=0;
while((len=zis.read(bt,0,1024))!=-1){
bos.write(bt,0,len);
}*/
IOUtils.copy(zis,bos);//作用与上面注释代码一样
}
System.out.println("解压完成......");
}catch(FileNotFoundExceptione){
e.printStackTrace();
returnfalse;
}finally{
if(bos!=null){
bos.close();
}
if(os!=null){
os.close();
}
}
}
}catch(Exceptione){
e.printStackTrace();
returnfalse;
}finally{
if(zis!=null){
zis.close();
}
if(fis!=null){
fis.close();
}
}
returntrue;
}
/**
*@paramfilesPathArray多个文件的绝对路径,是一个数组。
*@paramzipFilePath生成的压缩文件的位置,包括生成的文件名,如D:\zip\test.zip
*@description:将多个文件压缩成ZIP压缩包。
*@return:boolean
*@author:ZZY
*@time:2020/7/214:42
*/
publicstaticbooleanzipCompression(String[]filesPathArray,StringzipFilePath)throwsException{
System.out.println("开始压缩ZIP文件");
ZipArchiveOutputStreamzos=null;
FileOutputStreamfos=null;
try{
fos=newFileOutputStream(newFile(zipFilePath));
zos=newZipArchiveOutputStream(fos);
for(StringfilePath:filesPathArray){
FileInputStreamfis=null;
BufferedInputStreambis=null;
try{
Filefile=newFile(filePath);
//第二个参数如果是文件全路径名,那么压缩时也会将路径文件夹也缩进去;
//我们只压缩目标文件,而不压缩该文件所处位置的相关文件夹,所以这里我们用file.getName()
System.out.println("开始压缩..."+file.getName());
ZipArchiveEntryzae=newZipArchiveEntry(file,file.getName());
zos.putArchiveEntry(zae);
fis=newFileInputStream(file);
bis=newBufferedInputStream(fis);
intcount;
byte[]bt=newbyte[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("压缩完成......");
returntrue;
}
/**
*@paraminputStream每种TAR文件用不同的输入流,unCompress方法中已注明
*@paramunTarPathTAR文件解压后的存放路径
*@description:解压TAR类文件,包括.TAR.TAR.BZ2.TAR.GZ
*@return:void
*@author:ZZY
*@time:2020/7/217:42
*/
publicstaticvoidunTar(InputStreaminputStream,StringunTarPath)throwsIOException{
FileInputStreamfis=null;
TarArchiveInputStreamtis=null;
try{
tis=newTarArchiveInputStream(inputStream);
TarArchiveEntrynte=null;
System.out.println("开始解压......");
while((nte=tis.getNextTarEntry())!=null){
Stringdir=unTarPath+File.separator+nte.getName();
System.out.println("正在解压......"+dir);
FileOutputStreamfos=null;
BufferedOutputStreambos=null;
try{
if(nte.isDirectory()){
Filefile1=newFile(dir);
file1.mkdirs();
}else{
Filefile2=newFile(dir);
fos=newFileOutputStream(file2);
bos=newBufferedOutputStream(fos);
IOUtils.copy(tis,bos);
}
}catch(Exceptione){
e.printStackTrace();
}finally{
if(bos!=null){
bos.close();
}
if(fos!=null){
fos.close();
}
}
}
System.out.println("解压完成......");
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(tis!=null){
tis.close();
}
if(fis!=null){
fis.close();
}
}
}
publicstaticbooleanunCompress(StringfilePath,StringunCompressPath)throwsException{
StringfileType=filePath.toUpperCase();
if(fileType.endsWith(".TAR")){
System.out.println("解压的.TAR包");
//.TAR包用一般的FileInputStream流读取
unTar(newFileInputStream(filePath),unCompressPath);
}
elseif(fileType.endsWith(".TAR.GZ")){
System.out.println("解压的.TAR.GZ包");
//.TAR.GZ包要用GzipCompressorInputStream读取
unTar(newGzipCompressorInputStream(newFileInputStream(filePath)),unCompressPath);
}
elseif(fileType.endsWith(".TAR.BZ2")){
System.out.println("解压的.TAR.BZ2包");
unTar(newBZip2CompressorInputStream(newFileInputStream(filePath)),unCompressPath);
}
elseif(fileType.endsWith(".ZIP")){
System.out.println("解压的.ZIP包");
zipUnCompress(filePath,unCompressPath);
}
else{
System.out.println("暂不支持该种格式文件的解压");
}
returntrue;
}
publicstaticvoidmain(String[]args)throwsException{
unCompress("D:\\test\\zip\\nginx-1.18.0.rar","D:\\test\\zip");
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。