Search code examples
gozip

I am new to golang, I am trying to create a zip that must have a folder inside that is: tosend.zip/archivos/file.png


newZip, err := os.Create("./temp/tosend.zip")
if err != nil {
    return "", err
}
defer newZip.Close()

zipWriter := zip.NewWriter(newZip)
_, err = zipWriter.Create("archivos/")
if err != nil {
    return "", err
}
defer zipWriter.Close()

Solution

  • You are almost there. A file name can contain directories. Use this code:

    zipWriter := zip.NewWriter(newZip)
    w, err := zipWriter.Create("archivos/file.png") // <-- use full path here
    if err != nil {
        return "", err
    }
    w.Write(pngData)
    

    Run it on the playground.