Perl 模块之Compress::Zstd
2018-03-26 18:16:31 阿炯

Perl interface to the Zstd (Zstandard) (de)compressor.

Compress::Zstd模块是Perl下对Zstandard压缩格式的支持模块,提供了简单明了的API供调用。

use Compress::Zstd;
 
my $compressed = compress($bytes);
my $decompressed = decompress($compressed);

函数

compress($source [, $level])

Compresses the given buffer and returns the resulting bytes. The input buffer can be either a scalar or a scalar reference.

compress函数对给定的缓存区进行压缩处理,完成后返回字节数,输入函数可为句柄引用。

On error undef is returned.

出错即返回undef。

compress_mt($source, $num_threads [, $level])

支持多线程compress函数。

Note that this function uses experimental API of Zstandard.
注意:该函数调用了尚处理试验阶段Zstandard中的API。

decompress($source)
uncompress($source)


Decompresses the given buffer and returns the resulting bytes. The input buffer can be either a scalar or a scalar reference.

解压缩函数,调用方式同上。

On error (in case of corrupted data) undef is returned.


CONSTANTS
常用常量

ZSTD_VERSION_NUMBER
ZSTD_VERSION_STRING
ZSTD_MAX_CLEVEL


新的类似关联版本(2021年6月)


Compress::Stream::Zstd为Compress::Zstd这个模块的一个分支,它包含一些更改,以使流式压缩/解压缩更加健壮,当然API的调用方式是保持一致的。新建该模块的唯一原因是允许其被IO::Compress::Zstd模块使用;也希望这里所做的更改可以合并到上游,这样该模块就可以退役了。


示例:
-------------------------------
comp.zstd.pl
#将cp.txt压缩为'/opt/cp.zst',压缩率为9。

示例1

use v5.20;
use Compress::Zstd;

my $f='cp.txt';

open(SF, "<","$f");
binmode SF;

open(CPF,'>>',"/opt/cp.zst");
binmode CPF;

my ($buf,$data,$n,$lopcnt);

while (($n = read SF,$data,128*1024)!=0){
    $lopcnt++;
    #say "$n:\n$data";
    my $cpd = compress($data,9);
    print CPF $cpd;
}

close(SF);
close(CPF);

say "While Loop Count:$lopcnt";

示例2

use v5.20;
use IO::File;
use Compress::Zstd;

my ($sf,$df)=('cp.txt','/opt/cp.zst');

my $fr=IO::File->new("$sf", "r");
my $fw=IO::File->new($df,"w");
binmode($fw);
my ($data,$n,$lopcnt);

while(($n=$fr->sysread($data,32*1024))!=0){
 $lopcnt++;
 $fw->syswrite(compress($data,9));
}

undef $fr;
undef $fw;

say "While Loop Count:$lopcnt";

-------------------------------
uncomp.zstd.pl
#将/opt/cp.zst解压缩到/tmp/cp.txt文件中。
use v5.20;
use Compress::Zstd::Decompressor qw(ZSTD_DSTREAM_IN_SIZE);

my $f='/tmp/cp.txt';

open(SF, ">>","$f");
binmode SF;

open(CPF,'<',"/opt/cp.zst");
binmode CPF;

my $decompressor = Compress::Zstd::Decompressor->new;
while (read(CPF, my $buffer, ZSTD_DSTREAM_IN_SIZE)) {
    print SF $decompressor->decompress($buffer);
}

close(CPF);
close(SF);

示例3
-------------------------------
steam.zstdex0.pl

use v5.22;
use Compress::Stream::Zstd qw(ZSTD_MAX_CLEVEL);
use Compress::Stream::Zstd::Compressor qw(ZSTD_CSTREAM_IN_SIZE);
use Compress::Stream::Zstd::Decompressor qw(ZSTD_DSTREAM_IN_SIZE);
 
my ($decompress) = grep { $_ eq '-d' } @ARGV;
my ($level) = map { s/^-//; $_ } grep { /^-[0-9]+$/ } @ARGV;
$level = 3 if !$level || $level < 1 || $level > ZSTD_MAX_CLEVEL;
 
binmode $_ for (*STDIN, *STDOUT);
 
if ($decompress) {
    my $decompressor = Compress::Stream::Zstd::Decompressor->new;
    while (read(*STDIN, my $buffer, ZSTD_DSTREAM_IN_SIZE)) {
        print STDOUT $decompressor->decompress($buffer);
    }
} else {
    my $compressor = Compress::Stream::Zstd::Compressor->new($level);
    while (read(*STDIN, my $buffer, ZSTD_CSTREAM_IN_SIZE)) {
        print STDOUT $compressor->compress($buffer);
    }
    print STDOUT $compressor->end;
}
 
__END__


用法3.1

将文件内容作为输入内容压缩处理后将二进制文件流重定向到压缩文件中
perl steam.zstdex0.pl -6 < yasm.raw.txt > yasm.zst

将压缩文件传入脚本后解压输出到终端屏幕
perl steam.zstdex0.pl -d < yasm.zst



最新版本:0.1


项目主页:https://metacpan.org/release/Compress-Zstd