android 解压缩zip包

项目里面需要解压缩zip包;或者把项目assets里面的zip包拷贝到手机SD卡里面进行解压。
在这里我是使用的ant.jar这个库:ant.jar

废话不多说,直接附上整个工具类及使用方法:

public class UnZip{

    /**
     * 从项目assets文件夹拷贝到手机sd卡
     * @param context
     */
    public static void CopyAssets(Context context, String zipName) {
        AssetManager assetManager = context.getAssets();

        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(zipName);
            out = new FileOutputStream(
                    android.os.Environment.getExternalStorageDirectory() + "/"
                            + zipName);
            CopyFile(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (Exception e) {
            Log.e("Test", e.getMessage());
        }
    }

    private static void CopyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buf = new byte[1024];
        int n;
        while ((n = in.read(buf)) != -1) {
            out.write(buf, 0, n);
        }
    }

    /**
     * 解压指定zip文件
     * 
     * @param unZipfile
     *            压缩文件的路径
     * @param destFile
     *               解压到的目录 
     */
    public static void unZip(Context context, String unZipfile, String destFile)
            throws IOException, FileNotFoundException, ZipException {

        BufferedInputStream bi;
        ZipFile zipFile = new ZipFile(new File(unZipfile), "GBK");

        @SuppressWarnings("rawtypes")
        Enumeration e = zipFile.getEntries();
        while (e.hasMoreElements()) {
            ZipEntry ze2 = (ZipEntry) e.nextElement();
            String entryName = ze2.getName();
            String path = destFile + "/" + entryName;
            if (ze2.isDirectory()) {
                System.out.println("正在创建解压目录 - " + entryName);
                File decompressDirFile = new File(path);
                if (!decompressDirFile.exists()) {
                    decompressDirFile.mkdirs();
                }
            } else {
                System.out.println("正在创建解压文件 - " + entryName);
                String fileDir = path.substring(0, path.lastIndexOf("/"));
                File fileDirFile = new File(fileDir);
                if (!fileDirFile.exists()) {
                    fileDirFile.mkdirs();
                }
                BufferedOutputStream bos = new BufferedOutputStream(
                        new FileOutputStream(destFile + "/" + entryName));
                bi = new BufferedInputStream(zipFile.getInputStream(ze2));
                byte[] readContent = new byte[1024];
                int readCount = bi.read(readContent);
                while (readCount != -1) {
                    bos.write(readContent, 0, readCount);
                    readCount = bi.read(readContent);
                }
                bos.close();
            }
        }
        zipFile.close();
    }

    /**
     * 压缩文件
     * 
     * @param srcFile
     *      需要 压缩的目录或者文件
     * @param destFile
     *             压缩文件的路径
     */
    public static void doZip(String srcFile, String destFile) {// zipDirectoryPath:需要压缩的文件夹名
        File zipFile = new File(srcFile);
        try {
            // 生成ZipOutputStream,会把压缩的内容全都通过这个输出流输出,最后写到压缩文件中去
            ZipOutputStream zipOut = new ZipOutputStream(
                    new BufferedOutputStream(new FileOutputStream(destFile)));
            // 设置压缩的注释
            zipOut.setComment("comment");
            // 设置压缩的编码,如果要压缩的路径中有中文,就用下面的编码
            zipOut.setEncoding("GBK");
            // 启用压缩
            zipOut.setMethod(ZipOutputStream.DEFLATED);

            // 压缩级别为最强压缩,但时间要花得多一点
            zipOut.setLevel(Deflater.BEST_COMPRESSION);

            handleFile(zipFile, zipOut, "");
            // 处理完成后关闭我们的输出流
            zipOut.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    /**
     * 由doZip调用,递归完成目录文件读取
     * 
     * @param zipFile
     * @param zipOut
     * @param dirName
     *            这个主要是用来记录压缩文件的一个目录层次结构的
     * @throws IOException
     */
    private static void handleFile(File zipFile, ZipOutputStream zipOut, String dirName)
            throws IOException {
        System.out.println("遍历文件:" + zipFile.getName());
        // 如果是一个目录,则遍历
        if (zipFile.isDirectory()) {
            File[] files = zipFile.listFiles();

            if (files.length == 0) {// 如果目录为空,则单独创建之.
                // 只是放入了空目录的名字
                zipOut.putNextEntry(new ZipEntry(dirName + zipFile.getName()
                        + File.separator));
                zipOut.closeEntry();
            } else {// 如果目录不为空,则进入递归,处理下一级文件
                for (File file : files) {
                    // 进入递归,处理下一级的文件
                    handleFile(file, zipOut, dirName + zipFile.getName()
                            + File.separator);
                }
            }
        }
        // 如果是文件,则直接压缩
        else {
            FileInputStream fileIn = new FileInputStream(zipFile);
            // 放入一个ZipEntry
            zipOut.putNextEntry(new ZipEntry(dirName + zipFile.getName()));
            int length = 0;
            byte[] buffer = new byte[1024];
            // 放入压缩文件的流
            while ((length = fileIn.read(buffer)) > 0) {
                zipOut.write(buffer, 0, length);
            }
            // 关闭ZipEntry,完成一个文件的压缩
            zipOut.closeEntry();
        }

    }
}

场景一、从项目assets文件夹拷贝zip包到手机sd卡并解压。

1、首先拷贝zip包至sd卡:UnZip.CopyAssets(this, xxx.zip);

‘xxx.zip’参数就是压缩包的名字加后缀,这里我的包就放在assets根目录,拷贝到sd卡根目录。

2、解压拷贝至sd卡的zip包:UnZip.unZip(this, ZipFileName, UnZipLocation);

'ZipFileName' 就是zip所在的位置路径,
如:android.os.Environment.getExternalStorageDirectory() + "/xxx.zip"
'UnZipLocation' 就是需要解压到的位置,
如:android.os.Environment.getExternalStorageDirectory() + "/"

至此一个zip包就从项目里面拷贝到sd中并解压了。当然实际项目中还得判断是否有内存卡,是否文件存在等。

场景二、将sd卡中的文件夹压缩成zip包:

UnZip.doZip(FileName, ZipFileName);

‘FileName’ 就是你sd卡中文件夹路径,不管里面有多少个子文件或子文件夹都可以。这里我的路径是:
android.os.Environment.getExternalStorageDirectory() + “/文件夹名”

‘ZipFileName’ 就是需要压缩成zip包的路径。如:android.os.Environment.getExternalStorageDirectory() + “/文件夹名.zip”

好了,文件的解压缩操作大致就这些了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值