1

I'm using ZipOutputStream to create a zip of PDF files. I have a Map<String,byte[]>, the key is the name of the PDF file, the value is the content of the PDF file. The PDF file names contain French characters such as é.

Here is my code :

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
ZipOutputStream zout = new ZipOutputStream(bufferedOutputStream);

for(Map.Entry<String, byte[]> entry : files.entrySet()) {
    String fileName = entry.getKey() + ".pdf";
    fileName = new String(fileName.getBytes(), utf8);
    ZipEntry zip = new ZipEntry(fileName);
    zout.putNextEntry(zip);
    zout.write(entry.getValue());
    zout.closeEntry();
}
zout.close();
zipContent = byteArrayOutputStream.toByteArray();

An example of the PDf file name : Clés

Once the zip file is created, the name of my PDf file becomes this : Cl+®s

I tried the following solutions, and they didn't work :

Setting explicitly the encoding (I also tried with windows-1252 and ISO-8859-1) :

final Charset utf8 = Charset.forName("UTF-8");
ZipOutputStream zout = new ZipOutputStream(bufferedOutputStream, utf8);
...
fileName = new String(fileName.getBytes(), utf8);

Replacing the character é with the unicode :

fileName = fileName.replace("é", "\u00E9");

When I unzip the zip file, I did it with the built-in extractor program of Windows (double-click and extract all).

When I unzip with 7-zip, the encoding is back to normal. I see Clés instead of Cl+®s. However, I need the file name to be properly displayed even without 7-zip.

My JDK version : 8u172

josefk
  • 115
  • 1
  • 1
  • 10

0 Answers0