Chinaunix

标题: 批量反编译chm文档的脚本 [打印本页]

作者: weiqiboy    时间: 2007-09-10 18:21
标题: 批量反编译chm文档的脚本
chm文档可使用\windows\hh.exe反编译,但它一次只能反编译一个chm文档,也不支持长文件名,我写了一个脚本使之可以批量反编译chm文档,当然也可以“支持”任何合法的文件名(这里之所以把支持括起来是因为脚本里使用了小伎俩,并非真的使hh支持长文件名),使用方法:脚本名 filename1 filename2 ...(这里的filename 可以支持通配符,如*.chm.
#!/usr/bin/perl

#Decompile .chm file to html format.

#Author Huang Yong.

#2007-9-10

#Usage:progname filename1 filename2 ...

#可使用通配符


use File::Copy;
use File::Basename;

sub decompiler
{
    my $chmfile = $_[0];
    my $tmp = "temp";
    while (-e "$tmp.chm" or -d $tmp){
        my $i=1;
    $tmp = $tmp.$i;
    $i++;
    }    
    die "Cannot open file $chmfile" unless (-e $chmfile);
    
    copy($chmfile,"$tmp.chm") or die "Copy failed: $!";
    
    mkdir ("$tmp") or die "Cannot create temp!";
    `hh.exe -decompile $tmp $tmp.chm`;
    my $tm = basename($chmfile);
    my $html_dir;
    if ($tm =~ m@(.+)\.chm@i){
    $html_dir = $1;
    }
    move("$tmp",$html_dir);
    unlink ("$tmp.chm");
}

foreach(@ARGV){
    my @chmfile = glob $_;
    foreach $file(@chmfile){
        print "正在反编译 $file,请稍候...\n";
        decompiler($file);
        print "文件$file编译完成。\n"
    }   
}   

作者: __lxmxn__    时间: 2007-09-10 18:32
Very nice! Up
作者: zhangshebao    时间: 2007-09-10 21:36
好,顶一下。
作者: shihyu    时间: 2007-09-10 23:12
weiqiboy  代码是用什么工具软件上色?

谢谢
作者: itjiang    时间: 2007-09-12 15:03
简单用不错,但是文件名称中不能包含空格 多少算个 bug windows文件中包含空格的特别多
还是感谢你
作者: weiqiboy    时间: 2007-09-12 15:03
点击插入程序代码按扭就行了。
作者: weiqiboy    时间: 2007-09-12 15:13
原帖由 itjiang 于 2007-9-12 15:03 发表
简单用不错,但是文件名称中不能包含空格 多少算个 bug windows文件中包含空格的特别多
还是感谢你

感谢兄台指出,确实存在这样的问题,看来简单解析命令行参数还搞不定此事,哪位兄台有什么好的解决办法吗?
作者: weiqiboy    时间: 2007-09-12 15:28
修改后的代码,现在应该能处理文件名包含空格的情况了,请指正。
#!/usr/bin/perl

#Decompile .chm file to html format.

#Author Huang Yong.

#2007-9-10

#Usage:progname filename1 filename2 ...

#可使用通配符


use File::Copy;
use File::Basename;

sub decompiler
{
    my $chmfile = $_[0];
    my $tmp = "temp";
    while (-e "$tmp.chm" or -d $tmp){
        my $i=1;
    $tmp = $tmp.$i;
    $i++;
    }    
    die "Cannot open file $chmfile" unless (-e $chmfile);
    
    copy($chmfile,"$tmp.chm") or die "Copy failed: $!";
    
    mkdir ("$tmp") or die "Cannot create temp!";
    `hh.exe -decompile $tmp $tmp.chm`;
    my $tm = basename($chmfile);
    my $html_dir;
    if ($tm =~ m@(.+)\.chm@i){
    $html_dir = $1;
    }
    move("$tmp",$html_dir);
    unlink ("$tmp.chm");
}

foreach(@ARGV){
    my @chmfile;
    if(m@[*?[]]@){
       @chmfile =glob($_);
    }
    else{
       push(@chmfile,$_);
    }   
    foreach $file(@chmfile){
        print "正在反编译 $file,请稍候...\n";
        decompiler($file);
        print "文件$file编译完成。\n"
    }   
}   

作者: rwx_hc    时间: 2007-09-13 09:25
支持下!
作者: weiqiboy    时间: 2007-09-13 09:45
昨天晚上又看了代码,发现了两个BUG还有一点不太简洁的地方,修改如下。
#!/usr/bin/perl

#Decompile .chm file to html format.

#Author Huang Yong.

#2007-9-10

#Usage:progname filename1 filename2 ...

#可使用通配符


use File::Copy;
use File::Basename;

sub decompiler
{
    my $chmfile = $_[0];
    my $tmp = "temp";
    my $i=1;
    while (-e "$tmp.chm" or -d $tmp){
    $tmp = $tmp.$i;
    $i++;
    }    
    die "Cannot open file $chmfile" unless (-e $chmfile);
    
    copy($chmfile,"$tmp.chm") or die "Copy failed: $!";
    
    mkdir ("$tmp") or die "Cannot create temp!";
    `hh.exe -decompile $tmp $tmp.chm`;
    my $html_dir = basename($chmfile);
    $html_dir =~ s/\.chm$//;
    move("$tmp",$html_dir);
    unlink ("$tmp.chm");
}

for(@ARGV){
    my @chmfile;
    if(m@[*?\[\]]@){
       @chmfile =glob($_);
    }
    else{
       push(@chmfile,$_);
    }   
    for $file(@chmfile){
        print "正在反编译 $file,请稍候...\n";
        decompiler($file);
        print "文件$file编译完成。\n"
    }   
}   


[ 本帖最后由 weiqiboy 于 2007-9-13 09:49 编辑 ]
作者: weiqiboy    时间: 2007-09-13 09:47
把foreach全部改成了for,据说perler的原则之一是打尽量少的字
作者: nsnake    时间: 2007-09-13 10:38
最好注明一下 只能在WINDOWS下使用
作者: weiqiboy    时间: 2007-09-13 11:02
谢谢楼上的提醒。
作者: 雪夜狂飘    时间: 2007-10-13 21:42
zhichi




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2