下面是解压缩代码 每次运行的时候都再i=1处跳出 也就是FileOutputStream fos=new FileOutputStream (outputpath+"\\"+entryName);   请教了很多人都不能解决  高手请出手相救package test;
import java.io.*;
import java.util.zip.*;
public class unzip{ public  int  depress(String   zipFileName ,String outputpath)   {  
   int i=0; 
  try   {   
  File file=new File(zipFileName);
  FileInputStream fis=new FileInputStream (file); 
  ZipInputStream   in   =   new   ZipInputStream(fis);   
    
  ZipEntry   entry   =   null; 
   
  i=11; 
   
  while   ((entry   =   in.getNextEntry())   !=   null)   {   
    
  String   entryName   =   entry.getName();   
  i=9; 
  if   (entry.isDirectory())   {   
  File   file   =   new   File(outputpath+"\\"+ entryName);   
  file.mkdirs();   
  System.out.println("创建文件夹:"   +   entryName);   
  }  
   else   
   { 
  i=1;  
  
  FileOutputStream fos=new FileOutputStream (outputpath+"\\"+entryName); 
   i=2; 
  //   Transfer   bytes   from   the   ZIP   file   to   the   output   file   
  byte[]   buf   =   new   byte[1024];   
    
  int   len;   
  while   ((len   =   in.read(buf))   >   0)   {   
  fos.write(buf,   0,   len);   
  }   
  fos.close(); 
    
  in.close();   
  fis.close(); 
  //}   
  }   
    
  }   catch   (IOException   e)   {   
  }   
  System.out.println("解压文件成功");
 
  return i;  
  }   
  }

解决方案 »

  1.   

    你这几行: fos.close();   
            
        in.close();       
        fis.close();   
    .中的 in.close(); 是在while循环中的 else中, 即如果不是目录(是文件),则输出到磁盘.然后你就一股脑的把流都关闭了, 如果再有一个不是目录(既 文件)的,又会用这个名为 in的流,而它已被你关闭了, 所以出错.  放到while 循环外面就可以了
      

  2.   


    }catch (IOException e){
    }finally{ //最后再关闭流
      try{
        if (in != null){
          in.close();
        }
      }catch(IOException ex){}
    }
      

  3.   

    2 楼你抢我生意呀.  
    ---------------------改完代码如下
    ----------------
    package cn.z.exercise.csdn.unzip;import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;public class Unzip { public static void main(String[] args) throws Exception {
    new Unzip().depress("d:/learn.zip", "d:/");
    } /* zipFileName 要解压缩的文件 @param outputpath 解压后的文件存放的路径 */
    public void depress(String zipFileName, String outputpath) {
    // int i = 0;
    ZipInputStream in = null;
    try {
    // 从zipFileName 创建一个 ZipInputStream // 原来是下面这样定义一个file , 没有和下面的冲突吗
    // File file = new File(zipFileName);
    File zipFile = new File(zipFileName);
    FileInputStream fis = new FileInputStream(zipFile);
    in = new ZipInputStream(fis); // ZipEntry
    ZipEntry entry = null; // i = 11; // entry = in.getNextEntry();
    while ((entry = in.getNextEntry()) != null) {
    // (entry != null) {
    //
    FileOutputStream fos = null; String entryName = entry.getName();
    // i = 9;
    if (entry.isDirectory()) { // &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& ①②③④⑤⑥⑧⑦⑧⑨⑩
    // File file = new File(outputpath + "/" + entryName);
    File file = new File(outputpath + entryName);
    file.mkdirs();
    System.out.println("创建文件夹: " + entryName);
    } else {
    // i = 1;
    // 把fos 放到外面, 效率会好点
    fos = new FileOutputStream(outputpath + entryName); // FileOutputStream fos = new FileOutputStream(outputpath +
    // "\\"
    // + entryName);
    // i = 2;
    // Transfer bytes from the ZIP file to the output file
    byte[] buf = new byte[1024]; int len;
    while ((len = in.read(buf)) > 0) {
    fos.write(buf, 0, len);
    }
    fos.close();
    // 关闭也放到while 外面, 不然有有时候要出错的
    // in.close();
    // fis 就不用关了, in.close() 后 . fis也关了
    // fis.close();
    }
    }
    System.out.println("解压文件成功 ");
    } catch (Exception e) {
    System.out.println(e.getMessage());
    } finally {
    try {
    if (in != null) {
    in.close();
    }
    } catch (Exception ex) {
    System.out.println(ex.getMessage());
    }
    }
    }
    }
      

  4.   

    我再贴
    修完如下
    -----------------------------------package cn.z.exercise.csdn.unzip;import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;public class Unzip { /* 测试一下 */
    public static void main(String[] args) throws Exception { new Unzip().depress("d:/learn.zip", "d:/");
    } /* zipFileName 要解压缩的文件 @param outputpath 解压后的文件存放的路径 */
    public void depress(String zipFileName, String outputpath) { /* 保存共解压了多少个文件 */
    int fileCount = 0; /* 保存共解压了多少个文件夹 */
    int folderCount = 0; // ZipInputStream
    ZipInputStream in = null; // ZipEntry  解压文件用到的
    ZipEntry entry = null; // File 解压文件用到的
    File file = null; try {
    // 通过zipFileName 创建一个 ZipInputStream
    in = new ZipInputStream(new FileInputStream(zipFileName)); // 保存解压后文件的输出流
    FileOutputStream fos = null; while ((entry = in.getNextEntry()) != null) { // 文件名
    String entryName = entry.getName(); // 如果是目录
    if (entry.isDirectory()) { // 则创建目录
    file = new File(outputpath + entryName);
    file.mkdirs(); // 目录计数
    folderCount++;
    } else { // 如果是个文件
    // 则解压这个文件
    fos = new FileOutputStream(outputpath + entryName);
    byte[] buf = new byte[1024];
    for (int i = 0; ((i = in.read(buf)) > 0);) {
    fos.write(buf, 0, i);
    }
    // 关闭流
    fos.close();
    // 文件计数
    fileCount++;
    }
    }
    System.out.println("解压完毕. 共解压了" + fileCount + "个文件, 创建了"
    + folderCount + "个文件夹");
    } catch (Exception e) {
    System.out.println(e.getMessage());
    } finally {
    // 关闭流
    try {
    if (in != null) {
    in.close();
    }
    } catch (Exception ex) {
    System.out.println(ex.getMessage());
    }
    }
    }
    }