-module(zipbeam). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/zipbeam.gleam"). -export([new/0, add_file/3, create_in_memory/1, open/1, files/1, get/2, close/1]). -export_type([zip_builder/0, zip_handle/0, file/0]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. -opaque zip_builder() :: {zip_builder, list({binary(), bitstring()})}. -type zip_handle() :: any(). -type file() :: {file, binary(), integer()}. -file("src/zipbeam.gleam", 15). -spec new() -> zip_builder(). new() -> {zip_builder, []}. -file("src/zipbeam.gleam", 21). ?DOC(" Add a file to the builder, to be included in the zip archive.\n"). -spec add_file(zip_builder(), binary(), bitstring()) -> zip_builder(). add_file(Builder, Path, Contents) -> {zip_builder, [{Path, Contents} | erlang:element(2, Builder)]}. -file("src/zipbeam.gleam", 32). ?DOC(" Create a zip archive.\n"). -spec create_in_memory(zip_builder()) -> bitstring(). create_in_memory(Builder) -> zipbeam_ffi:create_in_memory(Builder). -file("src/zipbeam.gleam", 43). ?DOC( " Open a zip archive. Returns an error if the data given could not be parsed\n" " as a zip archive.\n" "\n" " The archive must be closed with `close`.\n" "\n" " The handle is closed if the process that originally opened the archive\n" " dies.\n" ). -spec open(bitstring()) -> {ok, zip_handle()} | {error, nil}. open(Zip) -> zipbeam_ffi:open(Zip). -file("src/zipbeam.gleam", 50). ?DOC( " List the files in the zip archive, including their uncompressed sized.\n" "\n" " Returns an error if the handle has already been closed.\n" ). -spec files(zip_handle()) -> {ok, list(file())} | {error, nil}. files(Handle) -> zipbeam_ffi:files(Handle). -file("src/zipbeam.gleam", 60). ?DOC( " Get a file from the archive.\n" "\n" " ## Security\n" "\n" " You must always check the size of the file with `files` before extracting a\n" " file, to prevent zip-bomb attacks.\n" ). -spec get(zip_handle(), binary()) -> {ok, bitstring()} | {error, nil}. get(Handle, Path) -> zipbeam_ffi:get(Handle, Path). -file("src/zipbeam.gleam", 65). ?DOC(" Close the handle.\n"). -spec close(zip_handle()) -> nil. close(Handle) -> zipbeam_ffi:close(Handle).