java对zip文件操作的工具类

  1 import org.apache.commons.io.IOUtils;
  2 
  3 import org.apache.tools.zip.ZipEntry;
  4 import org.apache.tools.zip.ZipFile;
  5 import org.apache.tools.zip.ZipOutputStream;
  6 
  7 import java.io.*;
  8 import java.util.ArrayList;
  9 import java.util.Enumeration;
 10 import java.util.List;
 11 
 12 /**
 13   * @Description: java操作zip文件工具类
 14   * @Version: 1.0
 15   */
 16 public class ZipFileUtil {
 17 
 18   private static final String ENCODE_UTF_8 = "UTF-8";
 19 
 20   /**
 21     * @param zip 压缩的目的地址 例如:D://zipTest.zip
 22     * @param srcFiles 压缩的源文件
 23     * @description: 压缩文件或路径
 24     * @version: 1.0
 25     */
 26   public static void zipFile(String zip, List<File> srcFiles) {
 27     try {
 28       // 判断是否为压缩后的文件后缀是否为.zip结尾
 29       if (zip.endsWith(".zip") || zip.endsWith(".ZIP")) {
 30         ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(new File(zip)));
 31         zipOut.setEncoding(ENCODE_UTF_8);// 设置编码
 32         for (File file : srcFiles) {
 33           zipFile(zip, zipOut, file, "");
 34         }
 35         zipOut.close();
 36       } else {
 37         throw new RuntimeException("target file[zip] is not .zip type file");
 38       }
 39     } catch (FileNotFoundException e) {
 40       throw new RuntimeException(e.getMessage());
 41     } catch (IOException e) {
 42       throw new RuntimeException(e.getMessage());
 43     }
 44   }
 45 
 46   /**
 47     * @param zip 压缩的目的地址 例如:D://zipTest.zip
 48     * @param zipOut
 49     * @param srcFile 被压缩的文件
 50     * @param path 在zip中的相对路径
 51     * @version: 1.0
 52     */
 53   private static void zipFile(String zip, ZipOutputStream zipOut, File srcFile, String path) throws IOException {
 54     System.out.println(" 开始压缩文件[" + srcFile.getName() + "]");
 55     if (!"".equals(path) && !path.endsWith(File.separator)) {
 56       path += File.separator;
 57     }
 58     if (!srcFile.exists()) {// 测试此抽象路径名定义的文件或目录是否存在
 59       throw new RuntimeException("压缩失败,文件或目录 " + srcFile + " 不存在!");
 60     } else {
 61       if (!srcFile.getPath().equals(zip)) {
 62         if (srcFile.isDirectory()) {
 63           // listFiles能够获取当前文件夹下的所有文件和文件夹,如果文件夹A下还有文件D,那么D也在childs里。
 64           File[] files = srcFile.listFiles();
 65           if (files.length == 0) {
 66             zipOut.putNextEntry(new ZipEntry(path + srcFile.getName() + File.separator));
 67             zipOut.closeEntry();
 68           } else {
 69             for (File file : files) {
 70               zipFile(zip, zipOut, file, path + srcFile.getName());
 71             }
 72           }
 73         } else {
 74           FileInputStream in = new FileInputStream(srcFile);
 75           zipOut.putNextEntry(new ZipEntry(path + srcFile.getName()));
 76           IOUtils.copy(in, zipOut);
 77           in.close();
 78           zipOut.closeEntry();
 79         }
 80       }
 81     }
 82   }
 83 
 84   /**
 85     * @param zipPath 待解压缩的ZIP文件路径
 86     * @param descDir 目标目录
 87     * @return List<File>
 88     * @description: 解压缩ZIP文件,将ZIP文件里的内容解压到targetDIR目录下
 89     * @version: 1.0
 90     */
 91   public static List<File> upzipFile(String zipPath, String descDir) {
 92     return upzipFile(new File(zipPath), descDir);
 93   }
 94 
 95   /**
 96     * @param zipFile 解压缩文件
 97     * @param descDir 压缩的目标地址,如:D:\\测试 或 /mnt/d/测试
 98     * @description: 对.zip文件进行解压缩
 99     * @version: 1.0
100     */
101   @SuppressWarnings({"rawtypes", "null"})
102   public static List<File> upzipFile(File zipFile, String descDir) {
103     List<File> list = new ArrayList<>();
104     // 防止文件名中有中文时出错
105     System.setProperty("sun.zip.encoding", System.getProperty("sun.jnu.encoding"));
106     try {
107       if (!zipFile.exists()) {
108         throw new RuntimeException("解压失败,文件 " + zipFile + " 不存在!");
109       }
110       ZipFile zFile = new ZipFile(zipFile, "GBK");
111       InputStream in = null;
112       OutputStream out = null;
113       for (Enumeration entries = zFile.getEntries(); entries.hasMoreElements();) {
114         ZipEntry entry = (ZipEntry) entries.nextElement();
115         File file = new File(descDir + File.separator + entry.getName());
116         if (entry.isDirectory()) {
117           file.mkdirs();
118         } else {
119           File parent = file.getParentFile();
120           if (!parent.exists()) {
121             parent.mkdirs();
122           }
123           in = zFile.getInputStream(entry);
124           out = new FileOutputStream(file);
125           IOUtils.copy(in, out);
126           out.flush();
127           list.add(file);
128         }
129       }
130       zFile.close();
131       in.close();
132       out.close();
133     } catch (IOException e) {
134       throw new RuntimeException(e.getMessage());
135     }
136     return list;
137   }
138 
139   /**
140     * @description: 对临时生成的文件夹和文件夹下的文件进行删除
141     * @param: delpath
142     * @version: 1.0
143     */
144   public static void deletefile(String delpath) {
145     try {
146       File file = new File(delpath);
147       if (!file.isDirectory()) {// 判断是不是一个目录
148         file.delete();
149       } else if (file.isDirectory()) {
150         String[] filelist = file.list();
151         for (int i = 0; i < filelist.length; i++) {
152           File delfile = new File(delpath + File.separator + filelist[i]);
153           if (!delfile.isDirectory()) {
154             delfile.delete();
155           } else if (delfile.isDirectory()) {
156             deletefile(delpath + File.separator + filelist[i]);// 递归删除
157           }
158         }
159         file.delete();
160       }
161     } catch (Exception e) {
162       e.printStackTrace();
163     }
164   }
165 
166  
167 
168   /**
169     * @description: 测试方法
170     * @version: 1.0
171     */
172 
173   public static void main(String[] args) {
174     String zip = "D://zipTest.zip";
175     List<File> srcFiles = new ArrayList<>();
176     srcFiles.add(new File("D://testZip"));
177     srcFiles.add(new File("D://999"));
178     zipFile(zip, srcFiles);
179     List<File> list = upzipFile("D://zipTest.zip", "D://Solin");
180     for (File file : list) {
181       System.out.println(file.getName());
182     }
183     // deletefile(zip);
184   }
185 }