I have a list of file tuples for which I want to create a zip file in memory. The code works as expected, but now I have an extra requirement. The resulting files in the zip have the creation date set to ‘now’; which is to be expected. However I want to be able to set that creation date myself. (Long story short: I have the correct date for that data in the database).
But no matter how I pass the file_info
to the zip method, I always end up getting {:error, :einval}
Here is my latest version, where I just used the file_info
struct I could get from a file on disk:
defmodule ZipHelper do
@doc """
Creates a valid file_info tuple.
`data` is the file binary.
`date` is a tuple in the format {{year, month, day}, {hour, minute, second}}.
"""
def make_file_info(data, date) do
size = byte_size(data)
type = :regular
mode = 0o644
# Using default values for the remaining fields:
major_device = 0
minor_device = 0
inode = 0
links = 1
uid = 0
gid = 0
{:file_info, size, type, mode, date, date, date, mode, links, major_device, minor_device,
inode, uid, gid}
end
end
defmodule ZipHelperDemo do
def run() do
date = {{2022, 3, 14}, {10, 28, 2}}
data = File.read!("dummy.pdf")
{:ok, file_info} = :file.read_file_info("dummy.pdf")
# file_info = ZipHelper.make_file_info(data, date)
files = [
{~c"dummy.pdf", data, [file_info]}
]
path = "."
:zip.create("full.zip", files, [{:cwd, path}])
end
end