dotnet / android-libzipsharp

A managed wrapper (and then some) around libzip (https://libzip.org/)
MIT License
30 stars 12 forks source link

Unable to parse `jmod` files #101

Closed jonpryor closed 1 year ago

jonpryor commented 3 years ago

Context: https://stackoverflow.com/a/64202720

JDK 9 added a new .jmod file format, which appears to be "normal ZIP" with a { 4a 4d 01 00 } file header. These files are able to be ready by unzip -l.

For example, if you have a xamarin-android build environment, then you have a JDK 11 provisioned, which contains .jmod files:

% unzip -l $HOME/android-toolchain/jdk-11/jmods/java.base.jmod  | grep /Object.class
warning [/Users/jon/android-toolchain/jdk-11/jmods/java.base.jmod]:  4 extra bytes at beginning or within zipfile
  (attempting to process anyway)
     1944  01-22-2021 19:28   classes/java/lang/Object.class

unzip is also able to extract the file contents:

% unzip $HOME/android-toolchain/jdk-11/jmods/java.base.jmod classes/java/lang/Object.class
Archive:  /Users/jon/android-toolchain/jdk-11/jmods/java.base.jmod
warning [/Users/jon/android-toolchain/jdk-11/jmods/java.base.jmod]:  4 extra bytes at beginning or within zipfile
  (attempting to process anyway)
  inflating: classes/java/lang/Object.class  

% echo $?
1

Odd that unzip sets an error exit code, though…. The file does exist:

% ls -lh classes/java/lang/Object.class
-rw-r--r--  1 jon  staff   1.9K Jan 22  2021 classes/java/lang/Object.class

Regardless, attempting to process this file with LibZipSharp results in an error:

% csharp -r:path/to/libZipSharp.dll
using System.IO;
using Xamarin.Tools.Zip;

var a = ZipArchive.Open(Path.Combine(Environment.GetEnvironmentVariable("HOME"), "android-toolchain/jdk-11/jmods/java.base.jmod"), FileMode.Open); 
// Xamarin.Tools.Zip.ZipIOException: The file '$HOME/android-toolchain/jdk-11/jmods/java.base.jmod' is not a ZIP archive.
//   at Xamarin.Tools.Zip.ZipArchive.Open (System.String path, System.IO.FileMode mode, System.String defaultExtractionDir, System.Boolean strictConsistencyChecks, Xamarin.Tools.Zip.IPlatformOptions options)
// …
jonpryor commented 3 years ago

Related: https://github.com/nih-at/libzip/issues/235#issuecomment-801744361

It looks like libzip -- justifiably! -- doesn't want to support "possibly invalid files".

jonpryor commented 1 year ago

.jmod support was implemented without needing libZipSharp changes: https://github.com/xamarin/Java.Interop/commit/8ccb8374d242490d8d1b032f2c8ca7a813fd40f3

Closing…