• 解压zip文件到指定目录


    代码很简单,但要注意解压的时候排除__MACOSX目录

    /**
         * 解压zip文件到指定目录
         * unzip(new File("1.zip"),new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"test"))
         */
        public static void unzip(File source, File dest) throws IOException {
            ZipFile zipFile = new ZipFile(source);
            try {
                if(!dest.exists()){
                    dest.mkdirs();
                }
                ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(source)));
                ZipEntry entry;
                byte[] buffer = new byte[1024];
                while((entry=zis.getNextEntry())!=null) {
                    String filename = entry.getName();
                    //排除MACOS环境下生成的隐藏文件
                    if (filename.contains("__MACOSX")) {
                    }
                    else {
                        if (entry.isDirectory()) {
                            new File(dest,filename).mkdirs();
                            continue;
                        }
                        InputStream inputStream=zipFile.getInputStream(entry);
                        int len;
                        try (FileOutputStream outputStream =new FileOutputStream(new File(dest, filename))) {
                            while ((len = inputStream.read(buffer)) >= 0) {
                                outputStream.write(buffer, 0, len);
                            }
                            outputStream.flush();
                            inputStream.close();
                        }
                    }
                    zis.closeEntry();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                zipFile.close();
            }
        }
  • 相关阅读:
    课后作业
    动手动脑
    原码,补码,反码
    CodingSouls团队项目冲刺-个人概况(7)
    《人月神话》阅读笔记03
    数据库学习
    家庭小账本——数据库的编写与测试
    家庭小账本——适配器的编写与测试
    UI高级组件
    UI高级组件
  • 原文地址:https://www.cnblogs.com/rainboy2010/p/13297752.html
Copyright © 2020-2023  润新知