JAVA解压压缩文件到指定路径(文本类)

本文介绍了一种Java方法,用于从指定的ZIP文件中解压所有TXT文件到目标文件夹。通过创建枚举变量遍历ZIP文件条目,检查每个条目是否为TXT文件,然后将其解压到用户指定的文件夹。
//解压压缩文件到指定路径(文本类)
 public void unZipFile(){
        String zipFilePath = "D:\\通知.zip";
        String targerFolder = "D:\\解压文件";
        ZipFile zf = null;
        try {
            zf = new ZipFile(zipFilePath);
            //创建枚举变量
            Enumeration e = zf.entries();
            //遍历枚举变量
            while (e.hasMoreElements()){
                //获得zipentry对象
                ZipEntry entry = (ZipEntry)e.nextElement();
                if(!entry.getName().endsWith(".txt")){
                    continue;
                }
                //利用用户选择的文件夹和zipEntrty对象名称创建解压后的文件
                File currentFile = new File(targerFolder + File.separator + entry.getName());
                FileOutputStream out = null;
                InputStream in = null;
                try {
                    in = zf.getInputStream(entry);
                    out = new FileOutputStream(currentFile);
                    int buffer = 0;
                    while ((buffer = in.read()) != -1){
                        out.write(buffer);
                    }
                }catch (Exception e1){
                    e1.printStackTrace();
                }finally {
                    in.close();
                    out.close();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                zf.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值