Skip to content

Instantly share code, notes, and snippets.

@42LM
Created September 20, 2025 07:33
Show Gist options
  • Select an option

  • Save 42LM/2c4fd2a6b8819d840094cf4676569c61 to your computer and use it in GitHub Desktop.

Select an option

Save 42LM/2c4fd2a6b8819d840094cf4676569c61 to your computer and use it in GitHub Desktop.
unzip dartagnan template
package main
import (
"archive/zip"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
)
func main() {
dst := "output"
archive, err := zip.OpenReader("archive.zip")
if err != nil {
panic(err)
}
defer archive.Close()
var unzippedFileBytes []byte
// Read all the files from zip archive
for _, zipFile := range archive.File {
fmt.Println("Reading file:", zipFile.Name)
unzippedFileBytes, err = readZipFile(zipFile)
if err != nil {
log.Println(err)
continue
}
filePath := filepath.Join(dst, zipFile.Name)
// check for desired files
switch {
case zipFile.Name == "mail.html", strings.Contains(zipFile.Name, "images/"):
if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
panic(err)
}
dstFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, zipFile.Mode())
if err != nil {
panic(err)
}
fileInArchive, err := zipFile.Open()
if err != nil {
panic(err)
}
if _, err := io.Copy(dstFile, fileInArchive); err != nil {
panic(err)
}
}
_ = unzippedFileBytes // this is unzipped file bytes
}
// return slice or map with KEYS
// fmt.Println(string(unzippedFileBytes))
}
func readZipFile(zf *zip.File) ([]byte, error) {
f, err := zf.Open()
if err != nil {
return nil, err
}
defer f.Close()
return io.ReadAll(f)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment