2012-07-31 59 views
2

我有一個文件,例如test.zip。如果我使用像winrar這樣的ZIP工具,很容易解壓縮(將test.zip解壓縮到test.csv)。但test.csv不是UTF8格式。我的問題是,當我使用java解壓縮它時,它無法讀取此文件。如何在java中解壓非UTF8格式的文件

ZipFile zf = new ZipFile("C:/test.zip"); 

拋出的異常說通過打開該文件出現錯誤。

關於java http://java.sun.com/developer/technicalArticles/Programming/compression/沒有什麼關於數據格式的文章。也許整個API僅適用於UTF8格式的數據。那麼,如果我必須解壓UTF8格式以外的數據,那麼如何解壓呢?尤其是日文和中文字符(UTF8除外)。我還在 http://truezip.java.net/6/tutorial.html處發現了一個API,提到了這個問題。但是,我沒有辦法解決這個問題。有沒有簡單的方法來解決這個問題?特別是從JAVA規範請求傳遞的API。

+0

什麼樣的「打開文件錯誤」? – 2012-07-31 06:10:53

+1

描述你所看到的錯誤。也可以嘗試用Winzip而不是WinRar打開文件。首先,您的zip文件可能不是zip格式。這與UTF-8無關。 – 2012-07-31 06:18:25

回答

2

不,zip文件是而不是僅用於UTF-8數據。 Zip文件根本不會嘗試解釋文件中的數據,Java API也不會。

可能存在有關文件的非ASCII 名稱的問題,但文件內容本身不應該成爲問題。在你的情況下,它看起來像文件的名稱只是test.zip,所以你不應該遇到任何名稱編碼問題。

如果文件不能打開,那麼它聽起來像你有一個不同的問題。你確定文件存在於你期望的地方嗎?

+0

當我解壓UTF 8文件時,代碼正常。但是,當解壓文件不是utf8格式時,代碼爲 ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile)); ZipEntry ze = zis.getNextEntry(); ze爲null。 – 2012-07-31 07:04:19

+0

@abishkar:這聽起來對我來說不太可能。請展示一個簡短但完整的程序來展示問題。 – 2012-07-31 07:42:21

0

我記得只有當文件名不用UTF8編碼時纔會出現這種情況。

如果第三個組件未被禁止,請嘗試使用Apache Zip API。

import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile;

0

你可以試試下面的代碼嗎?更多的例子點擊這裏http://java2novice.com/java-collections-and-util/zip/unzip/

FileInputStream fis = null; 
    ZipInputStream zipIs = null; 
    ZipEntry zEntry = null; 
    try { 
     fis = new FileInputStream(filePath); 
     zipIs = new ZipInputStream(new BufferedInputStream(fis)); 
     while((zEntry = zipIs.getNextEntry()) != null){ 
      try{ 
       byte[] tmp = new byte[4*1024]; 
       FileOutputStream fos = null; 
       String opFilePath = "C:/"+zEntry.getName(); 
       System.out.println("Extracting file to "+opFilePath); 
       fos = new FileOutputStream(opFilePath); 
       int size = 0; 
       while((size = zipIs.read(tmp)) != -1){ 
        fos.write(tmp, 0 , size); 
       } 
       fos.flush(); 
       fos.close(); 
      } catch(Exception ex){ 

      } 
     } 
     zipIs.close(); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
0

試試這個代碼whic我用來提取所有的zip文件

try 
    { 

     final ZipFile zf = new ZipFile("C:/Documents and Settings/satheesh/Desktop/POTL.Zip"); 

     final Enumeration<? extends ZipEntry> entries = zf.entries(); 
     ZipInputStream zipInput = null; 

     while (entries.hasMoreElements()) 
     { 
      final ZipEntry zipEntry=entries.nextElement(); 
      final String fileName = zipEntry.getName(); 
     // zipInput = new ZipInputStream(new FileInputStream(fileName)); 
      InputStream inputs=zf.getInputStream(zipEntry); 
      // final RandomAccessFile br = new RandomAccessFile(fileName, "r"); 
       BufferedReader br = new BufferedReader(new InputStreamReader(inputs, "UTF-8")); 
       FileWriter fr=new FileWriter(f2); 
      BufferedWriter wr=new BufferedWriter(new FileWriter(f2)); 

      while((line = br.readLine()) != null) 
      { 
       wr.write(line); 
       System.out.println(line); 
       wr.newLine(); 
       wr.flush(); 
      } 
      br.close(); 
      zipInput.closeEntry(); 
     } 


    } 
    catch(Exception e) 
    { 
     System.out.print(e); 
    } 
    finally 
    { 
     System.out.println("\n\n\nThe had been extracted successfully"); 

    } 

此代碼真正起作用我一個很好的方式。

3

JDK6在java.util.zip實現中有一個錯誤,它不能處理非USASCII字符。我使用Apache Commons-compress-1.0.jar庫來修復它。 JDK7已經修復了java.util.zip的實現。 http://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipInputStream.html

import java.io.*; 
import org.apache.commons.compress.archivers.ArchiveEntry; 
import org.apache.commons.compress.archivers.zip.*; 

public static int unzip(File inputZip, File outputFolder) throws IOException { 
    int count=0; 
    FileInputStream fis = null; 
    ZipArchiveInputStream zis = null; 
    FileOutputStream fos = null; 
    try { 
     byte[] buffer = new byte[8192]; 
     fis = new FileInputStream(inputZip); 
     zis = new ZipArchiveInputStream(fis, "Cp1252", true); // this supports non-USACII names 
     ArchiveEntry entry; 
     while ((entry = zis.getNextEntry()) != null) { 
      File file = new File(outputFolder, entry.getName()); 
      if (entry.isDirectory()) { 
       file.mkdirs(); 
      } else { 
       count++; 
       file.getParentFile().mkdirs(); 
       fos = new FileOutputStream(file); 
       int read; 
       while ((read = zis.read(buffer,0,buffer.length)) != -1) 
        fos.write(buffer,0,read); 
       fos.close(); 
       fos=null; 
      } 
     } 
    } finally { 
     try { zis.close(); } catch (Exception e) { } 
     try { fis.close(); } catch (Exception e) { } 
     try { if (fos!=null) fos.close(); } catch (Exception e) { } 
    } 
    return count; 
}