java 文件夹 tar文件_java解压.ZIP .TAR等常用格式文件

1 importorg.apache.commons.compress.archivers.tar.TarArchiveEntry;2 importorg.apache.commons.compress.archivers.tar.TarArchiveInputStream;3 importorg.apache.commons.compress.archivers.zip.ZipArchiveEntry;4 importorg.apache.commons.compress.archivers.zip.ZipArchiveInputStream;5 importorg.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;6 importorg.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;7 importorg.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;8 importorg.apache.commons.compress.utils.IOUtils;9 import java.io.*;10

11 /**

12 *@author:zhangzhiyong13 * @description: java实现常见文件格式的解压与压缩14 * 支持.ZIP .TAR .TAR.BZ2 .BZ2 .TAR.GZ .GZ15 * 其他格式compress包也支持,在此基础上开发即可16 * 另外压缩文件只写了ZIP压缩的方法zipCompression,其他的格式类似,换成对应的文件流即可。17 * 暂不支持RAR压缩格式,RAR可以用junrar的工具包,但是有缺陷:18 * 其一:如果压缩文件中有中文名字的文件夹,解压以后文件夹名字是乱码,但是不影响文件夹里面的文件;19 * 其二:最新 WinRar 压缩产生的 .rar 文件可能会无法解压。20 * 缺陷原因:rar 有版权,有些东西没有公开,对解压有一些限制,即使其他解压包也可能有问题,但是建议尝试。21 * @date :2020/7/1 20:4422 */

23 public classCompressionFileUtil {24 /**

25 *@paramfilePath 需要解压的zip文件的完成路径。26 *@paramunzipPath 解压过后生成文件的存放路径27 * @description: 对zip文件进行解压。28 *@return: boolean29 *@author: ZZY30 * @time: 2020/7/2 14:4731 */

32 public static boolean zipUnCompress(String filePath, String unzipPath) throwsIOException {33

34 System.out.println("开始解压ZIP..........");35 FileInputStream fis = null;36 ZipArchiveInputStream zis = null;37 try{38 File file = newFile(filePath);39 fis = newFileInputStream(file);40 zis = newZipArchiveInputStream(fis);41 ZipArchiveEntry nze = null;42 while ((nze = zis.getNextZipEntry()) != null) {43 FileOutputStream os = null;44 BufferedOutputStream bos = null;45 try{46 System.out.println("正在解压....." +nze.getName());47 //自动添加File.separator文件路径的分隔符,根据系统判断是\\还是/

48 String dir = unzipPath + File.separator + nze.getName(); //解压全路径

49 System.out.println("dir---" +dir);50 File file1 = null;51 if(nze.isDirectory()) {52 file1 = newFile(dir);53 file1.mkdirs();54 } else{55 file1 = newFile(dir);56 os = newFileOutputStream(file1);57 bos = newBufferedOutputStream(os);58 /*byte [] bt = new byte[1024];59 int len = 0;60 while((len = zis.read(bt,0,1024)) != -1){61 bos.write(bt,0,len);62 }*/

63 IOUtils.copy(zis, bos); //作用与上面注释代码一样

64 }65 System.out.println("解压完成......");66 } catch(FileNotFoundException e) {67 e.printStackTrace();68 return false;69 } finally{70 if (bos != null) {71 bos.close();72 }73 if (os != null) {74 os.close();75 }76 }77 }78 } catch(Exception e) {79 e.printStackTrace();80 return false;81 } finally{82 if (zis != null) {83 zis.close();84 }85 if (fis != null) {86 fis.close();87 }88 }89 return true;90 }91

92 /**

93 *@paramfilesPathArray 多个文件的绝对路径,是一个数组。94 *@paramzipFilePath 生成的压缩文件的位置,包括生成的文件名,如D:\zip\test.zip95 * @description: 将多个文件压缩成ZIP压缩包。96 *@return: boolean97 *@author: ZZY98 * @time: 2020/7/2 14:4299 */

100 public static boolean zipCompression(String[] filesPathArray, String zipFilePath) throwsException {101 System.out.println("开始压缩ZIP文件");102 ZipArchiveOutputStream zos = null;103 FileOutputStream fos = null;104 try{105 fos = new FileOutputStream(newFile(zipFilePath));106 zos = newZipArchiveOutputStream(fos);107 for(String filePath : filesPathArray) {108 FileInputStream fis = null;109 BufferedInputStream bis = null;110 try{111 File file = newFile(filePath);112 //第二个参数如果是文件全路径名,那么压缩时也会将路径文件夹也缩进去;113 //我们只压缩目标文件,而不压缩该文件所处位置的相关文件夹,所以这里我们用file.getName()

114 System.out.println("开始压缩..." +file.getName());115 ZipArchiveEntry zae = newZipArchiveEntry(file, file.getName());116 zos.putArchiveEntry(zae);117 fis = newFileInputStream(file);118 bis = newBufferedInputStream(fis);119 intcount;120 byte[] bt = new byte[1024];121 while ((count = bis.read(bt, 0, 1024)) != -1) {122 zos.write(bt, 0, count);123 }124 } finally{125 zos.closeArchiveEntry();126 if (bis != null)127 bis.close();128 if (fis != null)129 fis.close();130 }131 }132 } finally{133 if (zos != null)134 zos.close();135 if (fos != null)136 fos.close();137 }138 System.out.println("压缩完成......");139 return true;140 }141

142 /**

143 *@paraminputStream 每种TAR文件用不同的输入流,unCompress方法中已注明144 *@paramunTarPath TAR文件解压后的存放路径145 * @description: 解压TAR类文件,包括.TAR .TAR.BZ2 .TAR.GZ146 *@return: void147 *@author: ZZY148 * @time: 2020/7/2 17:42149 */

150 public static void unTar(InputStream inputStream, String unTarPath) throwsIOException {151

152 FileInputStream fis = null;153 TarArchiveInputStream tis = null;154 try{155 tis = newTarArchiveInputStream(inputStream);156 TarArchiveEntry nte = null;157 System.out.println("开始解压......");158 while ((nte = tis.getNextTarEntry()) != null) {159 String dir = unTarPath + File.separator +nte.getName();160 System.out.println("正在解压......" +dir);161 FileOutputStream fos = null;162 BufferedOutputStream bos = null;163 try{164 if(nte.isDirectory()) {165 File file1 = newFile(dir);166 file1.mkdirs();167 } else{168 File file2 = newFile(dir);169 fos = newFileOutputStream(file2);170 bos = newBufferedOutputStream(fos);171 IOUtils.copy(tis, bos);172 }173 } catch(Exception e) {174 e.printStackTrace();175 } finally{176 if (bos != null) {177 bos.close();178 }179 if (fos != null) {180 fos.close();181 }182 }183 }184 System.out.println("解压完成......");185 } catch(IOException e) {186 e.printStackTrace();187 } finally{188 if (tis != null) {189 tis.close();190 }191 if (fis != null) {192 fis.close();193 }194 }195 }196

197 public static boolean unCompress(String filePath,String unCompressPath) throwsException {198 String fileType =filePath.toUpperCase();199 if(fileType.endsWith(".TAR")){200 System.out.println("解压的.TAR包");201 //.TAR包用一般的FileInputStream流读取

202 unTar(newFileInputStream(filePath),unCompressPath);203 }204 else if(fileType.endsWith(".TAR.GZ")){205 System.out.println("解压的.TAR.GZ包");206 //.TAR.GZ包要用GzipCompressorInputStream读取

207 unTar(new GzipCompressorInputStream(newFileInputStream(filePath)),unCompressPath);208 }209 else if(fileType.endsWith(".TAR.BZ2")){210 System.out.println("解压的.TAR.BZ2包");211 unTar(new BZip2CompressorInputStream(newFileInputStream(filePath)),unCompressPath);212 }213 else if(fileType.endsWith(".ZIP")){214 System.out.println("解压的.ZIP包");215 zipUnCompress(filePath,unCompressPath);216 }217 else{218 System.out.println("暂不支持该种格式文件的解压");219 }220 return true;221 }222

223 public static void main(String[] args) throwsException {224

225 unCompress("D:\\test\\zip\\nginx-1.18.0.rar","D:\\test\\zip");226

227 }228

229 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值