1. 程式人生 > >Java 解壓zip壓縮包

Java 解壓zip壓縮包

ice gin inpu ret zipentry .get get next exce

因為最近項目需要批量上傳文件,而這裏的批量就是將文件壓縮在了一個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 }

Java 解壓zip壓縮包