Last active
September 13, 2020 07:18
-
-
Save ryochin/576c5b46a7e7b0e1fc8052540facedd7 to your computer and use it in GitHub Desktop.
Elixir: gunzip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@spec gunzip(binary) :: {:ok, binary} | {:error, term} | |
def gunzip(data) when is_binary(data) do | |
try do | |
z = :zlib.open() | |
:ok = :zlib.inflateInit(z, 16 + 15) | |
result = | |
z | |
|> :zlib.inflate(data) | |
|> List.flatten() | |
|> Enum.join() | |
:ok = :zlib.inflateEnd(z) | |
:ok = :zlib.close(z) | |
{:ok, result} | |
rescue | |
e -> {:error, inspect(e)} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see also: