# EOFError when extracting from a ZipFile inside a function

**URL:** <https://discourse.julialang.org/t/eoferror-when-extracting-from-a-zipfile-inside-a-function/124511>\
**Category:** General Usage\
**Created:** [January 7, 2025, 7:43pm UTC](https://discourse.julialang.org/t/eoferror-when-extracting-from-a-zipfile-inside-a-function/124511 "2025-01-07T19:43:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mitch\_Phillipson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch_phillipson/32/42883_2.png) [@Mitch\_Phillipson](https://discourse.julialang.org/u/Mitch_Phillipson)\
**Post date:** [January 7, 2025, 7:43pm UTC](https://discourse.julialang.org/t/eoferror-when-extracting-from-a-zipfile-inside-a-function/124511/1 "2025-01-07T19:43:51Z")

</div>

I am trying to download a Zip file and extract a few files. When I run this code outside of a function, it works great. However, when wrapped in a function I get an EOFError.

I’ve included the two versions below. Any advice would be immensely appreciated.

## Non Function Version - Works

```julia
using Downloads
using ZipFile

data = (
    summary_make = "IOMake_After_Redefinitions_PRO_1997-2023_Summary.xlsx",
    summary_use = "IOUse_After_Redefinitions_PRO_1997-2023_Summary.xlsx",
    detailed_make = "IOMake_After_Redefinitions_2017_Detail.xlsx",
    detailed_use = "IOUse_After_Redefinitions_PRO_2017_Detail.xlsx",
)

output_path = "data/national"

# Public link, no API key required
url = "https://apps.bea.gov/industry/iTables%20Static%20Files/AllTablesIO.zip"

X = Downloads.download(url, "tmp.zip")
r = ZipFile.Reader(X)

if !isdir(output_path)
    mkpath(output_path)
end

for f in r.files
    file_name = replace(f.name, " "=>"") #the bea file names may have random spaces
    if file_name∈data
        write(joinpath(output_path,file_name),read(f))
    end
end

close(r)
rm(X)

```

## Function Version - Throws error

```julia
function fetch_zip_data(
    url::String,
    data;
    output_path::String = joinpath(pwd(), "data"),
)
    if !isdir(output_path)
        mkpath(output_path)
    end

    X = Downloads.download(url, "tmp.zip")
    r = ZipFile.Reader(X)

    for f in r.files
        file_name = replace(f.name, " "=>"")
        if file_name∈data
            write(joinpath(output_path,file_name),read(f))
        end
    end
    close(r)
    rm(X)
    return true
end

fetch_zip_data(url, data)

```

---

<div class="post-metadata">

**Author:** ![nhz2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhz2/32/44428_2.png) [@nhz2](https://discourse.julialang.org/u/nhz2)\
**Post date:** [January 7, 2025, 9:24pm UTC](https://discourse.julialang.org/t/eoferror-when-extracting-from-a-zipfile-inside-a-function/124511/2 "2025-01-07T21:24:59Z")

</div>

It looks like you have a typo where `fetch_zip_file(url, data)` should be `fetch_zip_data(url, data)`

---

<div class="post-metadata">

**Author:** ![Mitch\_Phillipson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch_phillipson/32/42883_2.png) [@Mitch\_Phillipson](https://discourse.julialang.org/u/Mitch_Phillipson)\
**Post date:** [January 7, 2025, 9:31pm UTC](https://discourse.julialang.org/t/eoferror-when-extracting-from-a-zipfile-inside-a-function/124511/3 "2025-01-07T21:31:51Z")

</div>

This is true, it’s what happens when you change the function name when asking a question and then don’t run the code locally to test.

I’ll fix that error, but the end of file error is still present.

---

<div class="post-metadata">

**Author:** ![nhz2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhz2/32/44428_2.png) [@nhz2](https://discourse.julialang.org/u/nhz2)\
**Post date:** [January 7, 2025, 9:39pm UTC](https://discourse.julialang.org/t/eoferror-when-extracting-from-a-zipfile-inside-a-function/124511/4 "2025-01-07T21:39:33Z")

</div>

What is the output of

```julia
import Pkg
Pkg.status(Pkg.PKGMODE_MANIFEST)

```

and

```julia
versioninfo()

```

?  
The function works for me.

---

<div class="post-metadata">

**Author:** ![Mitch\_Phillipson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch_phillipson/32/42883_2.png) [@Mitch\_Phillipson](https://discourse.julialang.org/u/Mitch_Phillipson)\
**Post date:** [January 7, 2025, 9:52pm UTC](https://discourse.julialang.org/t/eoferror-when-extracting-from-a-zipfile-inside-a-function/124511/5 "2025-01-07T21:52:11Z")

</div>

Good news, it works for me now too. 😑

I think the issue was order of operations related, I tested the non-function version and that kept a lock on the file, or something.

Thank you for testing this for me. I’m am very relieved that the fix was incredibly easy, and the error was totally my fault.

---

<div class="post-metadata">

**Author:** ![nhz2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhz2/32/44428_2.png) [@nhz2](https://discourse.julialang.org/u/nhz2)\
**Post date:** [January 7, 2025, 10:12pm UTC](https://discourse.julialang.org/t/eoferror-when-extracting-from-a-zipfile-inside-a-function/124511/6 "2025-01-07T22:12:14Z")

</div>

Also, you might want to add some extra check that none of the names in `data` are absolute paths or other weird things, otherwise `write(joinpath(output_path,file_name), ...` may overwrite some important file on your computer.
