Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compress a directory in Elixir?

Tags:

elixir

I know how to open up a zip file in write mode in Elixir:

file = File.open("myzip.zip", [:write, :compressed])

but after this, say if I have a directory /home/lowks/big_files, how do I write this into file ?

like image 696
Muhammad Lukman Low Avatar asked Mar 21 '14 07:03

Muhammad Lukman Low


People also ask

How do I compress a directory?

To zip (compress) a file or folder Locate the file or folder that you want to zip. Press and hold (or right-click) the file or folder, select (or point to) Send to, and then select Compressed (zipped) folder. A new zipped folder with the same name is created in the same location.

How do I compress a folder in R?

How to zip files in R. To zip files in R, use the zip() function. The zipped file is stored inside the current directory unless a different path is specified in the zip() function argument. The zip() method creates a new ZIP archive, and it overwrites the output file if it exists.

What does it mean to extract a zip file?

Extract/Unzip Zipped Files When you extract files from a zipped folder, a new folder with the same name is created which contains the files. The compressed (zipped) version also remains. Right-click the zipped folder saved to your computer.


1 Answers

If you are operating on zip files, you'll need to use :zip.extract('foo.zip'), and :zip.create(name, [{'foo', file1data}, file2path, ...]).

:zip.create takes a name, and a list which can contain either of two options:

  1. A tuple containing a file name, and binary data to zip.
  2. A path to a file to zip.

:zip.extract can either take a path to a file, or binary data representing the zip archive (perhaps from doing File.open on a zip).

You can do something like the following to take a list of files in a path and zip them up:

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

:zip.create('foo.zip', files)
like image 74
bitwalker Avatar answered Sep 17 '22 00:09

bitwalker