VGA

Windows file explorer :: invalid ZIP file

TL;DR

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)

   zipFile.putNextEntry(file.name)
   zipFile.write(file.content)
   zipFile.closeEntry()
   zipFile.finish()
   zipFile.close()

It works pretty fine in most cases except on Windows and its file explorer. According to Windows file explorer, the compressed file is invalid.

windows_popup_screenshot

I tried to open the produced Zip file with some other tools like WinRAR and 7zip and it works like a charm. Only Windows file explorer sees the ZIP file as 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.

I spent some times looking for a solution and ended up with this post. Indeed, Windows API defines a maximum length for a path which is 260 characters as you can see here.

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.