Zip folder using Julia

Hi!
How to zip an entire folder using Julia?
I tried using ZipFile with no success.
Thanks.

any reason you need to do this “using Julia”? you can just use zip command line, you can let julia run that command.

1 Like

I was trying to use pure Julia. Currently, I am running a shell command.

again, I’m curious WHY is “pure” Julia important for this usage. I mean the zlib that Julia uses in stdlib is not written in Julia.

1 Like

There are use-cases where zipping inside Julia is useful, because many “data formats” are basically only some zipped files. Exporting such formats from Julia need a working zipping-tool.

Using the commandline relies on some pre-installed software, which is at least not platform independent.
(I think ZipFile is also only just a C-code-wrapper, but at least the external dependecies are resolved on the system when installing the Julia library).

I am struggling with this, too. The closest I currently have works only if there are no sub-directories inside the folder to pack:

function zip(tar_file, src_dir)
    zdir = ZipFile.Writer(tar_file) 
    for (root, dirs, files) in walkdir(src_dir)
        for file in files
            filepath = joinpath(root, file)
            f = open(filepath, "r")
            content = read(f, String)
            close(f)
            zf = ZipFile.addfile(zdir, basename(filepath));
            write(zf, content)
        end
    end
    close(zdir)
end

Maybe create a tar file of the folder (GitHub - JuliaIO/Tar.jl: TAR files: create, list, extract them in pure Julia) and then zip the tar file?

3 Likes

Thanks, didn’t know that one!
But sometimes it is explicitly needed to export a *.zip, so it would be usefull to have a solution for this, too.

1 Like