perl 文件打包操作

来源:互联网 发布:涂鸦跳跃源码 编辑:程序博客网 时间:2024/05/16 13:49

    在Linux下,运用perl对文件进行打包拆包操作可以直接调用系统的相关命令进行,但是在windows下,没有相关的直接命令,或者dos自带的makecab打包不能很好的与Linux进行交互,或者调用WinRAR工具的命令操作又相当麻烦,且打包的格式(rar/zip)在Linux下要安装相应的工具才能拆包,下面介绍一种perl通过Archive::Tar、IO::Zlib模块跨windows/Linux平台的打包拆包方法。直接上测试代码:


####1 perl打包
####所需包: Archive::Tar;IO::Zlib;
####所需打包文件 origin.tar
use Archive::Tar;
use IO::Zlib;

tar_test();

sub tar_test
{
#生成压缩包句柄
my $tar = Archive::Tar->new();

#读取origin.tar包内的文件到压缩包句柄
$tar->read('E:\My_Information\perl\origin.tar');

#将压缩包句柄内的CRM_20150210_01.txt文件解压,可加上路径
#$tar->extract_file('CRM_20150210_01.txt','E:\My_Information\perl\CRM_20150210_01.txt');

#将压缩包句柄文件解压到当前目录
$tar->setcwd('E:\My_Information\perl'); #设置压缩包句柄解压目录
$tar->extract(); #解压

#将数组@data_files所示的文件添加到压缩包句柄,文件必须存在
my @data_files = ('CRM_20150210_01.txt','CRM_20150211_01.txt');
$tar->add_files(@data_files);

#新建baz.txt文件,其内容是 'This is the contents now' 添加到压缩包句柄
$tar->add_data('baz.txt', 'This is the contents now');

#将压缩包句柄写入新的压缩文件files.tar/files.tgz/files.tbz
$tar->write('E:\My_Information\perl\files.tar');                   # plain tar
  #$tar->write('files.tgz', COMPRESS_GZIP);    # gzip compressed
  #$tar->write('files.tbz', COMPRESS_BZIP);    # bzip2 compressed
}

0 0
原创粉丝点击