Skip to content

Instantly share code, notes, and snippets.

@Hamcha
Created February 8, 2020 12:37
Show Gist options
  • Save Hamcha/2aab7ec30050297f48bce4c91e7deb26 to your computer and use it in GitHub Desktop.
Save Hamcha/2aab7ec30050297f48bce4c91e7deb26 to your computer and use it in GitHub Desktop.
Compress a folder into a hopefully working character pack for SRB2kart
package main
import (
"archive/zip"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
func main() {
inpath := flag.String("d", "", "Input folder")
outpath := flag.String("f", "", "Output file")
flag.Parse()
file, err := os.Create(*outpath)
checkErr(err)
w := zip.NewWriter(file)
allfiles := []string{}
skins := make(map[string]bool)
filepath.Walk(*inpath, func(path string, info os.FileInfo, err error) error {
checkErr(err)
relpath, err := filepath.Rel(*inpath, path)
checkErr(err)
zippath := strings.Replace(relpath, string(os.PathSeparator), "/", -1)
if strings.HasSuffix(zippath, "/S_SKIN") {
skins[zippath] = true
return nil
}
if zippath == "." || info.IsDir() {
return nil
}
allfiles = append(allfiles, zippath)
return nil
})
roots := make(map[string]bool)
for _, file := range allfiles {
dir := strings.Replace(filepath.Dir(file), string(os.PathSeparator), "/", -1) + "/"
// Did we ever get to this dir?
_, ok := roots[dir]
if !ok {
// Create dir
fmt.Printf(" - %s [DIR]\n", dir)
w.Create(dir)
roots[dir] = true
// Check for S_SKIN
sskin := dir + "S_SKIN"
if _, ok := skins[sskin]; ok {
fmt.Printf("[ SKIN \"%s\" ]\n", filepath.Base(dir))
fmt.Printf(" - %s\n", sskin)
checkErr(copyFile(w, filepath.Join(*inpath, sskin), sskin))
}
}
fmt.Printf(" - %s\n", file)
checkErr(copyFile(w, filepath.Join(*inpath, file), file))
}
checkErr(w.Close())
}
func copyFile(w *zip.Writer, path string, target string) error {
rc, err := os.Open(path)
if err != nil {
return err
}
fw, err := w.Create(target)
if err != nil {
return err
}
_, err = io.Copy(fw, rc)
if err != nil {
return err
}
return rc.Close()
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment