Skip to content

Instantly share code, notes, and snippets.

@d4l3k
Created July 7, 2020 03:10
Show Gist options
  • Save d4l3k/e4682bea29034bb95792eed540dc3e6a to your computer and use it in GitHub Desktop.
Save d4l3k/e4682bea29034bb95792eed540dc3e6a to your computer and use it in GitHub Desktop.
package main
import (
"archive/zip"
"io"
"log"
"os"
"path/filepath"
"strings"
"facette.io/natsort"
)
func main() {
if err := run(); err != nil {
log.Fatalf("%+v", err)
}
}
func run() error {
f, err := zip.OpenReader("hw3.0_emmc.zip")
if err != nil {
return err
}
defer f.Close()
files := map[string]*zip.File{}
var fileNames []string
for _, file := range f.File {
if strings.HasSuffix(file.Name, "/") {
// directory
continue
}
files[file.Name] = file
fileNames = append(fileNames, file.Name)
}
natsort.Sort(fileNames)
log.Println("files", fileNames)
output := filepath.Base(fileNames[0])
log.Printf("writing to %s", output)
out, err := os.OpenFile(output, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer out.Close()
for _, fileName := range fileNames {
file := files[fileName]
log.Printf("reading %s", fileName)
data, err := file.Open()
if err != nil {
return err
}
defer data.Close()
if _, err := io.Copy(out, data); err != nil {
return err
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment