Skip to content

Instantly share code, notes, and snippets.

@alexmacniven
Created June 14, 2022 09:31
Show Gist options
  • Save alexmacniven/b65ed56a548e56e499c7d2b96486aab9 to your computer and use it in GitHub Desktop.
Save alexmacniven/b65ed56a548e56e499c7d2b96486aab9 to your computer and use it in GitHub Desktop.
Extract a target from a zip archive with Go
package main
import (
"archive/zip"
"io"
"os"
)
func main() {
a := "myArchive.zip"
t := "targetFile.txt"
z, err := zip.OpenReader(a)
if err != nil {
panic(err)
}
defer z.Close()
for _, i := range z.File {
if i.Name == t {
d, err := os.Create(i.Name)
if err != nil {
panic(err)
}
defer d.Close()
f, err := i.Open()
if err != nil {
panic(err)
}
defer f.Close()
if _, err := io.Copy(d, f); err != nil {
panic(err)
}
break
}
// Remove condition and break to extract all
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment