package zipfile;import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;public class ZipTest {
public static void main(String args[]){
// BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
Scanner in=new Scanner(System.in);
System.out.println("please entry the file name:");
// String zipfilename=in.readLine();
String zipfilename=in.nextLine();
String content=loadZipFile(zipfilename);
System.out.println(content);

}private static String loadZipFile(String azipfilename){
try {
ZipInputStream zin=new ZipInputStream(new FileInputStream("H:\\workplace\\tests\\New.zip"));
    ZipEntry entry;
//     System.out.println(zin.available());
//     System.out.println(zin.getNextEntry());
    while((entry=zin.getNextEntry())!=null){
//      System.out.println(entry.getName());
     if(entry.getName().equalsIgnoreCase(azipfilename)){
     BufferedReader buf=new BufferedReader(new InputStreamReader(zin));
         StringBuffer str=new StringBuffer();
         String line;
         while((line=buf.readLine())!=null){
          str=str.append(line);
         }
         return str.toString();
     }
     zin.closeEntry();
    }
    
    zin.close();
     } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}运行出现如下错误:
please entry the file name:
2.txt
null
不能读取Zip文件中的文本内容
请高手指教问题出在哪里?
(新手不知道给多少分?请原谅)

解决方案 »

  1.   

    为什么将while((line=buf.readLine())!=null){ 
                    str=str.append(line); 
                    } 
    改为while((line=buf.readLine())!=null){ 
                    System.out.println(line); 
                    } 
    就能将压缩文件中的文本文件读取出来,上面的方法错在哪呢?郁闷。
      

  2.   

    public   class   ZipTest   
    {
    public   static   void   main(String   args[])
    {
    Scanner   in=new   Scanner(System.in);
    System.out.println("please   entry   the   file   name:");
    String   zipfilename=in.nextLine();
    try
    {
    String   content=loadZipFile(zipfilename);
    System.out.println(content);
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }

    } private   static   String   loadZipFile(String   azipfilename) throws Exception
    {

    StringBuffer   str=new   StringBuffer(null);
    try   
    {
    ZipInputStream   zin=new   ZipInputStream(new   FileInputStream("H:\\workplace\\tests\\New.zip")));
          ZipEntry   entry;
          while((entry=zin.getNextEntry())!=null)
          {
            if(entry.getName().equalsIgnoreCase(azipfilename))
            {
             BufferedReader   buf=new   BufferedReader(new   InputStreamReader(zin));                
              String   line;
              while((line=buf.readLine())!=null)
              {
                    str=str.append(line);
               }
            }
            zin.closeEntry();
          }       
          zin.close();
    }catch (FileNotFoundException  e)   
    {
    //   TODO   Auto-generated   catch   block
    e.printStackTrace();
    }   
    catch   (IOException   e)   
    {
    //   TODO   Auto-generated   catch   block
    e.printStackTrace();
    }
    finally
    {
    return str.toString();
    }
    }
      

  3.   

    public   class   ZipTest   
    {
    public   static   void   main(String   args[])
    {
    Scanner   in=new   Scanner(System.in);
    System.out.println("please   entry   the   file   name:");
    String   zipfilename=in.nextLine();
    String   content=loadZipFile(zipfilename);
    System.out.println(content);
    } private   static   String   loadZipFile(String   azipfilename)
    {

    StringBuffer   str=new   StringBuffer(null);
    try   
    {
    ZipInputStream   zin=new   ZipInputStream(new   FileInputStream("C:\\java\\ch01\\New.zip"));
          ZipEntry   entry;
          while((entry=zin.getNextEntry())!=null)
          {
            if(entry.getName().equalsIgnoreCase(azipfilename))
            {
             BufferedReader   buf=new   BufferedReader(new   InputStreamReader(zin));                
              String   line;
              while((line=buf.readLine())!=null)
              {
                    str=str.append(line);
               }
            }
            zin.closeEntry();
          }       
          zin.close();
    }catch (FileNotFoundException  e)   
    {
    //   TODO   Auto-generated   catch   block
    e.printStackTrace();
    }   
    catch   (IOException   e)   
    {
    //   TODO   Auto-generated   catch   block
    e.printStackTrace();
    }
    finally
    {
    return str.toString();
    }
    }
      

  4.   

    怪事 我今天又拿我的代码运行它就成功了,没出错了。而用楼上的代码运行提示:
    please       entry       the       file       name:
    4.txt
    Exception in thread "main" java.lang.NullPointerException
    at java.lang.StringBuffer.<init>(StringBuffer.java:104)
    at zipfile.ZipFile5.loadZipFile(ZipFile5.java:26)
    at zipfile.ZipFile5.main(ZipFile5.java:19)看来将StringBuffer  str=new  StringBuffer();
    改成  StringBuffer  str=new   StringBuffer(null);是不行的,不知道楼上是怎么运行的。
    不过还是谢谢这位热心的大侠!!!