Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch zip file from the web and decompress it in Go

Tags:

zip

go

How can I fetch a zip archived file from the Web and decompress it in Go? It looks like archive/zip package provides a set of tools to parse the zipped file. However, in order to decompress the zipped file, I have to use zip.OpenReader, which takes the filename as string.

So how can I fetch the zipped file from the Web, and put it into the above function as string...? Or maybe do I have to first fetch the file and put it in one of the directories of my filesystem, and then read it?

like image 918
Blaszard Avatar asked Oct 14 '25 14:10

Blaszard


1 Answers

It appears that in order to decompress a zip, you need to be able to seek to arbitrary locations. This means that unless you want to do something fancy, it either needs to be a local file or be completely in memory.

Assuming you have downloaded the zip and have it in a []byte, you want to do something like:

zipReader := zip.NewReader(bytes.NewReader(zipData), len(zipData))
like image 198
Stephen Weinberg Avatar answered Oct 17 '25 10:10

Stephen Weinberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!