From Default Perl on OSX, How To Unzip To Folder?

纵饮孤独 提交于 2019-12-25 01:46:35

问题


I'm trying to avoid having to include other libraries and keep this simple on OSX, using the default Perl that ships with OSX since Snow Leopard. I'm also trying to avoid shelling out to Bash to run the unzip. I found this example almost works, but dies on this line:

my $fh = IO::File->new($destfile, "w") or die "Couldn't write to $destfile: $!";

with this error:

Couldn't write to /tmp/mytest/Install Norton Security.localized//: Is a directory at test7.pl line 42.

Previously, I zipped the folder "Install Norton Security.localized" to mytest.zip and stuck it in /tmp. I then created a directory /tmp/mytest. Then, I ran this script with

unzip("/tmp/mytest/mytest.zip","/tmp/mytest");

I also did a lint syntax check on the script and it came back okay -- didn't warn me about missing libraries like many other zip libraries for Perl.

What do you suggest is the fix?


回答1:


This routine works on older OSX and doesn't involve as many lines of code. It also handles subfolders.

#!/usr/bin/perl

use strict;
use warnings;

use Archive::Zip qw(:ERROR_CODES :CONSTANTS);

my $sSource = "/tmp/mytest.zip";
my $sDest = "/tmp/mytest";

x_unzip($sSource,$sDest);

sub x_unzip {
    my ($zip_file, $out_file, $filter) = @_;
    my $zip = Archive::Zip->new($zip_file);
    unless ($zip->extractTree($filter || '', $out_file) == AZ_OK) {
        warn "unzip not successful: $!\n";
    }
}

SOURCE: https://gist.github.com/mshock/4156726



来源:https://stackoverflow.com/questions/34059730/from-default-perl-on-osx-how-to-unzip-to-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!