I have been using :zip.create/3
without issues for quite some time.
After updating to Elixir 1.9.2 on Erlang 22, I now get this error: {:error, :einval}
Any help please?
defmodule Utils do
def zip_test do
data = {"demo.txt", File.read!("demo.txt")}
IO.puts(inspect(data, @format))
:zip.create("demo.zip", [data], [:memory])
end
end
iex> Utils.zip_test
{"demo.txt", "this is a demo"}
{:error, :einval}
iex>
The types of the arguments required for the erlang function :zip.create() are erlang string types. In erlang, a string type is a list of integers. In erlang, as a shortcut, you can create a list of integers with double quotes, e.g. "hello". The list will contain the ASCII codes for the specified characters. On the other hand, in elixir double quotes create an elixir string, which is equivalent to an erlang binary type. Therefore, you are providing binary arguments when you need to be providing lists of integers.
You can use the elixir function String.to_charlist() to create a list of integers from an elixir string:
:zip.create(String.to_charlist("demo.zip"),
[String.to_charlist("demo.txt")],
[:memory])
Or, you can just use single quotes in elixir to create a list of integers:
:zip.create('demo.zip',
['demo.txt'],
[:memory])
For more info, see Erlang Interoperability.