I am generating a zip file using ByteArrayOutputStream and ZipOutputStream. Filenames at ZipEntry are OK, with the correct encoding. The problem comes when it is called "ByteArrayOutputStream.toByteArray()" and the zip file is created correctly but zipEntries are generated at "cp866". At controller the return type is ResponseEntity<byte[]>.
Below it is part of the code.
byteArrayOutputStream = new ByteArrayOutputStream();
zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
zipOutputStream.putNextEntry(new ZipEntry("ação.pdf")); //Here it is all OK
zipOutputStream.write(inputStream.readAllBytes());
zipOutputStream.closeEntry();
And the code that return to RestController is something like:
HttpHeaders headers = new HttpHeaders();
contentDisposition =
ContentDisposition.builder("attachment").filename("download.zip").build();
headers.setContentDisposition(contentDisposition);
headers.setContentLength(byteArrayOutputStream.size());
headers.setContentType(MediaType.parseMediaType("application/zip; charset-utf-8"));
return ResponseEntity.ok().headers(headers).body(byteArrayOutputStream.toByteArray());
At this scenario, a file called "ação.pdf" is generated as "a├з├гo.pdf", and if I specify charset ISO8859-1 at ZipEntry, it is generated as "aчуo.pdf".
What I have tried:
- returning a StreamingResponseBody (instead of byte[])
- specify charset at zipEntry filename string
- specify charset at zipEntry object
- convert do base64 (file is generated corrupted)
Without success in all possibilities