The filename is indeed in UTF-8 in the zip .war file.
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();
}
However the zip does not has added the encoding (as bytes[] extra information).
Three solutions would be imaginable:
- One short solution might be to run TomCat under a UTF-8 locale.
- The best would be to have maven build a war with UTF-8 encoding. (
<onfiguration><encoding>UTF-8</encoding></configuration>)
- Repairing the war by converting it.
With the first two solutions I have no experience. A quick search didn't yield anything ("encoding" is a bit ubiquitous).
The repair code is simple:
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();
}