Skip to content

Commit

Permalink
Implement first archiver; pointless
Browse files Browse the repository at this point in the history
  • Loading branch information
Bananarchist committed Feb 14, 2023
1 parent b16b18a commit 2dc7c2a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/behaviors/archiver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ end

defmodule Archiver do
@doc """
Insertsd files into an archive
Inserts files into an archive
"""
@callback archive(String.t, [String.t]) :: {:ok, binary} | {:error, String.t}
@callback archive([String.t] | binary) :: {:ok, binary} | {:error, String.t}
end
25 changes: 25 additions & 0 deletions lib/zlb.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Zlb do
@behaviour Identification
@behaviour Extractor
@behaviour Archiver

@magic_number <<0x5A, 0x4C, 0x42, 0x00>>

Expand Down Expand Up @@ -36,4 +37,28 @@ defmodule Zlb do
end).()
end
end

@doc """
This puts files into the Sting "zlb" format
Passing multiple files will combine them, but this format contains no directory so it should only be used when such result is acceptable.
For the above reason, this method will not handle directories.
"""
@impl Archiver
def archive(file_names) when is_list(file_names) do
if Enum.all?(file_names, fn f -> File.exists?(f) and not File.dir?(f) end) do
file_names
|> Enum.map(&File.stream!/1)
|> Enum.join()
|> archive()
else
file_no_existe = Enum.find(file_names, fn f -> not File.exists?(f) end)
{:error, if(file_no_existe == nil, do: "Cannot handle directories", else: "#{file_no_existe} doesn't exist")}

end
end

@impl Archiver
def archive(file_data) when is_binary(file_data) do
{:ok, @magic_number <> <<byte_size(file_data)::little-integer-size(32)>> <> :zlib.compress(file_data) }
end
end

0 comments on commit 2dc7c2a

Please sign in to comment.