压缩和解压多级文件夹

压缩和解压多级文件夹(空文件夹除外)

public class Test4 {

    public static void main(String[] args) {
        //压缩
        compression("C:\\Users\\Administrator\\Desktop\\test.zip",new File("C:\\Users\\Administrator\\Desktop\\test"));
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        if (s!=null){

        //解压缩
        decompression("C:\\Users\\Administrator\\Desktop\\test.zip","C:\\Users\\Administrator\\Desktop\\testnew");
    }
    }

    private static void compression(String zipFileName,File targetFile) {
        System.out.println("正在压缩...");
        try {
            //创建压缩流,关联要生成的压缩文件
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
            //高效流包装压缩流
            BufferedOutputStream bos = new BufferedOutputStream(out);

            //压缩目标文件夹,传入 压缩流,要压缩的目标文件夹,目标文件夹名,高效输出流;
            zip(out,targetFile,targetFile.getName(),bos);
            bos.close();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("压缩完成!");
    }

private static void zip(ZipOutputStream zOut, File targetFile,
                            String name, BufferedOutputStream bos) throws IOException {
        //压缩目标文件夹,传入 压缩流zOut,要压缩的目标文件夹targetFile,目标文件夹名name,高效输出流bos;

        //如果是文件夹目录
        if(targetFile.isDirectory()) {
            //获取文件夹子目录
            File[] files = targetFile.listFiles();
            if(targetFile.length() == 0) {
                //处理空目录 文件夹后面有个/
                //把空压缩条目放到压缩流中
                zOut.putNextEntry(new ZipEntry(name + "/"));
            }
            //如果文件夹不为空 递归处理子文件
            for(File f: files){
                //递归处理
                zip(zOut,f,name + "/" + f.getName(),bos);
            }

        }else { //如果不是目录,是文件的话:
            //处理文件 添加条目,将压缩条目放入压缩流
            zOut.putNextEntry(new ZipEntry(name));
            //读取文件,写入关联的zip压缩文件中
            InputStream in = new FileInputStream(targetFile);
            BufferedInputStream bis = new BufferedInputStream(in);
            byte[] bytes = new byte[1024];
            int len = -1;
            while((len = bis.read(bytes)) != -1) {
                bos.write(bytes,0,len);
            }
            bis.close();
        }

    }

    //解压
    private static void decompression(String targetFileName,String parent) {
        //传入要解压的压缩文件名,还有压缩进去的文件夹目录
        System.out.println("正在解压...");
        try {
            //1.构建解压输出流,关联要解压文件
            ZipInputStream zIn = new ZipInputStream(new FileInputStream(targetFileName));
            //压缩条目为空;
            ZipEntry entry = null;
            File file = null;
            //2.循环取得压缩条目,且条目不为空,条目不是文件夹
            while((entry = zIn.getNextEntry()) !=null && !entry.isDirectory()){
                //封装条目文件(要解压进去的父目录+当前条目名);
                file = new File(parent,entry.getName()); //父目录+当前条目名字
                //如果文件不存在
                if(!file.exists()) {
                    //创建此文件的上级目录
                    new File(file.getParent()).mkdirs();
                }
                //3.写文件
                //关联要写入的文件,用高效流写入
                OutputStream out = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(out);
                byte[] bytes = new byte[1024];
                int len = -1;
                while((len = zIn.read(bytes)) != -1) {
                    bos.write(bytes,0,len);
                }
                bos.close();
                System.out.println(file.getAbsolutePath() + "解压成功!");
            }



        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值