因为最近项目需要批量上传文件,而这里的批量就是将文件压缩在了一个zip包里,然后读取文件进行解析文件里的内容。
因此需要先对上传的zip包进行解压。以下直接提供代码供参考:
1.第一个方法是用于解压zip压缩包的方法。
2.第二个方法是 删除该文件夹以及子目录和子目录文件的方法。
3.第三个方法是 删除 删除文件夹内所有文件和子目录 的方法。
因为我们一般处理解析完数据之后需要删除上传的文件,以减小服务器的压力,所以提供第二、三方法以供参考。
我这里的解压zip包的方法返回的是解压后所得到的文件List,大家也可以根据需要返回自己需要的结果,或者不返回都行。
如果大家有什么更好的方法欢迎留言,请各位多多指教!
1 1 package com.forms.oa.weekreport.batchimport.service; 2 2 3 3 import java.io.File; 4 4 import java.io.FileOutputStream; 5 5 import java.io.IOException; 6 6 import java.io.InputStream; 7 7 import java.util.ArrayList; 8 8 import java.util.Enumeration; 9 9 import java.util.List; 10 10 11 11 import org.apache.tools.zip.ZipEntry; 12 12 import org.apache.tools.zip.ZipFile; 13 13 14 14 public class FileUtil { 15 15 16 16 /** 17 17 * 解压zip压缩包并返回解压之后所得到的文件List 18 18 * <br> 19 19 * @param zipPath 20 20 * @return 21 21 * List<File> 22 22 */ 23 23 public static List<File> UnZipFile(String zipPath) { 24 24 File file = new File(zipPath); 25 25 //设置 压缩包所在的目录下与压缩包同名文件夹 为 解压后的文件所在的目录 26 26 String unZipPath=zipPath.substring(0, zipPath.lastIndexOf(".")); 27 27 ZipFile zipFile = null; 28 28 List<File> fileList=new ArrayList<File>(); 29 29 try { 30 30 //设置编码格式 31 31 zipFile = new ZipFile(file,"GBK"); 32 32 } catch (IOException e1) { 33 33 e1.printStackTrace(); 34 34 } 35 35 Enumeration e = zipFile.getEntries(); 36 36 while(e.hasMoreElements()) { 37 37 ZipEntry zipEntry = (ZipEntry)e.nextElement(); 38 38 if(zipEntry.isDirectory()) { 39 39 String name = zipEntry.getName(); 40 40 name = name.substring(0,name.length()-1); 41 41 File f = new File(unZipPath+File.separator + name); 42 42 f.mkdirs(); 43 43 } else { 44 44 File f = new File(unZipPath +File.separator+ zipEntry.getName()); 45 45 fileList.add(f); 46 46 f.getParentFile().mkdirs(); 47 47 try { 48 48 f.createNewFile(); 49 49 InputStream is = zipFile.getInputStream(zipEntry); 50 50 FileOutputStream fos = new FileOutputStream(f); 51 51 int length = 0; 52 52 byte[] b = new byte[1024]; 53 53 while((length=is.read(b, 0, 1024))!=-1) { 54 54 fos.write(b, 0, length); 55 55 } 56 56 is.close(); 57 57 fos.close(); 58 58 } catch (IOException e1) { 59 59 e1.printStackTrace(); 60 60 } 61 61 } 62 62 } 63 63 if (zipFile != null) { 64 64 try { 65 65 zipFile.close(); 66 66 } catch (IOException e1) { 67 67 e1.printStackTrace(); 68 68 } 69 69 } 70 70 file.delete();//解压完以后将压缩包删除 71 71 return fileList; //返回解压后所得到的文件list 72 72 } 73 73 74 74 /** 75 75 * 删除该文件夹以及子目录和子目录文件 <br> 76 76 * @param folderPath void 77 77 */ 78 78 public static void delFolder(String folderPath) { 79 79 try { 80 80 delAllFile(folderPath); //删除完里面所有内容 81 81 File path = new File(folderPath); 82 82 path.delete(); //删除空文件夹 83 83 } catch (Exception e) { 84 84 e.printStackTrace(); 85 85 } 86 86 } 87 87 88 88 /** 89 89 * 删除文件夹内所有文件和子目录 <br> 90 90 * @param path 91 91 * @return boolean 92 92 */ 93 93 public static boolean delAllFile(String path) { 94 94 boolean flag = false; 95 95 File file = new File(path); 96 96 if (!file.exists()) { 97 97 return flag; 98 98 } 99 99 if (!file.isDirectory()) { 100100 return flag; 101101 } 102102 String[] tempList = file.list(); 103103 File temp = null; 104104 for (int i = 0; i < tempList.length; i++) { 105105 if (path.endsWith(File.separator)) { 106106 temp = new File(path + tempList[i]); 107107 } else { 108108 temp = new File(path + File.separator + tempList[i]); 109109 } 110110 if (temp.isFile()) { 111111 temp.delete(); 112112 } 113113 if (temp.isDirectory()) { 114114 delAllFile(path + File.separator + tempList[i]);//先删除文件夹里面的文件 115115 delFolder(path + File.separator + tempList[i]);//再删除空文件夹 116116 flag = true; 117117 } 118118 } 119119 return flag; 120120 } 121121 122122 123123 }