文件名確實是在zip .war文件中的UTF-8中。
try (ZipFile zipFile = new ZipFile(path, StandardCharsets.UTF_8)) {
zipFile.stream()
.forEachOrdered(entry -> System.out.printf("- %s%n", entry.getName()));
} catch (IOException e) {
e.printStackTrace();
}
但是zip沒有添加編碼(如bytes[] extra
信息)。
三種解決方案可以考慮的是:
- 一個短期的解決方案可能是一個UTF-8語言環境下運行Tomcat。
- 最好的辦法是讓maven用UTF-8編碼構建一場戰爭。 (
<onfiguration><encoding>UTF-8</encoding></configuration>
)
- 通過轉換修復戰爭。
由於前兩種解決方案我沒有經驗。快速搜索沒有產生任何東西(「編碼」有點無處不在)。
修復代碼很簡單:
Path path = Paths.get(".../api.war").toAbsolutePath().normalize();
Path path2 = Paths.get(".../api2.war").toAbsolutePath().normalize();
URI uri = URI.create("jar:file://" + path.toString());
Map<String,String> env = new HashMap<String,String>();
env.put("create", "false");
env.put("encoding", "UTF-8");
URI uri2 = URI.create("jar:file://" + path2.toString());
Map<String,String> env2 = new HashMap<String,String>();
env2.put("create", "true");
env2.put("encoding", "UTF-8");
try (FileSystem zipFS = FileSystems.newFileSystem(uri, env);
FileSystem zipFS2 = FileSystems.newFileSystem(uri2, env2)) {
Files.walkFileTree(zipFS.getPath("/"), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
System.out.println("* File: " + file);
Path file2 = zipFS2.getPath(file.toString());
Files.createDirectories(file2.getParent());
Files.copy(file, file2);
return FileVisitResult.CONTINUE;
}
});
} catch(IOException e) {
e.printStackTrace();
}
如果解壓縮戰成目錄,並用特殊字符的文件名保存下來?另一個嘗試是確保JAVA具有正確的編碼。 –
是的..解壓縮殺死文件名 – alexanoid
這表明操作系統沒有處理這種編碼的字體或字符集。所以它不是Java錯誤:) –