Last active
October 28, 2021 06:25
-
-
Save imjasonh/8322584 to your computer and use it in GitHub Desktop.
Go script to strip files matching a suffix from a ZIP file. I used this to strip .mp3 voicemails from my Google Voice takeout data dump, for example. "go run stripzip.go -file username@gmail.com-20140108T184123Z-Voice.zip -exclude .mp3"
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" | |
"flag" | |
"io" | |
"log" | |
"os" | |
"strings" | |
) | |
var file = flag.String("file", "", "ZIP file to strip") | |
var exclude = flag.String("exclude", "", "File extension to remove") | |
func main() { | |
flag.Parse() | |
r, err := zip.OpenReader(*file) | |
if err != nil { | |
log.Fatal("open:", err) | |
} | |
outfile, err := os.Create(*file + "-stripped.zip") | |
if err != nil { | |
log.Fatal("create:", err) | |
} | |
defer outfile.Close() | |
out := zip.NewWriter(outfile) | |
defer out.Close() | |
for _, f := range r.File { | |
n := f.FileHeader.Name | |
if strings.HasSuffix(n, *exclude) { | |
continue | |
} | |
w, err := out.CreateHeader(&f.FileHeader) | |
if err != nil { | |
log.Fatal("create %s: %v", n, err) | |
} | |
r, err := f.Open() | |
if err != nil { | |
log.Fatal("open %s: %v", n, err) | |
} | |
defer r.Close() | |
if _, err := io.Copy(w, r); err != nil { | |
log.Fatal("copy %s: %v", n, err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment