2012-03-22 118 views
3

我在解壓縮使用Java的ZIP文件時遇到了問題。方法如下。使用Java ZipFile解壓ZIP文件類

一旦文件解壓縮,文件結構就是正確的,這意味着ZIP文件內的目錄沒問題,但文件輸出長度爲零。

我檢查了ZIP文件,看壓縮是否正確,在那裏都正確。

,如果有人看到的東西,請我已經錯過了......

public static void unzip (File zipfile, File directory) throws IOException { 
    ZipFile zip = new ZipFile (zipfile); 
    Enumeration<? extends ZipEntry> entries = zip.entries(); 

    while (entries.hasMoreElements()) { 
    ZipEntry entry = entries.nextElement(); 
    File file = new File (directory, entry.getName()); 

    if (entry.isDirectory()) { 
     file.mkdirs(); 
    } 
    else { 
     file.getParentFile().mkdirs(); 

     ZipInputStream in = new ZipInputStream (zip.getInputStream (entry)); 
     OutputStream out = new FileOutputStream (file); 
     byte[] buffer = new byte[4096]; 
     int readed = 0; 

     while ((readed = in.read (buffer)) > 0) { 
     out.write (buffer, 0, readed); 
     out.flush(); 
     } 

     out.close(); 
     in.close(); 
    } 
    } 

    zip.close(); 
} 

更多的東西......很顯然,方法的getInputStream(項)將返回零個字節,不知道到底爲什麼。

+0

你有沒有嘗試過一些調試? in.read()調用是否會返回一些字節? – leonbloy 2012-03-22 20:32:14

+0

請勿在環內沖洗。 – EJP 2016-06-09 03:40:26

回答

2

ZipFile已經解壓一個條目的數據,也沒有必要使用ZipInputStream

相反的:

ZipInputStream in = new ZipInputStream (zip.getInputStream (entry)); 

用途:

InputStream in = zip.getInputStream (entry); 

ZipInputStream可以用來解壓ZIP的文件。你得到零長度數據流的原因是因爲使用ZipInputStream你需要調用getNextEntry()來讀取ZIP中的第一個文件。

+0

謝謝,的確是這個問題......試圖解壓縮一個已經解壓縮的流。 – 2012-03-23 15:08:43

-1

下面的代碼工作:

package so; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.util.Enumeration; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipFile; 
import java.util.zip.ZipInputStream; 

public class TestZip { 

    public static void main(String[] args) { 
     String path = "C:" + File.separator + "tmp" + File.separator; 
     String nom = "demo.zip"; 
     File zipfile = new File(path + nom); 
     File directory = new File(path); 
     TestZip m = new TestZip(); 
     try { 
      m.unzip(zipfile, directory); 
     } catch (Exception e) { 
      e.printStackTrace(System.out); 
     } 
    } 

    public static void unzip (File zipfile, File directory) throws IOException { 
     System.out.println(zipfile.toString()); 
     System.out.println(directory.toString()); 
      ZipFile zip = new ZipFile ( zipfile); 
      System.out.println("1"); 
      Enumeration<? extends ZipEntry> entries = zip.entries(); 
      System.out.println("2"); 

      while (entries.hasMoreElements()) { 
       System.out.println("3"); 
      ZipEntry entry = entries.nextElement(); 
      File file = new File (directory, entry.getName()); 

      if (entry.isDirectory()) { 
       file.mkdirs(); 
      } 
      else { 
       file.getParentFile().mkdirs(); 

       ZipInputStream in = new ZipInputStream (zip.getInputStream (entry)); 
       OutputStream out = new FileOutputStream (file); 
       byte[] buffer = new byte[4096]; 
       int readed = 0; 

       while ((readed = in.read (buffer)) > 0) { 
       out.write (buffer, 0, readed); 
       out.flush(); 
       } 

       out.close(); 
       in.close(); 
      } 
      } 

      zip.close(); 
     } 


} 

所以我覺得這個問題是你傳遞的參數。用「new File(complete_path + filename)」創建參數「zipfile」。如果您只是使用文件名創建它不起作用。