What is the best way to approve the contents of a zip archive in Elixir?

I am currently doing:

  • Test function allowing zip file / directory. It is claimed that it exists.
  • Using :zip.tand :zip.tt, let it list the contents of the zip folder to see what I expect.

Somehow I think something is missing. Is it better to test with :zip.table? The function looks confusing. Can someone give an example of use? Below is an example of the output I received, but I can’t figure out how to do this in a test? Is md5sum the best test for zip archives?

iex(4)> :zip.table('testing.zip')
{:ok,
 [{:zip_comment, []},
  {:zip_file, 'mix.exs',
   {:file_info, 930, :regular, :read_write, {{2015, 7, 15}, {2, 11, 9}},
    {{2015, 7, 15}, {2, 11, 9}}, {{2015, 7, 15}, {2, 11, 9}}, 54, 1, 0, 0, 0, 0,
    0}, [], 0, 444},
  {:zip_file, 'mix.lock',
   {:file_info, 332, :regular, :read_write, {{2015, 7, 15}, {2, 9, 6}},
    {{2015, 7, 15}, {2, 9, 6}}, {{2015, 7, 15}, {2, 9, 6}}, 54, 1, 0, 0, 0, 0,
    0}, [], 481, 152}]}
+4
source share
2 answers

:zip Erlang , .

-, zip_file, Erlang . , . File.Stat Elixir .

require Record

defmodule Zip.File do
  record = Record.extract(:zip_file, from_lib: "stdlib/include/zip.hrl")
  keys   = :lists.map(&elem(&1, 0), record)
  vals   = :lists.map(&{&1, [], nil}, keys)
  pairs  = :lists.zip(keys, vals)

  defstruct keys

  def to_record(%Zip.File{unquote_splicing(pairs)}) do
    {:zip_file, unquote_splicing(vals)}
  end

  def from_record(zip_file)
  def from_record({:zip_file, unquote_splicing(vals)}) do
    %Zip.File{unquote_splicing(pairs)}
    |> Map.update!(:info, fn(info) -> File.Stat.from_record(info) end)
  end
end

- Erlang zip. , , . list_files/1, , .

defmodule Zip do
  def open(archive) do
    {:ok, zip_handle} = :zip.zip_open(archive)
    zip_handle
  end

  def close(zip_handle) do
    :zip.zip_close(zip_handle)
  end

  def list_dir(zip_handle) do
    {:ok, result} = :zip.zip_list_dir(zip_handle)
    result
  end

  def list_files(zip_handle) do
    list_dir(zip_handle)
    |> Enum.drop(1)
    |> Enum.map(&Zip.File.from_record/1)
    |> Enum.filter(&(&1.info.type == :regular))
  end
end

, zip :

cd /tmp
touch foo bar baz
zip archive.zip foo bar baz

zip-:

test "files are in zip" do
  zip = Zip.open('/tmp/archive.zip')
  files = Zip.list_files(zip) |> Enum.map(&(&1.name))
  Zip.close(zip)
  assert files == ['foo', 'bar', 'baz']
end

zip , , .

+5

, zip . Elixir/Erlang, . :

  • . -.

  • .

  • zip .

  • - , .

  • , , 1. , .

  • . , 1. , .

, , , , , Erlang. zip- Erlang , , , , .

, , .

0
source

All Articles