Matrix Science Mascot Parser toolkit
 
Loading...
Searching...
No Matches
tools_ms_zip.pl

Using the tinycdb index files.

#!/usr/local/bin/perl
##############################################################################
# file: tools_ms_zip.pl #
# Mascot Parser toolkit example code #
##############################################################################
# COPYRIGHT NOTICE #
# Copyright 1998-2023 Matrix Science Limited All Rights Reserved. #
# #
##############################################################################
# $Source: parser/examples/test_perl/tools_ms_zip.pl $
# $Author: villek@matrixscience.com $
# $Date: 2023-11-22 16:25:28 +0000 $
# $Revision: 7442fec13bcb3ca4853591b3e3f55ce546d17719 | MSPARSER_REL_3_0_0-2024-09-24-0-g93ebaeb4f4 $
##############################################################################
use strict;
use v5.14;
use msparser;
use constant {
# Buffer size for reading the input file.
BUFSIZE => 1 * 1048576,
};
my $decompressed_fileIn = shift @ARGV or die "Usage: $0 infile.dat";
my $outfile = "example_ms_zip.$$.gz";
open(my $infile_fh, '<', $decompressed_fileIn) or die "unable to '$decompressed_fileIn' for reading: $!";
binmode($infile_fh);
my $zipper = msparser::ms_zip->new(0);
if (not $zipper->isValid) {
die "unable to initialise ms_zip: " . $zipper->getLastErrorString();
}
open(my $outfile_fh, '>', $outfile) or die "unable to open '$outfile' for writing: $!";
binmode($outfile_fh);
my $num_bytes = 0;
my $buffer;
while (1) {
my $r = read($infile_fh, $buffer, BUFSIZE);
if (not defined $r) {
die "error while reading '$decompressed_fileIn': $!";
}
say "(read $r uncompressed bytes)";
# When $r > 0, there is more data to compress, so do it.
#
# When $r == 0, it's EOF and $buffer is empty. Give it to
# compressMore() so we begin flushing.
my $data = $zipper->compressMore($buffer);
if (length($data) > 0) {
say "(received ", length($data), " compressed bytes)";
print $outfile_fh $data;
$num_bytes += length($data);
}
last if not $zipper->isValid();
last if $r == 0;
}
while ($zipper->isValid()) {
my $s = $zipper->compressMore('');
if (length($s) > 0) {
say "(received ", length($s), " compressed bytes)";
print $outfile_fh $s;
$num_bytes += length($s);
} else {
last;
}
}
close $outfile_fh or die "unable to close '$outfile': $!";
say "Wrote $num_bytes Compressed data in: $outfile";