Android 系统本身的解压功能zip

根据系统本身功能进行解压
优点:无需导入另外的jar包,需要自己思考解压流程,比较复杂
缺点:不能解压加密的zip文件

 

/**
     * 解压操作(包含子目录和文件)
     * @param zipFile
     * @param targetDir
     * @return
     */
    private int Unzip(String zipFile, String targetDir) {
        int BUFFER = 4096; //这里缓冲区我们使用4KB,
        ZipFile zipf = null;
        try {
            zipf = new ZipFile(zipFile);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        Enumeration zList = zipf.entries();
        ZipEntry ze = null;
        byte[] buf = new byte[BUFFER];
        while (zList.hasMoreElements()) {
            ze = (ZipEntry) zList.nextElement();
            // 列举的压缩文件里面的各个文件,判断是否为目录
            if (ze.isDirectory()) {
                String dirstr = targetDir + ze.getName();
                dirstr.trim();
                File f = new File(dirstr);
                f.mkdir();
                continue;
            }
            OutputStream os = null;
            FileOutputStream fos = null;
            // ze.getName()会返回 script/start.script这样的,是为了返回实体的File
            File realFile = getRealFileName(targetDir, ze.getName());
            try {
                fos = new FileOutputStream(realFile);
            } catch (FileNotFoundException e) {

                return 1;
            }
            os = new BufferedOutputStream(fos);
            InputStream is = null;
            try {
                is = new BufferedInputStream(zipf.getInputStream(ze));
            } catch (IOException e) {

                return 2;
            }
            int readLen = 0;
            // 进行一些内容复制操作
            try {
                while ((readLen = is.read(buf, 0, 1024)) != -1) {
                    os.write(buf, 0, readLen);
                }
            } catch (IOException e) {
                return 3;
            }
            try {
                is.close();
                os.close();
            } catch (IOException e) {

                return 4;
            }
        }
        try {
            zipf.close();
        } catch (IOException e) {
            return 5;
        }
        return 6;

    }
/**
     * 给定根目录,返回一个相对路径所对应的实际文件名.
     *
     * @param baseDir
     *            指定根目录
     * @param absFileName
     *            相对路径名,来自于ZipEntry中的name
     * @return java.io.File 实际的文件
     */
    public static File getRealFileName(String baseDir, String absFileName) {
        absFileName = absFileName.replace("\\", "/");
        String[] dirs = absFileName.split("/");
        File ret = new File(baseDir);
        String substr = null;
        if (dirs.length > 1) {
            for (int i = 0; i < dirs.length - 1; i++) {
                substr = dirs[i];
                ret = new File(ret, substr);
            }

            if (!ret.exists())
                ret.mkdirs();
            substr = dirs[dirs.length - 1];
            ret = new File(ret, substr);
            return ret;
        } else {
            ret = new File(ret, absFileName);
        }
        return ret;
    }

猜你喜欢

转载自blog.csdn.net/qwer492915298/article/details/88423679