# Open XLSX from Zip Archive in Memory

**URL:** <https://discourse.julialang.org/t/open-xlsx-from-zip-archive-in-memory/95664>\
**Category:** General Usage\
**Tags:** question, io\
**Created:** [March 7, 2023, 12:50pm UTC](https://discourse.julialang.org/t/open-xlsx-from-zip-archive-in-memory/95664 "2023-03-07T12:50:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [March 7, 2023, 12:50pm UTC](https://discourse.julialang.org/t/open-xlsx-from-zip-archive-in-memory/95664/1 "2023-03-07T12:50:40Z")

</div>

I want to download this url, get the file named “ZIP5\_DEC2022\_FINAL.xlsx”, and read the table in as a DataFrame. But I can’t quite work out how to compose Zipfiles.jl with XLSX.jl.

Here is my code currently:

```julia
using DataFrames
using HTTP
using XLSX
using ZipFile

url = "https://www.cms.gov/files/zip/2022-end-year-zip-code-file.zip"
zipped_bytes = HTTP.get(url)
zip = ZipFile.Reader(IOBuffer(zipped_bytes.body))
file_index = findfirst((f) -> f.name == "ZIP5_DEC2022_FINAL.xlsx", zip.files)
file_contents = read(zip.files[file_index], String)
df = XLSX.openxlsx(IOBuffer(file_contents)) |> DataFrame
# ERROR: MethodError: no method matching openxlsx(::IOBuffer)

```

It’s possible I shouldn’t be reading the zipfile in as a `String`. But it’s extra strange to me that I can see in the XLSX.jl source code [where the method is defined for `openxlsx(::IO; kwargs)`,](https://github.com/felipenoris/XLSX.jl/blob/7efae16bb58ea00c02930583a9d92d4b6ed03d49/src/read.jl#L164) so why the error on arg type?

The code above works when I’m trying to open a CSV in a zip archive (just replacing the last line with `CSV.read`.

---

<div class="post-metadata">

**Author:** ![julien\_goo](https://avatars.discourse-cdn.com/v4/letter/j/ce73a5/32.png) [@julien\_goo](https://discourse.julialang.org/u/julien_goo)\
**Post date:** [March 7, 2023, 1:23pm UTC](https://discourse.julialang.org/t/open-xlsx-from-zip-archive-in-memory/95664/2 "2023-03-07T13:23:26Z")

</div>

Hi,

XLSX.openxlsx expects a filepath (as string) and cannot not handle IOBuffer. You have to convert you stream to a temp file to make it work with XLSX.readtable.

This code works :

```julia
using DataFrames
using HTTP
using XLSX
using ZipFile

url = "https://www.cms.gov/files/zip/2022-end-year-zip-code-file.zip"
zipped_bytes = HTTP.get(url)
zip = ZipFile.Reader(IOBuffer(zipped_bytes.body))
file_index = findfirst((f) -> f.name == "ZIP5_DEC2022_FINAL.xlsx", zip.files)
file_contents = read(zip.files[file_index], String)

# Open safe temp file
df = mktemp() do path, io
    # Store your content in the temp file
    write(io, file_contents)
    # read it (the first sheet, here unnamed)
    return DataFrame(XLSX.readtable(path, 1))
end

show(df)

```

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [March 7, 2023, 2:27pm UTC](https://discourse.julialang.org/t/open-xlsx-from-zip-archive-in-memory/95664/3 "2023-03-07T14:27:32Z")

</div>

Awesome! Thank you!

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [March 7, 2023, 2:51pm UTC](https://discourse.julialang.org/t/open-xlsx-from-zip-archive-in-memory/95664/4 "2023-03-07T14:51:14Z")

</div>

to extract in format useful to dataframe constructor I had to use this expression.

```julia
df = mktemp() do path, io
    # Store your content in the temp file
    write(io, file_contents)
    # read it (the first sheet, here unnamed)
    return XLSX.readtable(path, 1)
end
DataFrame(;Base.zip(df[2],df[1])...)

```

I wanted to know how to extract text files.

1. Can any function of CSV be used?  
Which and which parameters to use?

2. is it possible with readdlm? and how?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 7, 2023, 3:16pm UTC](https://discourse.julialang.org/t/open-xlsx-from-zip-archive-in-memory/95664/5 "2023-03-07T15:16:30Z")

</div>

Looks like you might be on an old version of `XLSX`, `readtable` used to return column headers and a data matrix separately but is returning a `Tables.table` compatible object since 0.8
