一、解压zip文件:

/**     * 解压缩文件(压缩文件中可含子目录)     * @param zipFilePath 压缩文件完整路径     * @param targetDir  要解压到的地方的完整路径     * @throws IOException     */    public static void unZip(String zipFilePath, String targetDir) throws IOException    {        long startTime = 0, endTime;        int BUFFER = 4096; // 每次读取的缓冲区设为4KB        String strEntry; // zip中文件名称        ZipEntry entry;        ZipFile zipFile = new ZipFile(zipFilePath, "GBK"); // 如果压缩包中有中文命名的文件,加上“GBK”可避免解压缩后出现乱码的情况        Enumeration<?> enu = zipFile.getEntries();        startTime = System.currentTimeMillis();        System.out.println("开始时间: " + startTime);        while (enu.hasMoreElements())        {            entry = (ZipEntry) enu.nextElement();            int count;            byte[] data = new byte[BUFFER];            strEntry = entry.getName();            if (entry.isDirectory())            { // 如果是子文件夹/目录,则在目标路径新建子文件夹/目录                File file = new File(targetDir + File.separator + strEntry);                if (!file.exists()) {                    file.mkdirs();                }            }            else            { // 如果是文件,则直接解压到目标路径                File entryFile = new File(targetDir + "/" + strEntry);                File entryDir = new File(entryFile.getParent());                if (!entryDir.exists())                {                    entryDir.mkdirs();                  }                FileOutputStream fos = new FileOutputStream(entryFile);                OutputStream bufout = new BufferedOutputStream(fos, BUFFER);                InputStream bufis = new BufferedInputStream(zipFile.getInputStream(entry));                while ((count = bufis.read(data, 0, BUFFER)) != -1) {                    bufout.write(data, 0, count);                }                bufout.flush();                bufis.close();                bufout.close();            }        }        endTime = System.currentTimeMillis();        System.out.println("结束时间: " + endTime);        System.out.println("用时 :" + (endTime - startTime) + "ms");        zipFile.close();    }

因为java的api在解压zip包中有中文命名的文件时会出现乱码。所以要用到第三方jar包——ant.jar:

二、压缩文件成.zip:

File zipFile = new File(Environment.getExternalStorageDirectory().toString() + "/target.zip");            File targetDir = new File(Environment.getExternalStorageDirectory().toString() + "/xxx");            ZipOutputStream zos = null;            try {                zos = new ZipOutputStream(new FileOutputStream(zipFile));            } catch (FileNotFoundException e1) {                e1.printStackTrace();            }            File[] entries = targetDir.listFiles();            for (int i = 0; i < entries.length; i++) {                try {                    zipFile(zos, entries[i], "");                } catch (IOException e) {                    e.printStackTrace();                }            }

以下是压缩文件的核心代码:

private void zipFile(ZipOutputStream zos, File fileOrDirectory, String curPath) throws IOException {        FileInputStream fin = null;        try {            if (fileOrDirectory.isFile())             { // 压缩文件                byte[] buffer = new byte[4096];                int bytes_read;                fin = new FileInputStream(fileOrDirectory);                ZipEntry entry = new ZipEntry(curPath + fileOrDirectory.getName());                zos.putNextEntry(entry);                while ((bytes_read = fin.read(buffer)) != -1) {                    zos.write(buffer, 0, bytes_read);                }                zos.closeEntry();            }            else if (fileOrDirectory.isDirectory())            { // 压缩文件夹                File[] entries = fileOrDirectory.listFiles();                for (File file : entries) {                    // 递归压缩,更新curPath                    zipFile(zos, file, curPath + fileOrDirectory.getName() + "/");                }            }        } catch (Exception e) {            e.printStackTrace();        } finally {            if (fin != null) {                fin.close();            }        }    }

同样需要引入Apache 的ant.jar包

更多相关文章

  1. 一款常用的 Squid 日志分析工具
  2. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  3. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  4. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  5. Android(安卓)资源文件的命名规范问题
  6. Android(安卓)蓝牙4.0多蓝牙连接
  7. Android(安卓)编译源码相关
  8. SearchView默认扩展
  9. Android(安卓)studio 3.4.1NDK开发

随机推荐

  1. Android(安卓)打开系统设置
  2. Android(安卓)全局异常错误或崩溃捕捉
  3. Android(安卓)Weekly Notes Issue #232
  4. [android] 将时长数为毫秒的转化为分钟和
  5. Android(安卓)cocos2d-x开发(二)之create-a
  6. [Android(安卓)Pro] 使用CursorLoader异
  7. Android(安卓)Robolectric 加载运行本地
  8. Android(安卓)LCD(三):Samsung LCD接口篇
  9. Android(安卓)的http通信(原生代码)
  10. 通过类名获取对象集合