I am reading json files to String, I sometimes update them (replace some specific words by others) and then write those update json files in a zip.
This is what an example input file looks like:
The issue is, the output "loses" character escaping and is therefore no longer a valid json:
To read the json:
private InputStream processJsonFile(File file) throws IOException {
String content;
if (!file.exists())
return new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
try {
content = IOUtils.toString(new FileReader(file, StandardCharsets.UTF_8));
} catch (IOException | NullPointerException e) {
Logs.logError("Error while reading file " + file.getPath());
Logs.logError("It seems to be malformed!");
return new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
} finally {
IOUtils.closeQuietly();
}
// here i do things with content
return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
}
to add an inputstream to the zip file:
try (fis) {
while ((length = fis.read(bytes)) >= 0)
zos.write(bytes, 0, length);
}