Apache Commons Compress: ZipArchiveEntry.getUnixMode() always returns 0

245 views Asked by At

When analyzing the resulting file, the value is written correctly so setUnixMode() probably works ok, but getUnixMode() always returns 0. Does anyone have any experience with this?

File file = new File("Test.file");
ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(new FileOutputStream(file));   
ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry("Entry");  
zipArchiveEntry.setUnixMode(0744);
zipArchiveOutputStream.putArchiveEntry(zipArchiveEntry);
zipArchiveOutputStream.write("TestBytes".getBytes());
zipArchiveOutputStream.closeArchiveEntry();
zipArchiveOutputStream.close();
                        
ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(new FileInputStream(file));
ZipArchiveEntry entryOut = zipArchiveInputStream.getNextZipEntry();
System.out.println(entryOut.getUnixMode());
2

There are 2 answers

1
rmunge On

getUnixMode() returns 0 when platform != PLATFORM_UNIX (see here for the source code). So you also have to specify the platform: zipArchiveEntry.setPlatform(ZipArchiveEntry.PLATFORM_UNIX)

0
Nand On

Due to the design of the ZIP format, it is not possible to read the unix mode when the ZIP is opened as a stream.

ZipArchiveInputStream is limited and may even return false contents in some cases, use ZipFile whenever possible. See the ZIP documentation page for details. This limitation is a result of streaming data vs using random access and not a limitation of Compress' specific implementation.

source

The fix is to use ZipFile instead. I have tried this and can confirm that when doing so it works.