java如何读取压缩包中的文本文件 本帖最后由 zft1518912563 于 2012-06-24 16:33:47 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 那你得先知道那个压缩文件的解压算法。或者那个解压文件有支持java的库 public static void testZipToUnzip(ZipFile zipfile,File file){ //将一个压缩文件zipfile(可以包含文件)解压到file文件中 ZipEntry ze=null; File outfile; InputStream zis=null; FileOutputStream fos=null; Enumeration e=zipfile.getEntries(); while(e.hasMoreElements()){ ze=(ZipEntry) e.nextElement(); // System.out.println(ze.getName()); outfile=new File(file+File.separator+ze.getName()); //if(ze.toString().length()!=(ze.getName()).lastIndexOf('/')+1) //这句话的作用:如果ze不是一个目录则执行下面的语句,也就是创建输出文件并且将输入流输出到输出流中。 //上面的if语句作用和该语句作用一样 if(!ze.isDirectory()){ try { //System.out.println(ze.getName()); zis=zipfile.getInputStream(ze); //System.out.println(ze.getSize()); //System.out.println(outfile.getPath()); if(!outfile.exists()) //如果输出文件不存在,那么创建输出文件的父目录 outfile.getParentFile().mkdirs(); fos=new FileOutputStream(outfile); byte[] b=new byte[1024]; int len; while((len=zis.read(b))>0){ fos.write(b, 0, len); } fos.close(); zis.close(); } catch (ZipException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }}这是我写的解压代码,运行通过,如果有问题,可以发邮件至[email protected],希望能帮助你。 不解压怎么读取啊?你不要以为你使用 WinRAR 打开这个压缩文件,双击某个文件就能打开了。WinRAR 在你不知道的情况下解压到了临时目录中,这样你才有可能打开! Spring 环境搭建错误 大家来帮我看下这两个表达式有什么区别 请问在linux下如何安装JDK string.split的问题 求助: Soap中的名域标志符 是指的什么意思啊 java的IO流问题 DateFormat的parse方法为什么总是抛出异常! 解释一下System.in.read(); 编译java程序带命令参数的格式什么? BS聊天室 求助,java小程序
//将一个压缩文件zipfile(可以包含文件)解压到file文件中
ZipEntry ze=null;
File outfile;
InputStream zis=null;
FileOutputStream fos=null;
Enumeration e=zipfile.getEntries();
while(e.hasMoreElements()){
ze=(ZipEntry) e.nextElement();
// System.out.println(ze.getName());
outfile=new File(file+File.separator+ze.getName());
//if(ze.toString().length()!=(ze.getName()).lastIndexOf('/')+1)
//这句话的作用:如果ze不是一个目录则执行下面的语句,也就是创建输出文件并且将输入流输出到输出流中。
//上面的if语句作用和该语句作用一样
if(!ze.isDirectory()){
try {
//System.out.println(ze.getName());
zis=zipfile.getInputStream(ze);
//System.out.println(ze.getSize());
//System.out.println(outfile.getPath());
if(!outfile.exists())
//如果输出文件不存在,那么创建输出文件的父目录
outfile.getParentFile().mkdirs();
fos=new FileOutputStream(outfile);
byte[] b=new byte[1024];
int len;
while((len=zis.read(b))>0){
fos.write(b, 0, len);
}
fos.close();
zis.close();
} catch (ZipException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
这是我写的解压代码,运行通过,如果有问题,可以发邮件至[email protected],希望能帮助你。