Hris

Hris

Make a zip file in Elixir

Hello.
I have a text file and I want to make a zip file from it using Elixir or just to compress its folder.

files = File.ls!("path") 
           |> Enum.map(fn filename -> Path.join("path", filename) end)
           |> Enum.map(&String.to_charlist/1)

I then use :zip.create("foo.zip", files) to write the zip file. It returns a tuple: {:ok, "foo.zip"} but the file does not get saved.
Where is my mistake? Or can I get some sort of a solution? Thanks.

Marked As Solved

lackac

lackac

According to the Erlang docs on :zip.create/3 there is an optional third argument with options where you could set the working directory for compression with [cwd: path]. The effect is that files will be looked up within that path and will omit the path in the compressed file.

Expanding on your example:

files =
  File.ls!(path)
  |> Enum.map(&String.to_charlist/1)

:zip.create("files.zip", files, cwd: path)
$ unzip -l files.zip
Archive:  files.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
   212151  09-22-2016 17:32   file1.txt
   333814  09-22-2016 17:32   file2.txt
   269066  09-22-2016 17:32   file3.txt
   333736  09-22-2016 17:32   file4.txt
   284213  09-22-2016 17:32   file5.txt
---------                     -------
  1432980                     5 files

Also Liked

NobbZ

NobbZ

The file is created in the working dir of the running BEAM VM, you can check with System.cwd/0.

Qqwy

Qqwy

TypeCheck Core Team

And usually this is the folder that your ‘mix.exs’ is also in. If you are running in development that is.
When you have made a release, the folder location might be different.

Where Next?

Popular in Questions Top

freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement