Created
July 7, 2020 03:10
-
-
Save d4l3k/e4682bea29034bb95792eed540dc3e6a to your computer and use it in GitHub Desktop.
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" | |
"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