Creating an in memory zip file

Hi there,

My application is related to the creation of educational content and I have added a route to my application that is reponsible for creating an in-memory zip file that contains the necessary files for a SCORM 1.2 package for loading into LMS platforms.

I have experience with SCORM packages (as boring and mundane as they are) and I have started playing with the erlang :zip module to create my in memory zip file which is then sent on to the users browser.

However, I am not sure which area of elixir I should be looking in when it comes to creating the in-memory files that are used to generate my module. For example, one of the files in the SCORM package is a simple HTML file that includes an IFRAME to content in my application.

Whats the neatest and most readable way to create this HTML file in-memory? I was thinking Phoenix.View.render_to_iodata but then wasn’t sure of the implications of including templates in my templates directory that aren’t used by any views that are accessible via URL…

Hi,

You can use EEx.eval_string/3 or EEx.eval_file/3. See https://hexdocs.pm/eex/EEx.html

Sorry, I should have specified that I also want to dynamically create this file as depending on what element I am exporting will adjust the generated HTML file that composes the SCORM package.

So far I have pursued the avenue of creating a new view called ScormView and create associated templates in the appropriate templates folder /templates/scorm. For example, the SCORM manifest file is located at:

/project_root/lib/mannequin_web/templates/scorm/imsmanifest.xml.eex

I then render it to a string like this:

Phoenix.View.render_to_string(MannequinWeb.ScormView, "imsmanifest.xml", %{schema: "my_schema_name"})

And then use the string as part of the data sent to the zip command. I haven’t executed the :zip.create call yet using this string data, but based on my understanding of elixir, I should be able to use the generated template string as binary data… I’ll try that tommorow.

Do you see any particular issues using the above approach?

Cheers…

I’ve had a similar problem to solve. Create a zip file in memory from bunch of files that I had content in strings. And then base64 it. The safest way is to use zip module from erlang. Theres a create method that takes filename, list of files and options where you can pass that you wanna work in memory. This is what I came up with.

{:ok, {_filename, data}} = :zip.create("extension.zip", [{'file1.txt', "content of file 1"}, {'file2.txt',"content of file 2"}], [:memory])
data |> Base.encode64
2 Likes

Sweet - thanks!