java处理压缩文件

以下是最简单的一个例子

package zip;

import java.util.zip.*;
import java.io.*;

public class TestZip {
    public TestZip() {
    }

  //把zipFile 解压到dir目录中

    public static void  unZip(File zipFile,File dir){
        try {
            String zipdirname = zipFile.getName();
            int off = zipdirname.indexOf(".zip");
            zipdirname = zipdirname.substring(0,off);
            dir = new File(dir,zipdirname);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
            ZipEntry ze ;
            for(ze = zis.getNextEntry() ; ze != null ; ze = zis.getNextEntry()){
                File f = new File(dir,ze.getName());
                f.createNewFile();
                FileOutputStream fo = new FileOutputStream(f);
                for (int i = 0; i < ze.getSize(); i++) {
                    int c = zis.read();
                    fo.write(c);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    public static void main(String[]args) throws Exception{
        File zipFile = new File("c://123.zip");
        File zipdir = new File("c://");

        unZip(zipFile,zipdir);
//        ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
//        ZipEntry ze = zis.getNextEntry();
//        while(ze!=null){
//            System.out.println(ze);
//            for (int i = 0; i < ze.getSize(); i++) {
//                System.out.print((char) zis.read());
//            }
//            ze = zis.getNextEntry();
//        }
//        System.out.println("end");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值