开发者

How can I extract a compressed archive in Perl?

开发者 https://www.devze.com 2022-12-10 22:52 出处:网络
I need to allow users to upload a zip file via a web form.The server is running Linux with an Apache web server.Are there advantages to using a module like Archive::Zip to extract this archive or shou

I need to allow users to upload a zip file via a web form. The server is running Linux with an Apache web server. Are there advantages to using a module like Archive::Zip to extract this archive or should I just execute a system 开发者_高级运维call to unzip with backticks?


According to the Archive::Zip documentation you'd be better off using Archive::Extract:

If you are just going to be extracting zips (and/or other archives) you are recommended to look at using Archive::Extract instead, as it is much easier to use and factors out archive-specific functionality.

That's interesting because Archive::Extract will try Archive::Zip first and then fall back to the unzip binary if it fails. So it does seem that Archive::Zip is the preferred option.

Archive::Zip uses Compress::Raw::Zlib which is a low level interface to the zlib system library; so it's not a pure Perl implementation meaning it's going to be similar in performance to unzip. So, in other words, from a performance perspective there's no reason to pick unzip ahead of Archive::Zip.


If you execute the binary unzip, your process will fork/exec and

  1. instantiate a new process
  2. consume more memory (for the duration of the spawned process)

You'll also have to configure with the correct path to unzip. Given all this, I would strongly prefer the library approach.


One concern is with memory. We have found the hard way (production web server crashed) that Archive::Tar had a memory leak. So while overall using a module instead of a system call to an external command is a good idea (see other responses for reasoning), you need to make sure the module has no gotchas.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号