大家帮我看看我的程序到底哪里有问题?我的解压程序只能解压到我.zip文件所在的磁盘,不能解压到其它的磁盘?比如我的 e:\aa.zip只能解压到e:\下,不能解压到其它盘比如c:,d:等,这是怎么回事啊?程序如下:
package com.javaftp;
import java.io.*;import java.util.zip.*;
public class UnZip { 
static final int BUFFER = 2048;public static void main(String argv[]) {
    try {
        String fileName = "e:\\aa.zip";  //待解压缩的文件名
        
        /*
         * 创建一个File实例file
          
       public class File 为文件和目录路径名的抽象表示形式。
        构造方法File(String pathname) 
        通过将给定路径名字符串转换成抽象路径名来创建一个新 File 实例。
        */
        File file = new File(fileName);
        
        //从文件名中提取出子目录(路径)名
        //lastIndexOf(String str)返回在此字符串中最右边出现的指定子字符串的索引
        int n=fileName.lastIndexOf(".");
        String folderName = fileName.substring(0,n);
        System.out.println(n);
        File folder = new File(folderName);
   
        folder.mkdir(); //创建子目录(路径)
        
        System.out.println(folderName);
        BufferedOutputStream dest = null;
        FileInputStream fis = new FileInputStream(file);
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
        //ZipEntry类用于表示 ZIP 文件条目
        ZipEntry entry;
       
       // getNextEntry() 读取下一个 ZIP 文件条目并将流定位到该条目数据的开始处
        while((entry = zis.getNextEntry()) != null) {
          if (entry.isDirectory()) continue;
            System.out.println("Extracting: " +entry);
            int count;
            byte data[] = new byte[BUFFER];
            /*write the files to the disk
            FileOutputStream(String name)
            创建一个向具有指定名称的文件中写入数据的输出文件流。
             */            File f1 = new File(entry.getName());
            if (f1.getParent()!=null){
                String subDirName=folderName + "\\" + f1.getParent();
                File subDir = new File(subDirName);
                System.out.println(subDir.getAbsolutePath());
                subDir.mkdirs();
            }
            
            FileOutputStream fos = new FileOutputStream(folderName + "\\" + entry.getName());
            /*BufferedOutputStream(OutputStream out, int size)
            创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的基础输出流。
             */
            dest = new BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }
            //刷新此缓冲的输出流。
            dest.flush();
            dest.close();
        }
        zis.close();
    } catch(Exception e) {
        e.printStackTrace();
    }
}
}
我这个程序只能解压到e:\下,怎么解压到c:\AA目录下呢?
我觉得是 FileOutputStream fos = new FileOutputStream(folderName + "\\" + entry.getName());的问题,可我改不好!大家帮帮我,怎么改啊!!我郁闷了很久了!大家测试的时候一定要拿多层目录测试!大家最好把修改后的代码帖上来!谢拉!

解决方案 »

  1.   

    String folderName = fileName.substring(0,n);新加一个参数(表示目标路径),把上面这句话改成:String folderName = 新参数 + fileName.substring(0,n);
      

  2.   

    我试了下应该写成这样:
    String path = "c:\\01\\source\\";
    String folderName = path + fileName.substring(3,n);
    substring里取3到n才对!
    trumplet(检查)大哥,因为这个程序没有经过严格测试,我总觉得有问题!我在网上发现一个瞒好的解压缩程序,但是感觉它还有些问题,好象也不支持多层目录的,但是它写的挺好!您看下吧:要能把这个程序改好,那就完美了!我测试感觉有问题!
    /**
     *功能:Zip文件释放,Zip文件目录、选择目录后显示文件内容
     *  首先用户输入zip文件路径及名称,读取zip文件将文件集中的文件清单显示出来,
     *  用户选择需要显示的文件,之后通过DataInputStream类将文件内容读取出来并
     *  显示在标准输出端
     *日期:2005-06-27
     *作者:Pcera
     */
    import java.util.*;
    import java.util.zip.*;
    import java.io.*;class ZipFileHandle{
     private String[] FileNameArray;            //真实文件名存放数组
     private String[] FileNameArrayShow;        //需要显示的文件名存放数组
     private ZipInputStream zipFile;            //zip输入流对象
     private ZipEntry entry;                    //zip文件入口对象
     private int zipFileCount = 0;              //zip中的文件总数
     
     /**
      *初始化各个参数
      *通过类的套嵌来访问文件
      *将得到文件的清单附值给数组,以便在后面用户选择时从数组中获得文件名
      */
     public ZipFileHandle(File file){
      try{ 
       while (!(file.exists())) {
        System.out.println("Plase input right path again: "); 
        BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
        String filepath = userInput.readLine();
        file = new File(filepath); 
       }  
       zipFile = new ZipInputStream(new FileInputStream(file));
       while ((zipFile.getNextEntry()) != null){
        zipFileCount++;
       }   
       FileNameArray = new String[zipFileCount];
       FileNameArrayShow = new String[zipFileCount];
      }catch(IOException e){
       System.out.println("初始化错误!!!");
       e.printStackTrace();
      } 
     }
     /**
      *生成文件目录
      *根据show的值来确定返回值
      *如果show的值为"sh"则返回在屏幕上显示的名称
      *如果show的值为"gr"则返回实际名称
      */
     public String[] getFileNameList(String show,File file){
      try{
       int i = 0;
       String FileName;
       zipFile = new ZipInputStream(new FileInputStream(file));
       while ((entry = zipFile.getNextEntry()) != null){
        FileName = entry.getName();
        //真实文件名附值
        FileNameArray[i] = FileName;
        //显示文件名附值
        if (FileName.equals("")) FileName = "...";
        FileName = Integer.toString(i) + "-|" + FileName; 
        FileNameArrayShow[i] = FileName;
        i++;
       }
       zipFile.close();
       //根据条件返回文件数组
       if(show == "sh"){
        return FileNameArrayShow;
       }else{
        return FileNameArray;
       }   
      }catch(IOException e){
       System.out.println("读取zip文件内的文件名出错!!!");
       e.printStackTrace();
       return null;
      }
     }
     
     /**
      *读取文件内容
      *根据传递进来的zip文件对象和
      *zip中所要显示的文件
      *用户根据type来选择返回的是unicode信息还字节信息
      *如果type是"Str"则返回字符串信息,"Byte"则返回通过字节获得的文件内容
      */
     public String loadFileCon(String fileName,File file,String type){
      String fileContent = "",conTemp = "";
      byte[] fileConfByte;
      try{
       //找到要显示的文件入口,然后读取通过文本格式读取文件内容             
       zipFile = new ZipInputStream(new FileInputStream(file));
       //读取文件内容
       while ((entry = zipFile.getNextEntry()) != null){
        if (entry.getName().equals(fileName)){
         //通过字节读取文件内容 
         if (type.equals("Byte")){ 
          fileConfByte = new byte[(int)entry.getSize()]; 
          DataInputStream reader = new DataInputStream(zipFile);
          reader.readFully(fileConfByte,0,(int)entry.getSize());
          fileContent = new String(fileConfByte);     
         }else if ((type.equals("Str"))){
          //通过unicode字符读取文件内容
          BufferedReader in = new BufferedReader(new InputStreamReader(zipFile));
          while ((conTemp = in.readLine()) != null){
           fileContent = fileContent + conTemp + "\n\r";
          }
         }
        } 
       }
       //关闭文件zip流
       zipFile.closeEntry();
       zipFile.close();
       //返回
       return fileContent;  }catch(IOException e){
       System.out.println("读取文件内容失败!!!");   
       e.printStackTrace();
       return null;
      }  
      
     }
     
     /**
      *演示方法
      */
     public static void main(String args[]){
      String filePath;
      String[] showFileArr;
      String[] realFileArr;
      String fileContent;
      int choose;
      //通过用户获得文件路径和文件名
      try{
       BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
       System.out.println("Plase zip file path and name !!!");
       filePath = userInput.readLine();
       File file = new File(filePath);
       while (!(file.exists())) {
        System.out.println("Plase input right path again: "); 
        filePath = userInput.readLine();
        file = new File(filePath); 
       }
       //显示文件目录,显示选择的文件的内容
       //获得真实文件名和显示文件名数组
       ZipFileHandle zipFile = new ZipFileHandle(file);
       showFileArr = zipFile.getFileNameList("sh",file);
       realFileArr = zipFile.getFileNameList("gr",file);
       //打印显示文件名数组
       int i = 0;
       while (i < showFileArr.length){
        System.out.println(showFileArr[i]);
        i++;
       }
       //通过用户输入获得需要显示的文件
       System.out.println("Plase choose file num!!!");
       choose = Integer.parseInt(userInput.readLine());
       if ((choose <0)||(choose >  showFileArr.length)){
        System.out.println("Plase choose file num retry!!!");
        choose = Integer.parseInt(userInput.readLine());
       }
       //获得用户选择的文件的内容
       fileContent = zipFile.loadFileCon(realFileArr[choose],file,"Byte");
       System.out.println(fileContent);
       
      }catch(Exception e){
       System.out.println("测试程序出错!!!");
       e.printStackTrace();
      }  
     }  
    }
      

  3.   

    to  trumplet(检查) : 我觉得这两句话在程序中是多余的:File folder = new File(folderName);
    folder.mkdir(); //创建子目录(路径)
    我把上面两句话去了,程序仍然是好的!
    我觉得下面这段已经把目录给建好了啊!
     if (f1.getParent()!=null){
                    String subDirName=folderName + "\\" + f1.getParent();
                    File subDir = new File(subDirName);
                    System.out.println(subDir.getAbsolutePath());
                    subDir.mkdirs();
                }
    为什么又用下面的:
    File folder = new File(folderName);
    folder.mkdir(); //创建子目录(路径)trumplet(检查),你觉得呢??
    还有一个问题,我解压缩程序的中文问题至今未解决!我找不到支持中文的zipinputstream包,能帮帮我吗?