zoukankan      html  css  js  c++  java
  • Java 解压zip压缩包

    因为最近项目需要批量上传文件,而这里的批量就是将文件压缩在了一个zip包里,然后读取文件进行解析文件里的内容。

    因此需要先对上传的zip包进行解压。以下直接提供代码供参考:

    1.第一个方法是用于解压zip压缩包的方法。

    2.第二个方法是 删除该文件夹以及子目录和子目录文件的方法。

    3.第三个方法是 删除 删除文件夹内所有文件和子目录 的方法。

    因为我们一般处理解析完数据之后需要删除上传的文件,以减小服务器的压力,所以提供第二、三方法以供参考。

    我这里的解压zip包的方法返回的是解压后所得到的文件List,大家也可以根据需要返回自己需要的结果,或者不返回都行。

    如果大家有什么更好的方法欢迎留言,请各位多多指教!

      1 package com.forms.oa.weekreport.batchimport.service;
      2 
      3 import java.io.File;
      4 import java.io.FileOutputStream;
      5 import java.io.IOException;
      6 import java.io.InputStream;
      7 import java.util.ArrayList;
      8 import java.util.Enumeration;
      9 import java.util.List;
     10 
     11 import org.apache.tools.zip.ZipEntry;
     12 import org.apache.tools.zip.ZipFile;
     13 
     14 public class FileUtil {
     15     
     16     /**
     17      * 解压zip压缩包并返回解压之后所得到的文件List
     18      *  <br>
     19      * @param zipPath
     20      * @return
     21      * List<File>
     22      */
     23     public static List<File> UnZipFile(String zipPath) {
     24         File file = new File(zipPath);
     25         //设置 压缩包所在的目录下与压缩包同名文件夹 为 解压后的文件所在的目录 
     26         String unZipPath=zipPath.substring(0, zipPath.lastIndexOf("."));   
     27         ZipFile zipFile = null;
     28         List<File> fileList=new ArrayList<File>();
     29         try {
     30             //设置编码格式
     31             zipFile = new ZipFile(file,"GBK");
     32         } catch (IOException e1) {
     33             e1.printStackTrace();
     34         }
     35         Enumeration e = zipFile.getEntries(); 
     36         while(e.hasMoreElements()) { 
     37             ZipEntry zipEntry = (ZipEntry)e.nextElement(); 
     38             if(zipEntry.isDirectory()) { 
     39                 String name = zipEntry.getName(); 
     40                 name = name.substring(0,name.length()-1); 
     41                 File f = new File(unZipPath+File.separator + name); 
     42                 f.mkdirs(); 
     43             } else { 
     44                 File f = new File(unZipPath +File.separator+ zipEntry.getName()); 
     45                 fileList.add(f);
     46                 f.getParentFile().mkdirs(); 
     47                 try {
     48                     f.createNewFile();
     49                     InputStream is = zipFile.getInputStream(zipEntry); 
     50                     FileOutputStream fos = new FileOutputStream(f); 
     51                     int length = 0; 
     52                     byte[] b = new byte[1024]; 
     53                     while((length=is.read(b, 0, 1024))!=-1) { 
     54                         fos.write(b, 0, length); 
     55                     } 
     56                     is.close(); 
     57                     fos.close(); 
     58                 } catch (IOException e1) {
     59                     e1.printStackTrace();
     60                 } 
     61             } 
     62         } 
     63         if (zipFile != null) { 
     64             try {
     65                 zipFile.close();
     66             } catch (IOException e1) {
     67                 e1.printStackTrace();
     68             } 
     69         } 
     70         file.delete();//解压完以后将压缩包删除
     71         return fileList;  //返回解压后所得到的文件list
     72     }
     73     
     74     /**
     75      * 删除该文件夹以及子目录和子目录文件 <br>
     76      * @param folderPath void
     77      */
     78     public static void delFolder(String folderPath) {
     79         try {
     80             delAllFile(folderPath); //删除完里面所有内容  
     81             File path = new File(folderPath);
     82             path.delete(); //删除空文件夹  
     83         } catch (Exception e) {
     84             e.printStackTrace();
     85         }
     86     }
     87 
     88     /**
     89      * 删除文件夹内所有文件和子目录 <br>
     90      * @param path
     91      * @return boolean
     92      */
     93     public static boolean delAllFile(String path) {
     94         boolean flag = false;
     95         File file = new File(path);
     96         if (!file.exists()) {
     97             return flag;
     98         }
     99         if (!file.isDirectory()) {
    100             return flag;
    101         }
    102         String[] tempList = file.list();
    103         File temp = null;
    104         for (int i = 0; i < tempList.length; i++) {
    105             if (path.endsWith(File.separator)) {
    106                 temp = new File(path + tempList[i]);
    107             } else {
    108                 temp = new File(path + File.separator + tempList[i]);
    109             }
    110             if (temp.isFile()) {
    111                 temp.delete();
    112             }
    113             if (temp.isDirectory()) {
    114                 delAllFile(path + File.separator + tempList[i]);//先删除文件夹里面的文件  
    115                 delFolder(path + File.separator + tempList[i]);//再删除空文件夹  
    116                 flag = true;
    117             }
    118         }
    119         return flag;
    120     }
    121 
    122 
    123 }
  • 相关阅读:
    Ubuntu12.04 下安装QQ
    python下sqlite增删查改方法(转)
    你有哪些想要分享的 PyCharm 使用技巧?
    PEP 8
    macos 下安装virtualenv,virtualenvwrapper,然后在pycharm中正常配置方法日志
    最全Python爬虫总结(转载)
    Git 常用命令详解
    Python 扩展技术总结(转)
    使用setup.py安装python包和卸载python包的方法
    IPython3 notebook 成功配置Python2和Python3内核(Kernel)
  • 原文地址:https://www.cnblogs.com/sunshine6/p/8426908.html
Copyright © 2011-2022 走看看