leifg

leifg

Reading directly from a zipped stream

Hey there,

a while ago I implemented an Excel Parser Library. So far it’s working but it has the problem that it doesn’t scale with file size.

Background: The (2000) Excel format is just a bunch of compressed (zip) XML files. In order to get access to the underlying XML, I extract the whole archive in memory:

:zip.extract spreadsheet_filename, [:memory]

This of course is very bad in terms of memory usage. In other languages I have found examples where a compressed archive is directly read as a stream, filtered by filename and then processed.

Is there a way to do this in the Erlang/Elixir world?

Marked As Solved

effinbanjos

effinbanjos

A little follow-up many months later:

I finally had a little time to play with this and found that StreamGzip would die after a bit of processing. After casting about for other solutions, I found that the core File.stream! supports compressed files - you just need to feed it the right mode:

    File.stream!("./huge-file.gz", [:compressed])
    |> Stream.map(&IO.inspect(&1))
    |> Stream.run()

File.stream! also defaults to a line-by-line output mode instead of to a byte chunk output mode. I ended-up having to used chunk_while to re-order bytes chunks into line chunks with the StreamGzip library since I wanted to emit individual records into RabbitMQ.

HTH

Also Liked

bbense

bbense

Looking at the documentation for the :zip library , I think you can get what you want using the :zip.open interface. Open the handle with the :memory option, list all the files and then use :zip.get
to get each individual file.

You could wrap this with Stream.resource if you want to get all elixiry on it.

leifg

leifg

Thanks for the info. Played around with it but I’m not sure this is the way to go.

the :zip.zip_get function always blocks and returns the whole content of the referenced file. What I actually need is a function that returns chunks of the content so that I can process it right away.

Side Note: I peeked at the zip source code in OTP. It seems as if Erlang is reading the contents in chunks. So from research it seems to me that Erlang does not expose this API.

effinbanjos

effinbanjos

Did you ever find a solution for this issue? I have a use-case where I’d need to read from a very large compressed file (large for me, that is…22G compressed / 65G+ uncompressed).

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement