ZipException

booboy2563 2014-04-13 11:59:53
文件流出现下面异常,请问是那里出了问题,怎么修正代码啊?

异常内容:
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();
}
}

...全文
344 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
booboy2563 2014-04-23
  • 打赏
  • 举报
回复
问题查到了,是因为文件目录空间不足,导致write时异常发生,但没有进行捕获,在执行finally时,流close,再次发生异常,致使该异常向上抛出,所以出现上面异常。
MiceRice 2014-04-18
  • 打赏
  • 举报
回复
你贴的异常是: at test.batch.DataGet.doWrierZip(DataGet.java:409) 但是给的代码是: boolean isFinish = csvWriter.write(path,zipFileName,csvFileName,DataRS,endtime) 貌似两者没啥关系啊。
tony4geek 2014-04-17
  • 打赏
  • 举报
回复
具体代码,真卡不出来。你网上下载个jar ,可以进入调试源码的,然后具体看看那一步出错。
booboy2563 2014-04-17
  • 打赏
  • 举报
回复
好像只要不执行bw.flush();(前提是执行了至少一次bw.write())执行bw.close();时就会报错。这个说法成立吗? 要是这样的话,应该是代码中出现错误在bw.flush();前,bw.write()之后。 在finally中加上bw.flush();,finally中就不会出现错误,原本代码中异常将被抛出。 这理解对吗?
booboy2563 2014-04-16
  • 打赏
  • 举报
回复
现在就是差不出问题来,你贴的这是源代码吧,我也看了,可是我明明使用了zos.putNextEntry(file);为什么有时候还是会变为NULL呢,什么情况下出现这种变化啊?
tony4geek 2014-04-16
  • 打赏
  • 举报
回复
是不是数据没有,或者流关闭的问题。
/**
     * 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
     */
    public synchronized void write(byte[] b, int off, int len)
	throws IOException
    {
	ensureOpen();
        if (off < 0 || len < 0 || off > b.length - len) {
	    throw new IndexOutOfBoundsException();
	} else if (len == 0) {
	    return;
	}

	if (current == null) {
	    throw new ZipException("no current ZIP entry");
	}
	ZipEntry entry = current.entry;
	switch (entry.method) {
	case DEFLATED:
	    super.write(b, off, len);
	    break;
	case STORED:
	    written += len;
	    if (written - locoff > entry.size) {
		throw new ZipException(
		    "attempt to write past end of STORED entry");
	    }
	    out.write(b, off, len);
	    break;
	default:
	    throw new ZipException("invalid compression method");
	}
	crc.update(b, off, len);
    }
booboy2563 2014-04-16
  • 打赏
  • 举报
回复
对于这个问题我已经进行了各种尝试,在bw.flush();前面加上zos.entryclose(); fulsh()语句出错。之后进入finally,两个文件依次跑,一个在finally中再次出现错误, 此错误与客户的问题较为类似,只是中间多了一行: at test.batch.DataGet.csvWriter.write(csvWriter.java:125) 而另外一个文件则在finally中没有出现错误。 所以外层catch错误就不一样了。 所以怀疑是由于什么原因导致错误,bw没有更新成NULL,之后在finally中错误。 可是是什么原因现在不知道,也不知道到这条路对不对。
tony4geek 2014-04-15
  • 打赏
  • 举报
回复
你有7个没出来,其他出来了?
booboy2563 2014-04-15
  • 打赏
  • 举报
回复
这是什么原因啊,我代码总共出力70多个文件,其中只有7个文件出现这个错误。 求大神给力啊!
booboy2563 2014-04-15
  • 打赏
  • 举报
回复
对,而且出现错误是客户跑出来得、,我这里跑怎么也不报错
-江沐风- 2014-04-14
  • 打赏
  • 举报
回复

50,542

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧