[erlang-questions] Read from a compressed "GZIP" file

Vinayak Pawar <>
Fri Nov 27 16:26:04 CET 2009


Z = zlib:open(),
zlib:inflateInit(Z, 31),

% hopefully everything fits in memory
% if file size is big, you can do this read and inflate in chunks

CompressedBin = file:read_file(Filename),

Uncompressed = zlib:inflate(Z, CompressedBin),

zlib:inflateEnd(Z),
zlib:close(Z).

Hope this helps!

Thanks,
Vinayak

On Fri, Nov 27, 2009 at 7:41 PM, zabrane Mikael <> wrote:

> Hi List,
>
> Is there a way to read from a GZIPped file (example please)?
> I found a mode called "compressed" in the "file" module, but failed to
> understand how it works:
>
> $ erl
> 1> {ok, FH} = file:open("foo.gz", [read, compressed]).
> {ok,<0.42.0>}
>
> 2> {ok, Bin} = file:read(FH, 8).
> ** exception error: no match of right hand side value {error,terminated}
>
> Why am I faling to read 8 bytes from it?
> How can I read the whole file in one go?
>
> Regards
> Zabrane
>


More information about the erlang-questions mailing list