[英]Unzip file with special character through java.util.zip library
我的zip文件中包含幾個文件。 當我運行解壓縮代碼時:
public ArrayList<String> unzip(String zipFilePath, String destDirectory, String filename) throws IOException {
ArrayList<String> pathList = new ArrayList<String>();
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdir();
}
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry entry = zipIn.getNextEntry();
// iterates over entries in the zip file
while (entry != null) {
// Original file name
// String filePath = destDirectory + File.separator + entry.getName();
int _ordPosition = entry.getName().indexOf("_ord");
if (_ordPosition<0)
throw new DatOrderException("Files inside zip file are not in correct format (please order them with _ordXX string)");
String ord = entry.getName().substring(_ordPosition,_ordPosition+6);
String filePath = destDirectory + File.separator + filename + ord + "."+ FilenameUtils.getExtension(entry.getName());
if (!entry.isDirectory()) {
// if the entry is a file, extracts it
pathList.add(filePath);
extractFile(zipIn, filePath);
} else {
// if the entry is a directory, make the directory
File dir = new File(filePath);
dir.mkdir();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
return pathList;
}
如果歸檔文件中的文件包含特殊字符(例如º),則會收到行格式錯誤的異常消息
ZipEntry entry = zipIn.getNextEntry();
是否可以重命名此文件或解決此錯誤? 謝謝
嘗試讀取具有正確字符編碼的zip文件-使用ZipInputStream(java.io.InputStream,java.nio.charset.Charset)而不是ZipInputStream(java.io.InputStream)
正如@Andrew Kolpakov建議的那樣,
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath),Charset.forName("IBM437"));
它似乎有效
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.