Skip to content

OpenRiak/zstd-erlang

This branch is 7 commits ahead of, 3 commits behind AdRoll/zstd-erlang:main.

Folders and files

NameName
Last commit message
Last commit date
Oct 25, 2024
Jan 13, 2024
Jan 6, 2025
Jan 13, 2024
Jan 13, 2024
Sep 20, 2023
Apr 7, 2022
Sep 2, 2016
Oct 25, 2024
Sep 2, 2016
Feb 26, 2021
Aug 21, 2023
Sep 2, 2016
Oct 25, 2024

Repository files navigation

zstd-erlang

ZSTD OpenRiak Status

Zstd binding for Erlang/Elixir

http://facebook.github.io/zstd/

The current version uses v1.5.4

Usage

zstd:compress(Binary).
zstd:compress(Binary, CompressionLevel).
zstd:decompress(Binary).
1> Compressed = zstd:compress(<<"Hello, World!">>).
<<40,181,47,253,32,13,105,0,0,72,101,108,108,111,44,32,87,
  111,114,108,100,33>>
2> zstd:decompress(Compressed).
<<"Hello, World!">>
compress_file() ->
    {ok, File} = file:open("test.txt.zst", [write, raw, delayed_write, sync]),
    Bin = << <<"A">> || _ <- lists:seq(1, 1024 * 1024) >>,
    CStream = zstd:new_compression_stream(),
    ok = zstd:compression_stream_init(CStream),
    {ok, ResC} = zstd:stream_compress(CStream, Bin),
    file:write(File, ResC),
    {ok, ResF} = zstd:stream_flush(CStream),
    file:write(File, ResF),
    file:close(File).

decompress_file() ->
    {ok, Data} = file:read_file("test.txt.zst"),
    DStream = zstd:new_decompression_stream(),
    ok = zstd:decompression_stream_init(DStream),
    {ok, Bin} = zstd:stream_decompress(DStream, Data),
    io:format("~s", [Bin]),
    io:format("Size: ~p~n", [size(Bin)]).

decompress_large_file() ->
    DStream = zstd:new_decompression_stream(),
    ok = zstd:decompression_stream_init(DStream),

    {ok, RFile} = file:open("somefile.log.ztd", [read, raw, binary, read_ahead]),
    {ok, WFile} = file:open("somefile.log", [write, raw, delayed_write, sync]),
    read_file(RFile, WFile, DStream),
    file:close(RFile),
    file:close(WFile).

read_file(RFile, WFile, DStream) ->
    case file:read(RFile, 1024 * 8) of
        {ok, Data} ->
            {ok, Bin} = zstd:stream_decompress(DStream, Data),
            file:write(WFile, Bin),
            read_file(RFile, WFile, DStream);
        eof ->
            ok
    end.

For Elixir

iex(1)> compressed = :zstd.compress("Hello, World!")
<<40, 181, 47, 253, 32, 13, 105, 0, 0, 72, 101, 108, 108, 111, 44, 32, 87, 111,
  114, 108, 100, 33>>
iex(2)> :zstd.decompress(compressed)
"Hello, World!"

About

Zstd binding for Erlang/Elixir

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C 85.1%
  • C++ 12.9%
  • Makefile 1.2%
  • Assembly 0.4%
  • Erlang 0.3%
  • Starlark 0.1%