日期:2014-05-20  浏览次数:20797 次

java中解压缩操作异常的问题
import java.io.*;
import java.util.zip.*;

public class ZipInputStreamDemo {
  public static void main(String[] args) throws Exception {
  File file=new File("d:" +File.separator+"test.zip");
  File outFile=null;
  InputStream in=null;
  OutputStream out=null;
  ZipFile zf=new ZipFile(file);
  ZipInputStream zips=new ZipInputStream(new FileInputStream(file));
  ZipEntry entry=null;
  while((entry=zips.getNextEntry())!=null){
  System.out.println("解压缩"+entry.getName()+"名字");
  outFile=new File("d:" +File.separator +entry.getName() );
  if(!outFile.getParentFile().exists()){
  outFile.mkdir();
  }
  if(!outFile.exists()){
  outFile.createNewFile();
  }
  in=zf.getInputStream(entry);
  out=new FileOutputStream(outFile);
  int temp=0;
  while((temp=in.read())!=0){
out.write(temp);  

  }
  }
  in.close();
  out.close();
  }

}
上面的程序是一个解压缩操作,在d盘的根目录下有压缩文件zip.test,压缩实体是一个文件夹下面的4个文件
运行上面的程序 ,提示的错误:
 解压缩test\3.txt名字
  Exception in thread "main" java.io.IOException: 系统找不到指定的路径
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:947)
at IO.ZipInputStreamDemo.main(ZipInputStreamDemo.java:25)
 这儿的路径没问题,为什么总是找不到路径?

------解决方案--------------------
修正一下,刚才的程序如有先前有文件夹了可能会报错,用下面这个
Java code

import java.io.*;
import java.util.zip.*;

public class ZipInputStreamDemo {
      public static void main(String[] args) throws Exception {
          File file=new File("d:" +File.separator+"test.zip");
          File outFile=null;
          InputStream in=null;
          OutputStream out=null;
          ZipFile zf=new ZipFile(file);
          ZipInputStream zips=new ZipInputStream(new FileInputStream(file));
          ZipEntry entry=null;
          
          while((entry=zips.getNextEntry())!=null){
              System.out.println("解压缩"+entry.getName()+"名字");
              outFile=new File("d:" +File.separator +entry.getName() );
              /*if(!outFile.getParentFile().exists()){
                  outFile.mkdir();
              }*/
              if (entry.isDirectory())
              {
                  if (! outFile.exists())
                  {
                      outFile.mkdir();
                  }
                  else
                  {
                      continue;
                  }
              }
              if(!outFile.exists()){
                  outFile.createNewFile();
              }
              in=zf.getInputStream(entry);
              out=new FileOutputStream(outFile);
              int temp=0;
              while((temp=in.read())!=-1){    //文件结束标识为-1
                  out.write(temp);  
            
              }
              in.close();
              out.close();
          }
          //in.close();
          //out.close();
      }

}