zoukankan      html  css  js  c++  java
  • Java操作zip压缩和解压缩文件工具类

    需要用到ant.jar(这里使用的是ant-1.6.5.jar)

      1 import java.io.File;
      2 import java.io.FileInputStream;
      3 import java.io.FileNotFoundException;
      4 import java.io.FileOutputStream;
      5 import java.io.IOException;
      6 import java.io.InputStream;
      7 import java.io.OutputStream;
      8 import java.util.ArrayList;
      9 import java.util.Enumeration;
     10 import java.util.List;
     11 
     12 import org.apache.tools.zip.ZipEntry;
     13 import org.apache.tools.zip.ZipFile;
     14 import org.apache.tools.zip.ZipOutputStream;
     15 
     16 
     17 /**
     18  * 压缩或解压zip:
     19  * 由于直接使用java.util.zip工具包下的类,会出现中文乱码问题,所以使用ant.jar中的org.apache.tools.zip下的工具类
     20  * @author Administrator
     21  */
     22 
     23 public class ZipUtil {
     24     private static byte[] _byte = new byte[1024] ;
     25     /**
     26      * 压缩文件或路径
     27      * @param zip 压缩的目的地址
     28      * @param srcFiles 压缩的源文件
     29      */
     30     public static void zipFile( String zip , List<File> srcFiles ){
     31         try {
     32             if( zip.endsWith(".zip") || zip.endsWith(".ZIP") ){
     33                 ZipOutputStream _zipOut = new ZipOutputStream(new FileOutputStream(new File(zip))) ;
     34                 _zipOut.setEncoding("GBK");
     35                 for( File _f : srcFiles ){
     36                     handlerFile(zip , _zipOut , _f , "");
     37                 }
     38                 _zipOut.close();
     39             }else{
     40                 System.out.println("target file[" + zip + "] is not .zip type file");
     41             }
     42         } catch (FileNotFoundException e) {
     43         } catch (IOException e) {
     44         }
     45     }
     46     
     47     /**
     48      * 
     49      * @param zip 压缩的目的地址
     50      * @param zipOut 
     51      * @param srcFile  被压缩的文件信息
     52      * @param path  在zip中的相对路径
     53      * @throws IOException
     54      */
     55     private static void handlerFile(String zip , ZipOutputStream zipOut , File srcFile , String path ) throws IOException{
     56         System.out.println(" begin to compression file[" + srcFile.getName() + "]");
     57         if( !"".equals(path) && ! path.endsWith(File.separator)){
     58             path += File.separator ;
     59         }
     60         if( ! srcFile.getPath().equals(zip) ){
     61             if( srcFile.isDirectory() ){
     62                 File[] _files = srcFile.listFiles() ;
     63                 if( _files.length == 0 ){
     64                     zipOut.putNextEntry(new ZipEntry( path + srcFile.getName() + File.separator));
     65                     zipOut.closeEntry();
     66                 }else{
     67                     for( File _f : _files ){
     68                         handlerFile( zip ,zipOut , _f , path + srcFile.getName() );
     69                     }
     70                 }
     71             }else{
     72                 InputStream _in = new FileInputStream(srcFile) ;
     73                 zipOut.putNextEntry(new ZipEntry(path + srcFile.getName()));
     74                 int len = 0 ; 
     75                 while( (len = _in.read(_byte)) > 0  ){
     76                     zipOut.write(_byte, 0, len);
     77                 }
     78                 _in.close();
     79                 zipOut.closeEntry();
     80             }
     81         }
     82     }
     83 
     84     /**
     85      * 解压缩ZIP文件,将ZIP文件里的内容解压到targetDIR目录下
     86      * @param zipName 待解压缩的ZIP文件名
     87      * @param targetBaseDirName  目标目录
     88      */
     89     public static List<File> upzipFile(String zipPath, String descDir) {
     90         return upzipFile( new File(zipPath) , descDir ) ;
     91     }
     92     
     93     /**
     94      * 对.zip文件进行解压缩
     95      * @param zipFile  解压缩文件
     96      * @param descDir  压缩的目标地址,如:D:\测试 或 /mnt/d/测试
     97      * @return
     98      */
     99     @SuppressWarnings("rawtypes")
    100     public static List<File> upzipFile(File zipFile, String descDir) {
    101         List<File> _list = new ArrayList<File>() ;
    102         try {
    103             ZipFile _zipFile = new ZipFile(zipFile , "GBK") ;
    104             for( Enumeration entries = _zipFile.getEntries() ; entries.hasMoreElements() ; ){
    105                 ZipEntry entry = (ZipEntry)entries.nextElement() ;
    106                 File _file = new File(descDir + File.separator + entry.getName()) ;
    107                 if( entry.isDirectory() ){
    108                     _file.mkdirs() ;
    109                 }else{
    110                     File _parent = _file.getParentFile() ;
    111                     if( !_parent.exists() ){
    112                         _parent.mkdirs() ;
    113                     }
    114                     InputStream _in = _zipFile.getInputStream(entry);
    115                     OutputStream _out = new FileOutputStream(_file) ;
    116                     int len = 0 ;
    117                     while( (len = _in.read(_byte)) > 0){
    118                         _out.write(_byte, 0, len);
    119                     }
    120                     _in.close(); 
    121                     _out.flush();
    122                     _out.close();
    123                     _list.add(_file) ;
    124                 }
    125             }
    126         } catch (IOException e) {
    127         }
    128         return _list ;
    129     }
    130     
    131     /**
    132      * 对临时生成的文件夹和文件夹下的文件进行删除
    133      */
    134     public static void deletefile(String delpath) {
    135         try {
    136             File file = new File(delpath);
    137             if (!file.isDirectory()) {
    138                 file.delete();
    139             } else if (file.isDirectory()) {
    140                 String[] filelist = file.list();
    141                 for (int i = 0; i < filelist.length; i++) {
    142                     File delfile = new File(delpath + File.separator + filelist[i]);
    143                     if (!delfile.isDirectory()) {
    144                         delfile.delete();
    145                     } else if (delfile.isDirectory()) {
    146                         deletefile(delpath + File.separator + filelist[i]);
    147                     }
    148                 }
    149                 file.delete();
    150             }
    151         } catch (Exception e) {
    152             e.printStackTrace();
    153         }
    154     }
    155     
    156     public static void main(String[] args) {}
    157     
    158 }
  • 相关阅读:
    redis 下载安装
    Docker之网络
    容器与镜像
    Docker之容器
    Docker之镜像
    在安装完windows和linux双系统后,删除BIOS中的引导启动项在Window下
    Ububtu 18.04中如何在火狐浏览器中安装flash插件
    lambda 表达式
    IDEA DeBug mode
    spring 系统启动加载的类
  • 原文地址:https://www.cnblogs.com/DreamDrive/p/5760477.html
Copyright © 2011-2022 走看看