mirror of
https://github.com/riwiwa/muzi.git
synced 2025-12-30 12:45:26 -08:00
ported over extract function
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
build
|
||||||
|
lastfm-data
|
||||||
|
spotify-data
|
||||||
55
muzi.go
Normal file
55
muzi.go
Normal file
@@ -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/")
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user