2013-04-02 119 views
1

嗨,朋友,我可以创建一个zip文件,使用脚本目前正在运行的目录中的文件,现在我想从各种目录创建zip文件任何人都可以帮我吗?我的代码是 ;如何在perl中创建zip文件?

use IO::Compress::Zip qw(:all); 

    zip [ glob("inventory_report_for_neus.xls") ] => "my.zip" 
    or die "Cannot create zip file: $ZipError" ; 

这将产生压缩文件的具体文件,但

use IO::Compress::Zip qw(:all); 

     zip [ glob("..\\inventory_report_for_neus.xls") ] => "my.zip" 
     or die "Cannot create zip file: $ZipError" ; 

这也是产生zip文件,但里面有没有文件!

回答

2

更新

IO::Compress::Zip创建基于文件的当前目录中的原始位置层次结构中的zip文件。

但是,其结果是它不能正确处理未被当前工作目录包围的文件。

最简单的解决方案可能是在压缩之前将目录切换到父目录。如果这不是一个选项,请在压缩之前将文件复制到临时位置,或可能使用IO::Compress::Zip's more advanced features,如从文件句柄写入zip文件。

+0

,不,不,我试着用两回和斜杠......水珠返回值,但它不是ziped –

+0

@ThiyaguATR,看到更新的答案。我测试了一下,找出了发生了什么事。 – 2013-04-02 13:11:24

+0

我需要zip文件是Excel文件亚尔 –

0

朋友终于找到了这种情况的解决方案..简单地说,我已经在perl中使用了shell命令,所以我将用适当的文件创建zip文件...代码如下。

use strict; 
use warnings; 
use MIME::Lite; 
use Net::SMTP; 
my $msg = MIME::Lite->new (
    From => '[email protected]', 
    To => '[email protected]', 
    Subject => 'with inline images', 
    Type =>'multipart/mixed' 
) or die "Error creating mult;$!\n"; 
system("zip test.zip '../test_dir/report.xls'"); 
. 
. 
. 
. 
$msg ->( filename  => test.zip, 
       content_type => 'application/zip', 
       disposition => 'attachment', 
       name   => $filename, 
     ); 
MIME::Lite->send('smtp', 'smtp.gmail.com', Timeout=>60,AuthUser=>'xyz', AuthPass=>'remotecontrol'); 
$msg->send();