(SOLVED) Zlib Failed to Inflate Buffer -3

I’m testing out extracting ZIP files via zlib, but not having any luck. I’m using the following code to extract a ZIP file containing a test image, but it gives the error Failed to inflate buffer (-3) during zlib.inflate():

local zip = io.open("./test.zip", "rb")
local data = zip:read("a")
zip:close()

------------- This part's taken straight from the docs
local s = ""
for c in string.gmatch(data, ".") do
	s = s .. "\\" .. string.byte(c)
end
-------------

local png = io.open("./test.png", "wb")
png:write(zlib.inflate(s)) -- Error happens here
png:flush()
png:close()

Seems like error -3 just means that the given input isn’t the right format. I checked both the ZIP file I want to extract and a test file that’s the output of zlib.deflate(the png data), and it turns out they’re completely different formats! Neither Archive Utility nor The Unarchiver know how to open it, so I have no idea where to go from here.

Yes, zip is an archive format (like tar). It is used to archive and compress multiple files. The only widespread compression algorithm use by zip is Deflate. When you call zlib.deflate() you compress a single file using the Deflate algorithm. You are not creating a zip archive. When you call zlib.inflate() you decompress a single file, not a zip archive.

Long story short is that you need an extension to read and decompress a zip archive.

Yeah, that makes sense :pensive:.I was reading one of the few forum posts about this, and it gave me the impression that I was on the right track.

1 Like

But if you do need to iterate over the content of a zip file and unzip parts or all of it then I’m sure it’s not that hard to wrap an existing library into a native extension.

1 Like