[erlang-questions] zlib deflate problem

Colm colm.dougan@REDACTED
Mon May 19 19:55:34 CEST 2008


Hi,

I'm having a problem with erlang's zlib/deflate.  The problem is that
if I try to deflate a piece of data that is a multiple of 4000 bytes
(which also happens to be the zlib internal buffer size, I noticed)
and then inflate it I get an inflate exception.  Below is some sample
code.

I realize I can set different internal buffer size using
zlib:setBufSize but that doesn't really help as I can't predict the
size of data I will be deflating/inflating.

Am I doing something dumb here or is this a bug in the erlang zlib
interface?  I strongly suspect it is the former.

Thanks,
Colm



-module(zlib_deflate_problem).
-export([start/0, start/1]).

-define(DATA_SIZE, 4000).

start() ->
  start(?DATA_SIZE).

start(DataSize) ->
   % Generate a binary of size "0" x DATA_SIZE
   Data = list_to_binary(
       lists:map(fun(_) -> 0 end, lists:seq(1, DataSize))
   ),
   io:format("Raw data -> ~p~n", [size(Data)]),

   ZInflate = zlib:open(),
   ok = zlib:inflateInit(ZInflate),

   ZDeflate = zlib:open(),
   ok = zlib:deflateInit(ZDeflate),

   Deflated = zlib:deflate(ZDeflate, Data, sync),
   io:format("Deflated -> ~p~n", [Deflated]),

   Inflated = case catch zlib:inflate(ZInflate, Deflated) of
       {'EXIT', {'data_error', _Backtrace} } ->
           io:format("zlib:inflate data_error~n"),
           [];
       {'EXIT', Reason} ->
           io:format("zlib:inflate error -> [~p]~n", [Reason]),
           [];
       [] ->
           io:format("zlib:inflate empty response~n"), [];
       Iolist ->
           Iolist
   end,

   io:format("Inflated -> ~p~n", [iolist_size(Inflated)]),

   ok.



More information about the erlang-questions mailing list