Unzip file

Hello people,
Why the Erlang function :zip.unzip won’t work with double quote on Elixir?

:zip.unzip(“file.zip”)
{:error,
{:EXIT,
{{:badmatch, “file.zip”},
[{:zip, :binary_io, 2, [file: ‘zip.erl’, line: 1726]},
{:zip, :get_end_of_central_dir, 3, [file: ‘zip.erl’, line: 1313]},
{:zip, :get_central_dir, 3, [file: ‘zip.erl’, line: 1269]},
{:zip, :do_unzip, 2, [file: ‘zip.erl’, line: 380]},
{:zip, :unzip, 2, [file: ‘zip.erl’, line: 370]},
{:erl_eval, :do_apply, 6, [file: ‘erl_eval.erl’, line: 670]},
{:elixir, :erl_eval, 3, [file: ‘src/elixir.erl’, line: 223]},
{:elixir, :eval_forms, 4, [file: ‘src/elixir.erl’, line: 211]}]}}}

But, when I try this

:zip.unzip(‘file.zip’)
It´s ok

1 Like

That is because "file.zip" is not valid zip data.

If you pass in a charlist it is a filename.
If you pass in a binary it is a filedata.
Thus <<"file.zip">> is not a valid zip data, there is no identifier, no header, nothing, so yep it fails. :slight_smile:

This is documented at http://erlang.org/doc/man/zip.html#unzip-1

If argument Archive is specified as a binary, the contents of the binary is assumed to be a zip archive, otherwise a filename.

2 Likes

Or more fundamentally “file.zip” and ‘file.zip’ are distinctly different values in Elixir. The first is a binary and the 2nd is a list of ascii integers.

2 Likes

As the other guys already said, strings in Elixir are binaries, and strings in Erlang are character lists.
But if you need to call an Erlang function that expects an Erlang string, do this (in Elixir):

elixir_string = "z.zip"
erlang_string = String.to_char_list(elixir_string)

e.g.

$ erl
Eshell V8.0  (abort with ^G)
1> zip:zip("z.zip", ["Readme.txt", "AtomSetup.exe"]).
{ok,"z.zip"}
2> zip:list_dir("z.zip").
{ok,[{zip_comment,[]},
     {zip_file,"Readme.txt",
               {file_info,916,regular,read_write,
                          {{2016,5,30},{8,28,23}},
                          {{2016,5,30},{8,28,23}},
                          {{2016,5,30},{8,28,23}},
                          54,1,0,0,0,0,0},
               [],0,492},
     {zip_file,"AtomSetup.exe",
               {file_info,88936200,regular,read_write,
                          {{2016,8,22},{10,29,26}},
                          {{2016,8,22},{10,29,26}},
                          {{2016,8,22},{10,29,26}},
                          54,1,0,0,0,0,0},
               [],532,88873181}]}
3>
brian@Brian-PC MINGW64 /m/Users/epail/Downloads
$ unzip -l z.zip
Archive:  z.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
      916  2016-05-30 08:28   Readme.txt
 88936200  2016-08-22 10:29   AtomSetup.exe
---------                     -------
 88937116                     2 files

brian@Brian-PC MINGW64 /m/Users/epail/Downloads
$ iex
Eshell V8.0  (abort with ^G)
Interactive Elixir (1.3.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> :zip.list_dir(String.to_char_list("z.zip"))
{:ok,
 [{:zip_comment, []},
  {:zip_file, 'Readme.txt',
   {:file_info, 916, :regular, :read_write, {{2016, 5, 30}, {8, 28, 23}},
    {{2016, 5, 30}, {8, 28, 23}}, {{2016, 5, 30}, {8, 28, 23}}, 54, 1, 0, 0, 0,
    0, 0}, [], 0, 492},
  {:zip_file, 'AtomSetup.exe',
   {:file_info, 88936200, :regular, :read_write, {{2016, 8, 22}, {10, 29, 26}},
    {{2016, 8, 22}, {10, 29, 26}}, {{2016, 8, 22}, {10, 29, 26}}, 54, 1, 0, 0,
    0, 0, 0}, [], 532, 88873181}]}
iex(2)>
1 Like

Thank you @OvermindDL1, @gregvaughn, @epailty! I am learning Elixir and the answers helped me a lot!

2 Likes