-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourcePackageWriter.go
More file actions
44 lines (35 loc) · 1.03 KB
/
ResourcePackageWriter.go
File metadata and controls
44 lines (35 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"io"
"mime/multipart"
"os"
"os/exec"
"path/filepath"
)
type ResourcePackageWriter struct {
InputStream *multipart.File
ArchivePath string
}
func NewResourcePackageWriter(file *multipart.File, archivePath string) *ResourcePackageWriter {
// Convert to absolute path (required for the tar command to work correctly)
archivePath, _ = filepath.Abs(archivePath)
return &ResourcePackageWriter{InputStream: file, ArchivePath: archivePath}
}
func (writer *ResourcePackageWriter) Write() error {
outFile, err := os.OpenFile(writer.ArchivePath, os.O_WRONLY|os.O_CREATE, 0664)
if err != nil {
return err
}
defer outFile.Close()
// Write to outFile
io.Copy(outFile, *writer.InputStream)
return nil
}
func (writer *ResourcePackageWriter) Extract(targetDir string) error {
// Call Unix tar (golang is able to untar/gunzip out-of-the-box, but this requires a lot more lines of code)
cmd := exec.Command("tar", "xfz", writer.ArchivePath)
// Set working dir to target dir
cmd.Dir = targetDir
// Go!
return cmd.Run()
}