Apero.Compress (Apero v1.0.0)

Copy Markdown View Source

Universal compression and archive utilities.

Wraps system-level tools behind a consistent {:ok, result} | {:error, reason} interface. Supports: zip, tar (gzip/bzip2/xz/zstd), gzip, gunzip, zstd, xz, bzip2, 7z, rar. Auto-detects format from file extension.

Security

All paths passed to shell commands are properly escaped. Passwords are passed via command-line flags (-P for zip/unzip, -p for 7z) instead of shell piping to avoid shell injection vectors.

Summary

Functions

Compresses a single file with the specified algorithm. Replaces the original with the compressed version.

Creates a 7z archive from source paths.

Creates a RAR archive from source paths.

Decompresses a single file. Algorithm auto-detected from extension.

Detects the archive or compression type from a file extension.

Extracts an archive, auto-detecting the format from its extension.

Extracts a 7z archive.

Extracts a RAR archive.

Decompresses a gzip file, keeping the original.

Compresses a file with gzip (replaces original with .gz).

Lists the contents of an archive. Auto-detects format.

Creates a tar archive. Supports compression: :gzip, :bzip2, :xz, :zstd, :none.

Extracts a tar archive. Compression auto-detected from extension.

Extracts a zip archive.

Creates a zip archive from source paths. Supports password.

Types

archive_type()

@type archive_type() ::
  :zip
  | :tar
  | :tar_gz
  | :tar_bz2
  | :tar_xz
  | :tar_zst
  | :gz
  | :zst
  | :xz
  | :bz2
  | :seven_z
  | :rar
  | :unknown

single_algo()

@type single_algo() :: :zstd | :xz | :bzip2 | :gzip

Functions

compress(file, algo)

@spec compress(binary(), single_algo()) :: {:ok, binary()} | {:error, binary()}

Compresses a single file with the specified algorithm. Replaces the original with the compressed version.

create_7z(output, files, opts \\ [])

@spec create_7z(binary(), [binary()], keyword()) ::
  {:ok, binary()} | {:error, binary()}

Creates a 7z archive from source paths.

create_rar(output, files, opts \\ [])

@spec create_rar(binary(), [binary()], keyword()) ::
  {:ok, binary()} | {:error, binary()}

Creates a RAR archive from source paths.

decompress(file, algo)

@spec decompress(binary(), single_algo()) :: {:ok, binary()} | {:error, binary()}

Decompresses a single file. Algorithm auto-detected from extension.

detect_type(path)

@spec detect_type(binary()) :: archive_type()

Detects the archive or compression type from a file extension.

Examples

iex> Apero.Compress.detect_type("backup.tar.gz")
:tar_gz
iex> Apero.Compress.detect_type("data.zst")
:zst
iex> Apero.Compress.detect_type("unknown.xyz")
:unknown

extract(file, opts \\ [])

@spec extract(
  binary(),
  keyword()
) :: {:ok, binary()} | {:error, binary()}

Extracts an archive, auto-detecting the format from its extension.

Options

  • :output — destination directory (default: current directory)
  • :password — optional password for zip/7z/rar

Examples

Apero.Compress.extract("backup.tar.gz", output: "/tmp/restore")

extract_7z(file, opts \\ [])

@spec extract_7z(
  binary(),
  keyword()
) :: {:ok, binary()} | {:error, binary()}

Extracts a 7z archive.

extract_rar(file, opts \\ [])

@spec extract_rar(
  binary(),
  keyword()
) :: {:ok, binary()} | {:error, binary()}

Extracts a RAR archive.

gunzip(file)

@spec gunzip(binary()) :: {:ok, binary()} | {:error, binary()}

Decompresses a gzip file, keeping the original.

gzip(file)

@spec gzip(binary()) :: {:ok, binary()} | {:error, binary()}

Compresses a file with gzip (replaces original with .gz).

list(file)

@spec list(binary()) :: {:ok, [binary()]} | {:error, binary()}

Lists the contents of an archive. Auto-detects format.

Returns a list of file name strings.

tar(output, input, opts \\ [])

@spec tar(binary(), binary(), keyword()) :: {:ok, binary()} | {:error, binary()}

Creates a tar archive. Supports compression: :gzip, :bzip2, :xz, :zstd, :none.

untar(file, opts \\ [])

@spec untar(
  binary(),
  keyword()
) :: {:ok, binary()} | {:error, binary()}

Extracts a tar archive. Compression auto-detected from extension.

unzip(file, opts \\ [])

@spec unzip(
  binary(),
  keyword()
) :: {:ok, binary()} | {:error, binary()}

Extracts a zip archive.

zip(output, files, opts \\ [])

@spec zip(binary(), [binary()], keyword()) :: {:ok, binary()} | {:error, binary()}

Creates a zip archive from source paths. Supports password.