You read the buffer before you've closed the zip.Writer, so any final data is needs to flush to the buffer has gotten lost. You should remove the
defer archive.Close() and instead close the archive before getting the bytes out of the buffer. e.g.
err = archive.Close() return buf.Bytes(), err
It works in your file case because you defer closing the file. The defer's will be applied last in first out, so the archive gets closed before the file is closed.