美文网首页
java 批量解压文件及子文件下的zip文件

java 批量解压文件及子文件下的zip文件

作者: OyeAndroid | 来源:发表于2021-02-02 14:04 被阅读0次

    直接上代码:

      public class BaseUtils {
         /**
        * 指定解压单个文件(可循环调用)
        *
        *   @param zipFile 解压的文件路径含文件本身
        * @param descDir 解压到某个路径
        * @Author Maiza
        */
         private final static String dest_path = "E:\\tdmap\\img_w";
    
        public static void unZipFile(File zipFile, String descDir) {
              File pathFile = new File(descDir);
              OutputStream out = null;
             InputStream in = null;
              ZipFile zip = null;
              try {
                    if (!pathFile.exists()) {
                    pathFile.mkdirs();
                 }
            //设置处理压缩包语言
            zip = new ZipFile(zipFile, Charset.forName("UTF-8"));
            //开始循环解压缩
            for (Enumeration entries = zip.entries(); entries.hasMoreElements(); ) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                String zipEntryName = entry.getName();
                in = zip.getInputStream(entry);
                String outPath = descDir + "\\" + zipEntryName;
                out = new FileOutputStream(outPath);
                byte[] buf1 = new byte[1024];
                int len;
                while ((len = in.read(buf1)) > 0) {
                    out.write(buf1, 0, len);
                }
                //每次循环关闭一次流,避免资源泄露.如果不关闭,系统将一直占用资源.
                out.close();
                in.close();
            }
        } catch (Exception e) {
    
        } finally {
            try {
                out.close();
                in.close();
                zip.close();
            } catch (Exception e) {
    
            }
        }
    }
    
    public static void showDir(File dir) {
        if (dir.exists()) {
            //抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件和目录。
            File[] files = dir.listFiles();
            if (null != files) {
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isDirectory()) {
                        showDir(files[i]);
                    } else {
                        System.out.println(files[i]);
                        unZipFile(files[i], dest_path);
                    }
                }
            }
        } else {
            System.out.println("文件不存在!");
        }
    }
    
        public static void main(String[] args) {
            String source_path = "E:\\BaiduNetdiskDownload\\sichuan\\cia_w";
             showDir(new File(source_path));
          }
     }

    相关文章

      网友评论

          本文标题:java 批量解压文件及子文件下的zip文件

          本文链接:https://www.haomeiwen.com/subject/yblytltx.html