Reputation: 11665
My code reads files from a zipfile , which works fine except for files with special characters. Problematic character is 'è' ( See my code fère_champenoise )
String works="(3/(3)_juno.jpa";
String sdoesntwork="ba/battle of fère_champenoise.jpa";
ZipFile file1 = null;
try {
file1 = new ZipFile(sZipFileOld);
} catch (IOException e) {
System.out.println("Could not open zip file " + sZipFileOld + ": " + e);
}
try {
file1.getInputStream(file1.getEntry(sdoesntwork));
} catch (IOException e) {
System.out.println(sdoesntwork + ": IO Error " + e);
e.printStackTrace();
}
it throws an error but doesn't go throught the exception handler:
Exception in thread "main" java.lang.NullPointerException
at java.util.zip.ZipFile.getInputStream(Unknown Source)
at ZipCompare.main(ZipCompare.java:56)
Any Solutions ?
Upvotes: 1
Views: 1613
Reputation: 75
file1 = new ZipFile(sZipFileOld,StandardCharsets.UTF_8);
charset - The charset to be used to decode the ZIP entry name and comment (ignored if the language encoding bit of the ZIP entry's general purpose bit flag is set).
if the zip entry and its comment is ASCII, it is not necessary to use this way to construct the ZipFile.
Upvotes: 0
Reputation: 3650
When constructing the zipfile, explicitly specifying the encoding: file1 = new ZipFile(sZipFileOld, Charset.forName("IBM437"));
Zip files doesn't use the default UTF-8 encoding for special characters
Upvotes: 2
Reputation: 1910
It doesn't go through your exception handler because is another type of exception, Null pointer exception is thrown because the entry is not found. You should check how or with which Charset the file has been define.
Upvotes: 0
Reputation: 10342
The problem is file1.getEntry(sdoesntwork)
returns null because it does not find that entry.
If you are sure this name is correct, then try to use:
file1 = new ZipFile(sZipFileOld,StandardCharsets.UTF_8);
Upvotes: 0
Reputation: 133
I believe you need to specify an encoding, UTF-8 probably. Something like this:
final InputStream in = new InputStreamReader(file1.getInputStream(file1.getEntry(sdoesntwork)), "utf-8");
Make sure you remember to close this in a finally.
Upvotes: 0