Just Another Tech Journal

Projects

Elsewhere

Ruby - Reading A Remote Zip File

01 OCTOBER 2013

I need to access a remote zip file and this is something that works:

# In the console, gem install "httparty"require"httparty"# In the console, gem install "rubyzip"require"zip"# Get the contents of the remote zip file via HTTParty# and write it into a temp zip filezipfile=Tempfile.new("file")zipfile.binmode# This might not be necessary depending on the zip filezipfile.write(HTTParty.get("http://localhost:3000/file.zip").body)zipfile.close# Unzip the temp zip file and process the contents# Let garbage collection delete the temp zip fileZip::File.open(zipfile.path)do|file|file.eachdo|content|data=file.read(content)putsdata# Do whatever you want with the contentsendend

The code is simple, but at the start, I kept getting an error when unzipping the temp zip file,
and I thought I was doing something wrong.

End-of-central-directory signature not found

Did some debugging and finally figured out that the problem was with the remote zip file
- because the file was not fully constructed even though I had a link to it.

The remote zip file link was actually returned by an earlier API call to an external service
that also triggered the building of the remote zip file.