My name is Philipp C. Heckel and I write about nerdy things.

Snippet 0x09: Reading a ZIP/JAR file with PHP (here: a JAR manifest)

Code Snippets, Programming

Snippet 0x09: Reading a ZIP/JAR file with PHP (here: a JAR manifest)

For my open source file sync software Syncany, I have integrated the automatic plugin build process (we provide plugin repository and an easy plugin API to download plugins) with an upload to the Syncany API server. Plugins (JAR files) are uploaded by Travis (example: Samba plugin) to the Syncany server. To serve meta data on through the plugin API, I need to parse the plugins’ MANIFEST.MF files and store them in a database.

This tiny blog post shows you how to read a ZIP/JAR file entry with PHP, and parse JAR manifest (MANIFEST.MF) file. That’s it. Nothing fancy.

1. Read JAR/ZIP file entry

First, we need to read the MANIFEST.MF file from the JAR file. Since a JAR file is nothing more than a ZIP file, PHP’s zip_* functions can be used.

2. Parse JAR file manifest (MANIFEST.MF)

Once this is done, the MANIFEST.MF needs to be parsed. Manifest files have a very easy key-value format, with a colon (:) as a delimiter. This piece of code does the parsing for us:

3. Using the functions

Combining these two functions, we can read the contents of a ZIP file entry with readZipFileEntry() and parse the JAR file manifest with parseJarManifest():

A. About this post

I’m trying a new section for my blog. I call it Code Snippets. It’ll be very short, code-focused posts of things I recently discovered or find fascinating or helpful. I hope this helps

Leave a comment

I'd very much like to hear what you think of this post. Feel free to leave a comment. I usually respond within a day or two, sometimes even faster. I will not share or publish your e-mail address anywhere.