I know that \Phar and \PharData exist, but I'm having some trouble with the methods they supply so far. I'm still having to detect the mime-type / file type by whatever means, before determining which Phar*::method() to use in an attempt to extract the archive and do work on the files it contains.

Is there a go-to, "easy-button" class that I could include (maybe some package available via composer) that handles this at a very high level? Or am I failing to use the Phar and friends properly or in need of re-RTM so far?

Basically, I want to do the following (it's a CLI script that I control for now, so security, while important with this type of thing, is on the backburner for now):

  1. Detect that a file might be an archive of some kind.
  2. Validate that it seems to be one of the following: .tar, .gz, .tar.gz, or .zip.
  3. If so, attempt to extract the archive and then parse the content of it's actual files.

Is there an "easy-button" for this that I'm unaware of, or do I need to build some logic that guesses as best it can as to what type of archive it might be, and then try to use the appropriate Phar* method to attempt to extract it's files and do whatever work I need to on them?

I hope that makes sense the way I wrote it. I'm trying to avoid re-inventing the wheel for a mini-project here if someone has already figured all of this out basically.

2

There are 2 answers

2
Villeneuve Michaël On

If you don't need pure php and if your code is running on a linux machine, a

exec('uncompress [-cfv] [file...]');

or a

exec('unzip filename.zip -d destination');

will extract the file and make it usable for php. Of course you need to check the extension (zip, tar, etc) in order to call the right command

0
Kevin On

So, while continuing to research this I ended up seeing my own (this) SO question in google search results, which annoys me for some reason. So just in case someone stumbles upon this looking for a good solution, I've since found a couple by searching https://packagist.org/search/?q=archive (go figure):

Here's a few of them that seem promising.

wapmorgan/UnifiedArchive:

alchemy/zippy:

zetacomponents/Archive

  • zetacomponents/Archive (packagist)
  • zetacomponents/Archive (github source)

    Features (at first glance):

    • It seems to be a pure php implementation? If so that's just awesome.
    • Last updated 15 days ago, so it's the most active of the three I mentioned.
    • Seems to be maintained by an organization as opposed to a single person.
    • It has the most downloads by far on packagist (when searching for "archive"), and though I haven't played with it yet, that's usually a good sign.

Disclaimer: I have only actually tried wapmorgan/UnifiedArchive as of this writing, and so far it's exactly what I was looking for.

Anyway, I hope this helps anyone who might stumble upon this question.