2015-12-03 17 views
0

我試圖避免必須包括其他庫,並保持這個簡單的OSX,使用自Snow Leopard自OSX附帶的默認Perl。我也試圖避免炮轟到Bash來運行解壓縮。我發現幾乎this example作品,但在這條線模具:從OSX上的默認Perl,如何解壓到文件夾?

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

與此錯誤:

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

以前,我拉上文件夾「安裝Norton Security.localized」到mytest.zip,把它貼in/tmp。然後我創建了一個目錄/ tmp/mytest。然後,我跑這個腳本

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

我也沒有對劇本棉絨語法檢查和它回來好嗎 - 沒有提醒我關於失蹤庫類似於Perl許多其它zip庫。

你有什麼建議是修復?

回答

0

此例程適用於較舊的OSX,不涉及儘可能多的代碼行。它也處理子文件夾。

#!/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"; 
    } 
} 

來源:https://gist.github.com/mshock/4156726