免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4817 | 回复: 13
打印 上一主题 下一主题

[已解决]请问读取压缩包文件应该怎么做? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-07-06 15:26 |只看该作者 |倒序浏览
请问各位大大..读取的文本文件是.Z的压缩包, 我尝试用read或者sysread来读,在匹配的时候均出现问题(如无法标准输出)!用while<>却没有问题,之前MMMIX说过 读取的文本文件是.Z的压缩包最好使用binmode, 请各位大大帮忙看一下是什么问题?


  1. sub creatTmp(){ #read each line and put the attribute to record hash
  2.         my ($infile,$mmsgz,$smtingz,$iomgz)= @_;
  3.         my $tmpStatus;
  4.         my $tmpStatuscode;
  5.         my $tmpStart;
  6.         my $tmpEnd;
  7.         my $buffer= "";
  8.         if(!open(INFILE,"zcat $infile|")){
  9.                 die("Could not open file $infile:$!");
  10.         }
  11.          binmode(INFILE);
  12.         while(<INFILE>){
  13.         #while(sysread(INFILE,$buffer,1024)){
  14.         #while(read(INFILE,$buffer,1024)){
  15.                #process and count
  16.                 #next if $buffer=~/^Record.*\(.*\).*\"MiepPush.+\"/../End/;
  17.                 next if /^Record.*\(.*\).*\"MiepPush.+\"/../End/;
  18.                 #if ($buffer=~/(^Record.*\(\d+\).*\"MiepPull.+")/){
  19.                 if (/(^Record.*\(\d+\).*\"MiepPull.+")/){
  20.                         $tmpStart=$1;
  21.                 #} elsif ($buffer=~/\s+\"(.*)\"\s\=\s\"(.*)\"/){
  22.                 } elsif (/\s+\"(.*)\"\s\=\s\"(.*)\"/){
  23.                         $tmpStatus = $1;
  24.                         $tmpStatuscode = $2;
  25.                         $recordhash{$tmpStatus} = $tmpStatuscode;
  26.                 #} elsif ($buffer=~/(^End.*Record.*\(\d+\))/){
  27.                 } elsif (/(^End.*Record.*\(\d+\))/){
  28.                         $tmpEnd = $1;    #在这里开始便无法输出数据
  29.                         if ($recordhash{"url"}=~/http\:\/\/mms\.smartone.+|202\.140\.96\.(?=92|208|46|97|227|47).+|10\.16\.96\.(?=76
  30. |79|93|94|187|188|180|137|167).+/i){
  31.                                print $tmpStart."\n" ;
  32.                                 foreach (keys %recordhash){
  33.                                                 print "\"".$_."\" = \"".$recordhash{$_}."\"\n";
  34.                                 }
  35.                                 
  36.                                 print $tmpEnd."\n" ;

  37.                         }
  38.                         undef %recordhash;
  39.                 }
  40.         }
  41.         close(INFILE);
  42. }

复制代码



        binmode(INFILE);
        while(<INFILE>{
        #while(sysread(INFILE,$buffer,1024)){
        #while(read(INFILE,$buffer,1024)){


$buffer是能正常输出的..能确定匹配是没有错的..想问一下为什么无法输出

[ 本帖最后由 hyoryeo 于 2009-7-9 14:44 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-07-06 17:13 |只看该作者

回复 #1 hyoryeo 的帖子

Using CPAN Module: IO::Zlib to deal with *.Z file

You can not use binmode to process contents from a *.Z file.

i.e.:
use IO::Zlib;
my $zfile = 'your.Z';
my $fh = IO::Zlib->new($zfile, "rb");
if (defined $fh) {
    while(<$fh>) {
       chomp;
       # now process each line:
      # ....
    }
}

论坛徽章:
0
3 [报告]
发表于 2009-07-06 17:22 |只看该作者
原帖由 ulmer 于 2009-7-6 17:13 发表
Using CPAN Module: IO::Zlib to deal with *.Z file

You can not use binmode to process contents from a *.Z file.

i.e.:
use IO::Zlib;
my $zfile = 'your.Z';
my $fh = IO::Zlib->new($zfile, " ...


Thank ulmer, I will try it

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
4 [报告]
发表于 2009-07-06 19:36 |只看该作者
原帖由 ulmer 于 2009-7-6 17:13 发表
Using CPAN Module: IO::Zlib to deal with *.Z file

You can not use binmode to process contents from a *.Z file.

为什么不行?

当然,针对使用 Compress::Zlib 的情况,使用 IO::Zlib 是正确的选择。

论坛徽章:
0
5 [报告]
发表于 2009-07-07 09:08 |只看该作者
請問能否加快读取压缩包的速度的方法?

论坛徽章:
0
6 [报告]
发表于 2009-07-07 09:46 |只看该作者
使用IO::Zlib;出來的数据是乱码!请问该怎么做?

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
7 [报告]
发表于 2009-07-07 15:58 |只看该作者
原帖由 hyoryeo 于 2009-7-7 09:46 发表
使用IO::Zlib;出來的数据是乱码!请问该怎么做?

完整的可重现问题的代码贴上来看看。

论坛徽章:
0
8 [报告]
发表于 2009-07-07 16:16 |只看该作者

回复 #6 hyoryeo 的帖子

put your *.Z file here if possible.

1. Using open FH, '-|', 'zcat', 'yourZfile' or die $!;
    then processing while(<FH>) each line is most simplest way.

2.  IO::Zlib will output unziped or decoded original text. if your original text
    with other encoding (GB) encoded, the extra decoding for original text
   should be used.

Test:
# compress /etc/passwd as pwd.Z file
cat /etc/passwd | gzip -f > pwd.Z
# check it
zcat pwd.Z
# using perl IO::Zlib to process its data:
perl -MIO::Zlib -e '$f=IO::Zlib->new("pwd.Z","rb");print while(<$f>)'

论坛徽章:
0
9 [报告]
发表于 2009-07-07 16:36 |只看该作者
原帖由 MMMIX 于 2009-7-7 15:58 发表

完整的可重现问题的代码贴上来看看。

  1. sub creatTmp(){ #read each line and put the attribute to record hash
  2.         my ($infile,$mmsgz,$smtingz,$iomgz)= @_;
  3.         my $tmpStatus;
  4.         my $tmpStatuscode;
  5.         my $tmpStart;
  6.         my $tmpEnd;
  7.         #if(!open(INFILE,"zcat $infile|")){
  8.          #       die("Could not open file $infile:$!");
  9.         #}
  10.          my $readOriginal = IO::Zlib -> new("$infile","rb");
  11.         if(defined $readOriginal){
  12.                 while(<$readOriginal>){
  13.                 chomp;
  14.                 next if /^Record.*\(.*\).*\"MiepPush.+\"/../End/;
  15.                 if (/(^Record.*\(\d+\).*\"MiepPull.+")/){
  16.                         $tmpStart=$1;
  17.                 } elsif (/\s+\"(.*)\"\s\=\s\"(.*)\"/){
  18.                         $tmpStatus = $1;
  19.                         $tmpStatuscode = $2;
  20.                         $recordhash{$tmpStatus} = $tmpStatuscode;
  21.                 } elsif (/(^End.*Record.*\(\d+\))/){
  22.                         $tmpEnd = $1;
  23.                         if ($recordhash{"url"}=~/http\:\/\/mms\.smartone.+|202\.140\.96\.(?=92|208|46|97|227|47).+|10\.16\.96\.(?=76
  24. |79|93|94|187|188|180|137|167).+/i){
  25.                         $mmsgz -> gzwrite($tmpStart."\n");
  26.                                 foreach (keys %recordhash){
  27.                                         $mmsgz -> gzwrite("\"".$_."\" = \"".$recordhash{$_}."\"\n");
  28.                                 }
  29.                                 $mmsgz->gzwrite($tmpEnd."\n");
  30.                           }
  31.                           undef %recordhash;
  32.                     }
  33.            }
  34.            #undef $readOriginal;
  35. }
复制代码

[ 本帖最后由 hyoryeo 于 2009-7-7 18:07 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2009-07-07 16:40 |只看该作者
原帖由 ulmer 于 2009-7-7 16:16 发表
put your *.Z file here if possible.

1. Using open FH, '-|', 'zcat', 'yourZfile' or die $!;
    then processing while() each line is most simplest way.

2.  IO::Zlib will output unziped or d ...


if i use test by
Test:
# compress /etc/passwd as pwd.Z file
cat /etc/passwd | gzip -f > pwd.Z
# check it
zcat pwd.Z
# using perl IO::Zlib to process its data:
perl -MIO::Zlib -e '$f=IO::Zlib->new("pwd.Z","rb");print while(<$f>)'

的确是正常的..但是我的.Z还是乱码?我再试试.

[ 本帖最后由 hyoryeo 于 2009-7-7 16:50 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP