一个关于解析ZIP流文件格式的问题

allan1031 2003-08-05 04:18:47
我写了一个解析ZIP文件的CLASS,但是不知道为什么,总是只能解析出一个FILE,然后便抛出“STREAM CLOSED”异常,可是我希望能够解析出所有文件。

有人能帮我下吗

具体代码如下:

private void WorkDone() {

try{
ZipInputStream zip_In=new ZipInputStream(new FileInputStream(selfFile));
ZipEntry entry;

while((entry=zip_In.getNextEntry())!=null){

byte[] buff=new byte[4096];
BufferedInputStream bin=new BufferedInputStream(zip_In);
BufferedOutputStream bout=new BufferedOutputStream(new FileOutputStream(entry.getName()));
System.out.println("File: "+entry.getName()+" has been extracted!");
while(bin.read(buff,0,1)!=-1){
bout.write(buff,0,1);
}
bout.close();
bin.close();


zip_In.closeEntry();
}


zip_In.close();

}catch(IOException exceptionHere){
System.out.println(exceptionHere);
System.exit(0);

}

}

...全文
107 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
fpwang 2003-08-06
  • 打赏
  • 举报
回复
import java.io.*;
import java.util.*;
import java.util.zip.*;

public class ZipTool{
static String zipName;
private String ZIPUSER;
private String ZIPPASS;

static String TEST="asd\n asdasd\na";

static private String fileName;

static byte[] buffer=new byte[1024];
static int bytesRead;
static String entryName;
public static void main(String []args)throws IOException{
boolean HELP=false;
if(args.length==0){
help();
System.exit(0);
}
if(args.length>2 && args[0].equals("c")){
HELP=true;
zipName=args[1];

try{
ZipOutputStream zip=new ZipOutputStream(new FileOutputStream(zipName));
for(int i=2;i<args.length;i++){
fileName=args[i];

// FileInputStream file=new FileInputStream(fileName);
ZipEntry entry=new ZipEntry(fileName);
zip.putNextEntry(entry);
/*
while((bytesRead=file.read(buffer))!=-1)
zip.write(buffer,0,bytesRead);
*/
buffer=TEST.getBytes();
bytesRead=buffer.length;
zip.write(buffer,0,bytesRead);
System.out.println(entry.getName()+" added.");
// file.close();
}
zip.close();
System.out.println(zipName+" created.");
}catch(IOException ioe){ System.out.println(ioe);}
}

//解压,读压缩文件
if(args.length==3 && args[0].equals("x")){
HELP=true;
zipName=args[1];
entryName=args[2];
try{
ZipFile zip=new ZipFile(zipName);
ZipEntry entry=zip.getEntry(entryName);
if(entry!=null){
InputStream entryStream=zip.getInputStream(entry);
FileOutputStream file=new FileOutputStream("asd/"+entry.getName());
while((bytesRead=entryStream.read(buffer))!=-1)
//System.out.println(new String(buffer)+" "+bytesRead);
file.write(buffer,0,bytesRead);
System.out.println(entry.getName()+" extracted.");
file.close();
entryStream.close();
}else System.out.println(entryName+" not found.");
zip.close();
}catch(IOException ioe){
System.out.println(ioe);
}
}

if(args.length==2 && args[0].equals("l")){
HELP=true;
try{
ZipFile zip=new ZipFile(args[1]);

for(Enumeration list=zip.entries();list.hasMoreElements();){
ZipEntry entry=(ZipEntry)list.nextElement();
System.out.println(entry.getName());
}
zip.close();
}catch(IOException ioe){
System.out.println(ioe);
}
}

if(!HELP) help();
}

private static void help(){
System.out.println("Usage:ZipTool<command><zip_file>[<file>...]");
}
}
seven1996 2003-08-06
  • 打赏
  • 举报
回复
楼主,你的代码中while语句中似乎有句不需要,如下:
while((entry=zip_In.getNextEntry())!=null){

byte[] buff=new byte[4096];
BufferedInputStream bin=new BufferedInputStream(zip_In);
BufferedOutputStream bout=new BufferedOutputStream(new FileOutputStream(entry.getName()));
System.out.println("File: "+entry.getName()+" has been extracted!");
while(bin.read(buff,0,1)!=-1){
bout.write(buff,0,1);
}
bout.close();
bin.close();


zip_In.closeEntry();\\为何要closeEntry????
}

删掉它试试
ChDw 2003-08-06
  • 打赏
  • 举报
回复
你不应该调用bin.close();
如果你需要使用BufferedInputStream
那么应该是包装在ZipInputStream 里面
ZipInputStream zip_In=new ZipInputStream(new BufferedInputStream(new FileInputStream(selfFile)));

下面就不要使用BufferedInputStream 了,并且绝对不能调用in.close()
allan1031 2003-08-06
  • 打赏
  • 举报
回复
哪位大人可以给我一个答复呢

分不够的话我可以再加的说~~~

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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