ZipOutputStream是个啥

 

在日常的使用中经常会使用到像WinRAR或WinZIP这样的压缩文件,通过这些软件可以把一个很大的文件进行压缩以方便传输。

在JAVA中 为了减少传输时的数据量也提供了专门的压缩流,可以将文件或文件夹压缩成ZIP、JAR、GZIP等文件的格式。

 

 

ZipOutputStream:如果要想完成一个文件或文件夹的压缩,要使用ZipOutputStream类完成,ZipOutputStream是OutputStream的子类。

 

最近在工作中就出现了一个尴尬的情况,我写复制文件的功能时,用的是ZipOutputStream 作为输出流,结果文件出来后比原文件小很多,且在项目中该文件无法使用。最后我选择换了BufferedOutputStream作为输出流,文件才得以正常。

 

注意:各位在选择输入流和输出流时,一定要看清楚该流的用途,谨防出现问题。

 

附上代码:

public static boolean unZipFile(String fileName,String unZipPath,String bankZip){
        log.debug("FILENAME:"+fileName);
        String  bankCopyCertPath = COPY_CERT_PATH + bankZip + "/cert/";
        boolean flag = true;
        File file = new File(fileName);
        if(null == file){

        }
        ZipFile zipFile = null;
        try{
            zipFile = new ZipFile(file,"GBK");
            Enumeration entries = zipFile.getEntries();
            while(entries.hasMoreElements()){
                ZipEntry zipEntry = (ZipEntry) entries.nextElement();
                if(zipEntry.isDirectory()){
                    String name = zipEntry.getName();
                    File f = new File(unZipPath + name);
                    f.mkdirs();
                }else{
                    File f = new File(unZipPath + zipEntry.getName());
                    f.getParentFile().mkdirs();
                    f.createNewFile();
                    //一开始使用的ZipInputStream的输入输出流,导致出现问题                   
//ZipInputStream stream = new ZipInputStream(zipFile.getInputStream(zipEntry));
                    //ZipOutputStream os = new ZipOutputStream(new FileOutputStream(f));
                    BufferedInputStream stream = new BufferedInputStream(zipFile.getInputStream(zipEntry));
                    BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(f));
                    int length = 0;
                    byte[] bts = new byte[1024];
                    //一个一个字节的读取并写入
                    while((length=stream.read(bts, 0, 1024))!=-1)
                    {
                        log.debug("尝试写入:"+bts);
                        os.write(bts,0,length);
                    }
                    os.flush();
                    stream.close();
                    os.close();
                }
            }
            if(null != zipFile){
                zipFile.close();
            }
//            file.delete();
            log.debug("删除压缩文件-------"+file.getName());
        }catch (Exception e){
            log.debug("解压文件发生异常"+e);
            flag = false;
        }
        log.debug("CopyFile参数:"+fileName.substring(0,fileName.lastIndexOf("."))+"---------------"+bankCopyCertPath);
        copyCert(fileName.substring(0,fileName.lastIndexOf(".")),bankCopyCertPath);
        return flag;
    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值