手机中的Zip格式文件解压和文件夹的压缩

今天主要跟大家介绍下如何通过代码把手机存储中的zip文件解压到指定的地方以及如何对很多文件或者文件夹进行压缩生成zip格式的压缩文件。

1.将某个Zip文件解压到指定目录下:

/**
     * 解压缩一个Zip格式的压缩文件
     *
     * @param zipFile 需要解压缩文件
     * @param folderPath 解压缩的目标目录
     * @throws IOException 当解压缩过程出错时抛出
     */
    public static void upZipFile(File zipFile, String folderPath) throws ZipException, IOException {
        File desDir = new File(folderPath);
        if (!desDir.exists()) {
            desDir.mkdirs();//mkdirs()无论是否父目录存在,都会创建目录成功!
        }
        ZipFile zf = new ZipFile(zipFile);
        for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
             //压缩文件里面的每一个文件或者文件夹都对应一个ZipEntry,通过zf.entries()函数可以把所有的文件或者文件夹对应的ZipEntry都读出来
            ZipEntry entry = ((ZipEntry)entries.nextElement());
            
            String str = folderPath + File.separator + entry.getName();
            Log.i("zip", "file name = " + entry.getName());//打印出压缩文件中的所有文件或者文件夹名
            File desFile = new File(str);
            
            if (!desFile.exists()) {
                File fileParentDir = desFile.getParentFile();
                if (!fileParentDir.exists()) {
                    fileParentDir.mkdirs();
                }
                if(entry.isDirectory()){//用来判断当前的ZipEntry对应的是文件还是文件夹
                	desFile.mkdir();//mkdir()就是创建一个目录,但是前提是创建的目录的父目录一定要存在
                }else{
                	desFile.createNewFile();
                	InputStream in = zf.getInputStream(entry);
                	OutputStream out = new FileOutputStream(desFile);
                	byte buffer[] = new byte[1024 * 1024];
                	int realLength;
                	while ((realLength = in.read(buffer)) > 0) {
                		out.write(buffer, 0, realLength);
                	}
                	in.close();
                	out.close();
                }
            }
        }
    }
现假如手机存储根目录下有个sample.zip,通过调用upZipFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar + "sample.zip"), Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar + "sample");就会将文件解压到根目录的sample文件夹中,打印的log如下:


2.将文件夹压缩成压缩文件并放到指定位置

/**
     * 批量压缩文件(夹)
     *
     * @param resFileList 要压缩的文件(夹)列表
     * @param zipFile 生成的压缩文件
     * @throws IOException 当压缩过程出错时抛出
     */
    public static void zipFiles(Collection<File> resFileList, File zipFile) throws IOException {
        ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(
                zipFile), 1024 * 1024));
        for (File resFile : resFileList) {
            zipFile(resFile, zipout, "");
        }
        zipout.close();
    }

 /**
     * 压缩文件
     *
     * @param resFile 需要压缩的文件(夹)
     * @param zipout 压缩的目的文件
     * @param rootpath 压缩的文件路径
     * @throws FileNotFoundException 找不到文件时抛出
     * @throws IOException 当压缩过程出错时抛出
     */
    private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath)
            throws FileNotFoundException, IOException {
        rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : File.separator)
                + resFile.getName();
        if(!resFile.exists()){
        	return;
        }
        if (resFile.isDirectory()) {
            File[] fileList = resFile.listFiles();
            for (File file : fileList) {
                zipFile(file, zipout, rootpath);
            }
            rootpath = rootpath + File.separatorChar;//添加File.separatorChar表示该ZipEntry对应的是文件夹,也就是此时调用ZipEntry.isDirectory()会返回true
            zipout.putNextEntry(new ZipEntry(rootpath));
        } else {
            byte buffer[] = new byte[1024 * 1024];
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(resFile),
                    BUFF_SIZE);
            zipout.putNextEntry(new ZipEntry(rootpath));
            int realLength;
            while ((realLength = in.read(buffer)) != -1) {
                zipout.write(buffer, 0, realLength);
            }
            in.close();
            zipout.flush();
            zipout.closeEntry();
        }
    }
如果有什么问题或者不对的地方随时欢迎留言,谢谢。。。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值