2014-06-06 211 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()) { 
...