From 37a14f2c135076e6261c13c1f1ad3cb5ac8dba65 Mon Sep 17 00:00:00 2001 From: riwiwa Date: Sun, 30 Nov 2025 01:27:21 -0800 Subject: [PATCH] ported over extract function --- .gitignore | 3 +++ go.mod | 3 +++ muzi.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 muzi.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aba9333 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +build +lastfm-data +spotify-data diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d523c9a --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module muzi + +go 1.25.4 diff --git a/muzi.go b/muzi.go new file mode 100644 index 0000000..a5d10e5 --- /dev/null +++ b/muzi.go @@ -0,0 +1,55 @@ +package main + +import "archive/zip" +import "path/filepath" +import "fmt" +import "strings" +import "os" +import "io" + +func extract(path string, target string) { + archive, err := zip.OpenReader(path) + if (err != nil) { + panic(err) + } + defer archive.Close() + + zipDir := filepath.Base(path) + zipDir = zipDir[:(strings.LastIndex(zipDir, "."))] + target = filepath.Join(target, zipDir) + + for _, f := range archive.File { + filePath := filepath.Join(target, f.Name) + fmt.Println("extracting:", filePath) + + if !strings.HasPrefix(filePath, filepath.Clean(target) + string(os.PathSeparator)) { + fmt.Println("Invalid file path") + return + } + if f.FileInfo().IsDir() { + fmt.Println("Creating Directory") + os.MkdirAll(filePath, os.ModePerm) + continue + } + if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil { + panic(err) + } + fileToExtract, err := os.OpenFile(filePath, os.O_WRONLY | os.O_CREATE | os.O_TRUNC, f.Mode()) + if err != nil { + panic(err) + } + extractedFile, err := f.Open() + if err != nil { + panic(err) + } + if _, err := io.Copy(fileToExtract, extractedFile); err != nil { + panic(err) + } + fileToExtract.Close() + extractedFile.Close() + } +} + +func main() { + extract("./spotify-data/zip/rileyman35@gmail.com-my_spotify_data.zip", "./spotify-data/extracted/") +}