zip:create 在 windows 上似乎在 Erlang 22 中不起作用

zip:create seems not to work in Erlang 22, on windows

我使用 :zip.create/3 已经有一段时间没有问题了。

在 Erlang 22 上更新到 Elixir 1.9.2 后,我现在收到此错误:{:error, :einval}

有什么帮助吗?

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>

erlang函数:zip.create()所需的参数类型为erlang字符串类型。在 erlang 中,字符串类型是整数列表。在 erlang 中,作为一种快捷方式,您可以创建一个带双引号的整数列表,例如"hello"。该列表将包含指定字符的 ASCII 代码。另一方面,在 elixir 双引号中创建一个 elixir string,它等同于 erlang binary 类型。因此,当您需要提供整数列表时,您将提供二进制参数。

您可以使用 elixir 函数 String.to_charlist() 从 elixir 字符串创建整数列表:

:zip.create(String.to_charlist("demo.zip"),
           [String.to_charlist("demo.txt")],
           [:memory])

或者,您可以只在 elixir 中使用单引号来创建整数列表:

:zip.create('demo.zip', 
            ['demo.txt'], 
            [:memory])

有关详细信息,请参阅 Erlang Interoperability