# 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:** 1\
**Showing post:** 5

<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))

---

_[View the full topic](https://discourse.julialang.org/t/how-to-extract-a-file-in-a-zip-archive-without-using-os-specific-tools/34585)._
