How to read gz file line by line in Perl6

I would recommend using the module Compress::Zlib for this purpose. You can find the readme and code on github and install it with zef install Compress::Zlib.

This example is taken from the test file number 3 titled "wrap":

use Test;
use Compress::Zlib;

gzspurt("t/compressed.gz", "this\nis\na\ntest");

my $wrap = zwrap(open("t/compressed.gz"), :gzip);
is $wrap.get, "this\n", 'first line roundtrips';
is $wrap.get, "is\n", 'second line roundtrips';
is $wrap.get, "a\n", 'third line roundtrips';
is $wrap.get, "test", 'fourth line roundtrips';

This is probably the easiest way to get what you want.


use the read-file-content method in the Archive::Libarchive module, but i don't know if the method read all lines into memory at once:

use Archive::Libarchive; 
use Archive::Libarchive::Constants;

my $a = Archive::Libarchive.new: operation => LibarchiveRead, file => 'test.tar.gz';
my Archive::Libarchive::Entry $e .= new;

my $log = '';
while $a.next-header($e) {
    $log = get-log($a,$e) if $e.pathname.ends-with('.txt');
}

sub get-log($a, $e) {
    return $a.read-file-content($e).decode('UTF8-C8');
}

Tags:

Raku