当前位置: 代码迷 >> Java相关 >> ZipException,该如何处理
  详细解决方案

ZipException,该如何处理

热度:63   发布时间:2016-04-22 21:11:52.0
ZipException
文件流出现下面异常,请问是那里出了问题,怎么修正代码啊?

异常内容:
java.util.zip.ZipException:no current ZIP entry
at java.util.zip.ZipPutputStream.write(ZipOutputStream.java:267)
at java.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:202)
at java.nio.cs.StreamEncoder.impClose(StreamEncoder.java:297)
at java.nio.cs.StreamEncoder.close(StreamEncoder.java:130)
at java.io.OutputStreamWriter.close(OutputStreamWriter.java:217)
at java.io.BufferedWriter.close(BufferedWriter.java:249)
at test.batch.DataGet.doWrierZip(DataGet.java:409)
at test.batch.DataGet.excute(DataGet.java:204)
at test.batch.DataGet.main(DataGet.java:1110)

其中倒数第三行指向代码:
boolean isFinish = csvWriter.write(path,zipFileName,csvFileName,DataRS,endtime)

调用writer方法:
public final boolean write(final String path,final String zipFileName,final String csvFileName,final ResultSet DataRS,final String endtime) throws Exception {
    ZipOutputStream zos=null;
    BufferedWriter bw =null;
    ZipEntry file=null;
    int judgeNum=1000;
    try {
        String filePath = path+ zipFileName;
        zos = new ZipPutputStream(New BufferedOutputStream(New FileOutputStream(filePath),Const.BUFFER_SIZE));
        bw = new BufferedWriter(New OutputStreamWriter(zos,"SJIS"));
        file = new ZipEntry(csvFileName);
        zos.putNextEntry(file);
        int timeJudgeCount=0;
        long countData=0;
        while(dataRS.next()){
            timeJudgeCount++;
            String data=getRowData(dataRS);
            bw.write(data);
            if(timeJudgeCount>judgeNum){
                bw.flush();
                Long nowtime=System.currentZTimeMillis();
                if(nowtime>= endtime){
                    zos.colseEntry();
                    bw.close();
                    bw=null;
                    return false;
                }
                timeJudgeCount=0;
            }
            countData++;
        }
        bw.flush();
        zos.closeEntry();
        bw,close;
        bw=null;
        Long nowtime=System.currentZTimeMillis();
        if(nowtime>= endtime){
            return false;
        }
        return true;
    } finally {
        if(bw!=null){
            zos.closeEntry();
            bw.close();
        }
    }


------解决方案--------------------
是不是数据没有,或者流关闭的问题。

/**
     * Writes an array of bytes to the current ZIP entry data. This method
     * will block until all the bytes are written.
     * @param b the data to be written
     * @param off the start offset in the data
     * @param len the number of bytes that are written
     * @exception ZipException if a ZIP file error has occurred
     * @exception IOException if an I/O error has occurred