1

I have a simple question for you guys.

For example, In php we have ZipArchive class to create .zip files, and using exec we can do zip -r dir to compress a whole directory.

My question is, What is the best option, best performance ... etc?

Summary: exec vs class.

5
  • 1
    Depends. Why don't you benchmark it for us? Commented Oct 29, 2015 at 16:17
  • The real question about this is: Is a very good practise, use of exec? Commented Oct 29, 2015 at 16:19
  • Not every program than can be called from shell has an equivalence php class. Commented Oct 29, 2015 at 16:21
  • Well exec is mostly used for interaction with the OS. If there is already an implementation in PHP I would rather use it. in other words I would only use exec if my application had a lot of interactions with the OS. Commented Oct 29, 2015 at 16:25
  • depends on just how much you're zipping. the phpzip library builds in-memory, and you can very easily exceed the memory_limit. exec() isn't bound by that. Commented Oct 29, 2015 at 16:37

1 Answer 1

2

This ultimately depends on what you are willing to sacrifice. By using ZipArchive you will gain portability with your codebase and the freedom to jump environments with little difficulty(as opposed to say trying to tar on a windows environment). The trade off is that you take a performance hit. I did benchmark this exact problem a few years and found exec() when doing a gz to be considerably faster but obviously time has moved on and this might be different now(I doubt it). Another issue you might run into with ZipArchive is php memory limit(large archives would cause severe issues).

Based on the fact that your asking this question then my advice is to stay with ZipArchive, exec() has serious security concerns surrounding it that ZipArchive solves for you. With correct error handling you should be fine.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you, for the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.