java直接读取Zip压缩包内的文件

//zip数据处理
public List<Map<String,Object>> readZipFile(String file,ChironUser user) throws Exception {
    //ZipFile zf = new ZipFile(file);
    int i=0;
    List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
    URL url = new URL(file);
    URLConnection connection = url.openConnection();
    InputStream in = connection.getInputStream();//new BufferedInputStream(new FileInputStream(file));

    ZipInputStream zin = new ZipInputStream(in);
    ZipEntry ze;
    while ((ze = zin.getNextEntry()) != null) {
        Integer fail=0;
        String msg="";
        if (ze.isDirectory()) {
        } else {
            i++;
            ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
            byte[] byte_s=new byte[1024];
            int num=-1;
            while((num=zin.read(byte_s,0,byte_s.length))>-1){//通过read方法来读取文件内容
                byteArrayOutputStream.write(byte_s,0,num);
            }
            byte[] byte_s_=byteArrayOutputStream.toByteArray();
            Map<String,Object> m=new HashedMap();
//可以不要------
            if(ze.getName().substring(ze.getName().indexOf(".")).equals(".png") || ze.getName().substring(ze.getName().indexOf(".")).equals(".jpg") ||
            ze.getName().substring(ze.getName().indexOf(".")).equals(".gif") || ze.getName().substring(ze.getName().indexOf(".")).equals(".bmp") ||
                ze.getName().substring(ze.getName().indexOf(".")).equals(".jpeg")){
                if(ze.getName() != null){
                    if(getStudentCot(user,ze.getName().substring(3,ze.getName().indexOf(".")))==0){
                        if(user.getTypeId().equals(10003)){
                            fail++;
                            msg += "<br/>错误:行" + i + " :您不是该学生的辅导员";
                        }else {
                            fail++;
                            msg += "<br/>错误:行" + i + " :未找到该学生或该学生没有辅导员";
                        }

                    }
                }else {
                    fail++;
                    msg += "<br/>错误:行" + i + " :未找到该学生";
                }
            }else {
                fail++;
                msg += "<br/>错误:行" + i + " :只支持 png , jpg , gif , bmp , jpeg格式图片上传";
            }
//-----可以不要  到这
            m.put("fail",fail);
            m.put("msg",msg);
            m.put("idCode",ze.getName());
            m.put("bytes",byte_s_);//压缩包中的单个byte
            list.add(m);
            //String fileNeirong=new String(byte_s,"UTF-8");//将字节数组转化为字符串,UTF-8格式(容许中文)
            //System.out.println(fileNeirong);

        }
    }
    zin.closeEntry();
    return  list;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值