2014-06-06 157 views
0

我一直在試圖用java來提取我的程序中的幾個zip文件,並將它們放在正確的目錄中。我一直在找過這個以供參考: http://javabeginnerstutorial.com/code-base/how-to-extract-zip-file-with-subdirectories/在Java中解壓縮ZIP的問題

我當前的代碼看起來像

   String ziplocation = "/home/user/folder/"; 
       String zipfile = "libraries.zip"; 
       String liblocation = "/home/user/folder"; 

       ZipEntry zipentry; 

       if (!zipentry.isDirectory()) 
       { 
        File fileFile = new File(liblocation + "/" + zipentry.getName()); 

        InputStream inputstream = zipfile.getInputStream(zipentry); 
        String str = new java.util.Scanner(inputstream).useDelimiter("/A").next(); 

        BufferedWriter bw = new BufferedWriter(new FileWriter(fileFile)); 
        bw.append(str); 
        bw.close(); 

       } 

一切似乎從正確適應上述引用,但是當我編譯它給了我:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method getInputStream(ZipEntry) is undefined for the type String 

at Main.main(Main.java:53) 

編輯:

我現在已經改成了

   String ziplocation = "/home/user/folder/zipfile.zip"; 
       String liblocation = "/home/user/folder/"; 

       File file = new File(ziplocation); 
       ZipFile zipfile = new ZipFile(file); 
       InputStream inputStream = new BufferedReader(new FileReader(zipfile)); 

       ZipEntry zipentry; 

       if (!zipentry.isDirectory()) 
       { 
        File fileFile = new File(liblocation + "/" + zipentry.getName()); 

        InputStream inputstream = zipfile.getInputStream(zipentry); 
        String str = new java.util.Scanner(inputstream).useDelimiter("/A").next(); 

        BufferedWriter bw = new BufferedWriter(new FileWriter(fileFile)); 
        bw.append(str); 
        bw.close(); 

       } 

但是現在的編譯器與迴應:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Type mismatch: cannot convert from BufferedReader to InputStream 
The constructor FileReader(ZipFile) is undefined 

at Main.main(Main.java:48) 
+3

在提到的文章'zipfile'是'的ZipFile zip文件=新的ZipFile(文件);'。在你的代碼中它是'String zipfile =「libraries.zip」;'。正如你的錯誤消息所說:「* getInputStream(ZipEntry)'方法未定義爲類型'String' *」 – Pshemo

+0

Pshemo,爲什麼不作出答案呢? – hoefling

回答

0

的問題是在句子:

InputStream inputStream = new BufferedReader(new FileReader(zipfile)); 

那句話是不是在你下面的例子。取而代之的是通過元素的循環內zip文件發現:

File file = new File(zipFileLocation); 
ZipFile zipfile = new ZipFile(file); 

ZipEntry zipentry; 

System.out.println("nList of files in zip archive"); 
int fileNumber = 0; 
for (Enumeration<? extends ZipEntry> e = zipfile.entries(); e 
     .hasMoreElements(); fileNumber++) { 
    zipentry = e.nextElement(); 

    if (!zipentry.isDirectory()) { 
...