Windows file explorer :: invalid ZIP file
Windows file explorer sees a ZIP file as invalid if it contains at least one entry with a name greater than 260 characters.
I am working on a small Kotlin application and one of its features is to download a ZIP file built from a set of JUnit report files.
Here’s an extract from the piece of code to do it
val byteArrayOutputStream = ByteArrayOutputStream()
val zipFile = ZipOutputStream(byteArrayOutputStream)
It works pretty fine in most cases except on Windows and its file explorer. According to Windows file explorer, the compressed file is invalid.
My first attempt to resolve this problem was to specify ZIP file encoding.
val zipFile = ZipOutputStream(byteArrayOutputStream, StandardCharsets.UTF_8)
This charset is used to encode the entry names and comments. It did not fix the issue but it’s a good practice to specify the encoding.
Finally, I ensured that some file names in the ZIP file are longer than 260 characters, and I added a piece of code to shrink file name.