Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read gz file line by line in Perl6

Tags:

raku

I'm trying to read a huge gz file line by line in Perl6.

I'm trying to do something like this

my $file = 'huge_file.gz';
for $file.IO.lines -> $line {
    say $line;
}

But this give error that I have a malformed UTF-8. I can't see how to get this to read gzipped material from the help page https://docs.perl6.org/language/unicode#UTF8-C8 or https://docs.perl6.org/language/io

I want to accomplish the same thing as was done in Perl5: http://blog-en.openalfa.com/how-to-read-and-write-compressed-files-in-perl

How can I read a gz file line by line in Perl6?

thanks

like image 783
con Avatar asked Jan 02 '19 17:01

con


People also ask

How do I read .gz files?

Launch WinZip from your start menu or Desktop shortcut. Open the compressed file by clicking File > Open. If your system has the compressed file extension associated with WinZip program, just double-click on the file.

How do I read a .gz file in Perl?

use strict ; use warnings ; use Compress::Zlib; my $file = "test. gz"; my $gz = gzopen ($file, "rb") or die "Error Reading $file: $gzerrno"; while ($gz->gzreadline($_) > 0 ) { if (/pattern/) { print "$_----->PASS\n"; } } die "Error reading $file: $gzerrno" if $gzerrno !=

How do I read a gz file in Linux?

You can use the zcat command in Linux to view the contents of a compressed file without unzipping it. It works for all compression utilities, including Gzip.

How do I view .gz files without extracting?

Also It is Possible to read gzip file using a text editor. In Linux you can use command line text editor vim. With a Text Editor we can also edit and add new content to the file without extracting. If You are Not Comfortable with the command line you can use Graphical Editors like gedit or kate to read gzip files.


1 Answers

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.

like image 150
timotimo Avatar answered Oct 22 '22 14:10

timotimo