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;
}
什麼樣的「打開文件錯誤」? – 2012-07-31 06:10:53
描述你所看到的錯誤。也可以嘗試用Winzip而不是WinRar打開文件。首先,您的zip文件可能不是zip格式。這與UTF-8無關。 – 2012-07-31 06:18:25