# How to extract a file in a zip archive (without using OS specific tools)?

**URL:** https://discourse.julialang.org/t/how-to-extract-a-file-in-a-zip-archive-without-using-os-specific-tools/34585
**Category:** General Usage
**Tags:** io, zip
**Created:** [February 13, 2020, 4:12pm UTC](https://discourse.julialang.org/t/how-to-extract-a-file-in-a-zip-archive-without-using-os-specific-tools/34585 "2020-02-13T16:12:30Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [February 13, 2020, 4:12pm UTC](https://discourse.julialang.org/t/how-to-extract-a-file-in-a-zip-archive-without-using-os-specific-tools/34585/1 "2020-02-13T16:12:30Z")

</div>

I am trying to extract a file from a zip archive using ZipFile.jl, but I don’t know how to write it back to disk:

```julia
using ZipFile
zarchive = ZipFile.Reader("myarchive.zip")
myf = zarchive.files[1]
out = open(myf.name,"w")
write(out,myf) # error
close(out)
close(zarchive)

```

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [February 13, 2020, 4:44pm UTC](https://discourse.julialang.org/t/how-to-extract-a-file-in-a-zip-archive-without-using-os-specific-tools/34585/2 "2020-02-13T16:44:09Z")

</div>

This is the solution I found. It works in my case (shapefiles, a zipfile wit ha single subdirectory level) but I don’t think it is very “solid” (e.g. may not work with other kind of binary files, as I am using `String` as a temporary medium…):

```julia
zarchive = ZipFile.Reader("myarchive.zip")

for f in zarchive.files
    println(f.name)
    fullFilePath = joinpath(rootDir,dataFolder,f.name)
    if endswith(f.name,"/")
        mkdir(fullFilePath)
    else
        out = open(fullFilePath,"w")
        write(out,read(f,String))
        close(out) 
    end
end

close(zarchive)

```

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [February 13, 2020, 5:02pm UTC](https://discourse.julialang.org/t/how-to-extract-a-file-in-a-zip-archive-without-using-os-specific-tools/34585/3 "2020-02-13T17:02:52Z")

</div>

Maybe you can use `Vector{UInt8}` instead of `String`? It should give more generic solution

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [February 13, 2020, 7:01pm UTC](https://discourse.julialang.org/t/how-to-extract-a-file-in-a-zip-archive-without-using-os-specific-tools/34585/4 "2020-02-13T19:01:30Z")

</div>

> [@sylvaticus](#):
>
> out = open(fullFilePath,“w”)  
> write(out,read(f,String))  
> close(out)

If you prefer to write less, you can shorten that to

```julia
write(fullFilePath, read(f, String))

```

Without having tested I suspect that you can also shorten `read(f, String)` to `read(f)` for this use case.

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [February 14, 2020, 11:16am UTC](https://discourse.julialang.org/t/how-to-extract-a-file-in-a-zip-archive-without-using-os-specific-tools/34585/5 "2020-02-14T11:16:26Z")

</div>

Thank you all.

I ended up writing the following function:

# unzip(file,exdir=“”)

**Unzip a zipped archive using ZipFile**

# Arguments

- `file`: a zip archive to unzip and extract (absolure or relative path)
- `exdir=""`: an optional directory to specify the root of the folder where to extract the archive (absolute or relative).

# Notes:

- The function doesn’t perform a check to see if all the zipped files have a common root.

# Examples

```julia-auto
julia> unzip("myarchive.zip",exdir="mydata")

```

```julia-auto
function unzip(file,exdir="")
    fileFullPath = isabspath(file) ? file : joinpath(pwd(),file)
    basePath = dirname(fileFullPath)
    outPath = (exdir == "" ? basePath : (isabspath(exdir) ? exdir : joinpath(pwd(),exdir)))
    isdir(outPath) ? "" : mkdir(outPath)
    zarchive = ZipFile.Reader(fileFullPath)
    for f in zarchive.files
        fullFilePath = joinpath(outPath,f.name)
        if (endswith(f.name,"/") || endswith(f.name,"\\"))
            mkdir(fullFilePath)
        else
            write(fullFilePath, read(f))
        end
    end
    close(zarchive)
end

```

If one is interested, I added it to my own repository of utility functions ([GitHub - sylvaticus/LAJuliaUtils.jl: Utility functions for Julia, mainly dataframes operations](https://github.com/sylvaticus/LAJuliaUtils.jl))
