如何在java中解压缩不是UTF8格式的文件

我有一个文件,例如test.zip。 如果我使用像winrar这样的ZIP工具,则很容易提取(将test.zip解压缩到test.csv)。 但test.csv不是UTF8格式。 我的问题是,当我使用java解压缩它时,它无法读取此文件。

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

抛出的exception表示通过打开该文件会发生错误。

在java http://java.sun.com/developer/technicalArticles/Programming/compression/上没有关于数据格式化的文章。 也许整个API仅针对UTF8格式数据而设计。 那么,如果我必须解压缩除UTF8格式之外的数据,如何解压缩呢? 特别是拥有更多空间大小的日文和中文字符(UTF8除外)。 我还在http://truezip.java.net/6/tutorial.html找到了一个API,其中提到了这个问题。 但是,我没有找到解决方法。 有没有简单的方法来解决这个问题? 特别是从JAVA规范请求传递的API。

JDK6在java.util.zip实现中有一个错误,它无法处理非USASCII字符。 我使用Apache Commons 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; } 

不,zip文件不仅适用于UTF-8数据。 Zip文件根本不会尝试解释文件中的数据,Java API也不会。

围绕文件的非ASCII 名称可能存在问题,但文件内容本身应该不是问题。 在您的情况下,看起来该文件的名称只是test.zip ,因此您不应该遇到任何名称编码问题。

如果文件无法打开 ,那么听起来你有一个不同的问题。 你确定文件存在于你期望的位置吗?

我记得这只发生在文件名未以UTF8编码时。

如果未禁止第3个组件,请尝试使用Apache Zip API。

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

你可以试试下面的代码吗? 有关更多示例,请访问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(); } 

尝试使用此代码,我用它来提取所有的zip文件

 try { final ZipFile zf = new ZipFile("C:/Documents and Settings/satheesh/Desktop/POTL.Zip"); final Enumeration 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"); } 

这段代码对我很有帮助。