# Zip folder using Julia

**URL:** https://discourse.julialang.org/t/zip-folder-using-julia/69565
**Category:** General Usage
**Tags:** question, filesystem, zip
**Created:** [October 11, 2021, 3:05pm UTC](https://discourse.julialang.org/t/zip-folder-using-julia/69565 "2021-10-11T15:05:49Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![RaulDurand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rauldurand/32/2044_2.png) [@RaulDurand](https://discourse.julialang.org/u/RaulDurand)
#### Post date: [October 11, 2021, 3:05pm UTC](https://discourse.julialang.org/t/zip-folder-using-julia/69565/1 "2021-10-11T15:05:49Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [October 11, 2021, 3:19pm UTC](https://discourse.julialang.org/t/zip-folder-using-julia/69565/2 "2021-10-11T15:19:32Z")

</div>

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

---

<div class="post-metadata">

### Author: ![RaulDurand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rauldurand/32/2044_2.png) [@RaulDurand](https://discourse.julialang.org/u/RaulDurand)
#### Post date: [October 11, 2021, 7:22pm UTC](https://discourse.julialang.org/t/zip-folder-using-julia/69565/4 "2021-10-11T19:22:39Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [October 11, 2021, 9:21pm UTC](https://discourse.julialang.org/t/zip-folder-using-julia/69565/5 "2021-10-11T21:21:48Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ThummeTo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thummeto/32/26105_2.png) [@ThummeTo](https://discourse.julialang.org/u/ThummeTo)
#### Post date: [January 5, 2022, 1:41pm UTC](https://discourse.julialang.org/t/zip-folder-using-julia/69565/6 "2022-01-05T13:41:34Z")

</div>

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:

```julia
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

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 5, 2022, 1:47pm UTC](https://discourse.julialang.org/t/zip-folder-using-julia/69565/7 "2022-01-05T13:47:01Z")

</div>

Maybe create a tar file of the folder ([https://github.com/JuliaIO/Tar.jl](https://github.com/JuliaIO/Tar.jl)) and then zip the tar file?

---

<div class="post-metadata">

### Author: ![ThummeTo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thummeto/32/26105_2.png) [@ThummeTo](https://discourse.julialang.org/u/ThummeTo)
#### Post date: [January 7, 2022, 7:54am UTC](https://discourse.julialang.org/t/zip-folder-using-julia/69565/8 "2022-01-07T07:54:34Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Aaron\_Denney](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaron_denney/32/37067_2.png) [@Aaron\_Denney](https://discourse.julialang.org/u/Aaron_Denney)
#### Post date: [January 12, 2023, 10:25pm UTC](https://discourse.julialang.org/t/zip-folder-using-julia/69565/9 "2023-01-12T22:25:39Z")

</div>

Just take out the “basename”. The only way zip files record directory structure is by recording the full relative path name.

Well, you might need to do something special on Windows and normalize the path separator to ‘/’. (Don’t have easy access, so can’t test.)

---

<div class="post-metadata">

### Author: ![ThummeTo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thummeto/32/26105_2.png) [@ThummeTo](https://discourse.julialang.org/u/ThummeTo)
#### Post date: [January 13, 2023, 7:41am UTC](https://discourse.julialang.org/t/zip-folder-using-julia/69565/10 "2023-01-13T07:41:47Z")

</div>

Exactly, this was working for me too.

```julia
zipfile = "[path to my ZIP file].zip"
comprDir = "[path you want to zip]"
compress = true # or false, like you wish
zdir = ZipFile.Writer(zipfile) 
for (root, dirs, files) in walkdir(comprDir)
    for file in files
        filepath = joinpath(root, file)
        f = open(filepath, "r")
        content = read(f, String)
        close(f)

        zippath = subtractPath(filepath, comprDir * "/")
        println("\t$(zippath)")
        zf = ZipFile.addfile(zdir, zippath; method=(compress ? ZipFile.Deflate : ZipFile.Store));
        write(zf, content)
    end
end
close(zdir)

```
