Created
June 14, 2022 09:31
-
-
Save alexmacniven/b65ed56a548e56e499c7d2b96486aab9 to your computer and use it in GitHub Desktop.
Extract a target from a zip archive with Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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