This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
/
unzip.go
146 lines (125 loc) · 3.75 KB
/
unzip.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package util
import (
"archive/zip"
"fmt"
"io"
"os"
"path/filepath"
)
// UnzipOver safely unzips a file and copies it contents to a destination path.
// If destination path exists, it will be removed first.
// The filename must have a ".zip" extension.
// You can specify a check function, which will run before moving the unzipped
// directory into place.
// If you specify a tmpDir and destination path exists, it will be moved there
// instead of being removed.
//
// To unzip Keybase-1.2.3.zip and move the contents Keybase.app to /Applications/Keybase.app
//
// UnzipOver("/tmp/Keybase-1.2.3.zip", "Keybase.app", "/Applications/Keybase.app", check, "", log)
func UnzipOver(sourcePath string, path string, destinationPath string, check func(sourcePath, destinationPath string) error, tmpDir string, log Log) error {
unzipPath := fmt.Sprintf("%s.unzipped", sourcePath)
defer RemoveFileAtPath(unzipPath)
err := unzipOver(sourcePath, unzipPath, log)
if err != nil {
return err
}
contentPath := filepath.Join(unzipPath, path)
err = check(contentPath, destinationPath)
if err != nil {
return err
}
return MoveFile(contentPath, destinationPath, tmpDir, log)
}
// UnzipPath unzips and returns path to unzipped directory
func UnzipPath(sourcePath string, log Log) (string, error) {
unzipPath := fmt.Sprintf("%s.unzipped", sourcePath)
err := unzipOver(sourcePath, unzipPath, log)
if err != nil {
return "", err
}
return unzipPath, nil
}
func unzipOver(sourcePath string, destinationPath string, log Log) error {
if destinationPath == "" {
return fmt.Errorf("Invalid destination %q", destinationPath)
}
if _, ferr := os.Stat(destinationPath); ferr == nil {
log.Infof("Removing existing unzip destination path: %s", destinationPath)
err := os.RemoveAll(destinationPath)
if err != nil {
return err
}
}
log.Infof("Unzipping %q to %q", sourcePath, destinationPath)
return Unzip(sourcePath, destinationPath, log)
}
// Unzip unpacks a zip file to a destination.
// This unpacks files using the current user and time (it doesn't preserve).
// This code was modified from https://stackoverflow.com/questions/20357223/easy-way-to-unzip-file-with-golang/20357902
func Unzip(sourcePath, destinationPath string, log Log) error {
r, err := zip.OpenReader(sourcePath)
if err != nil {
return err
}
defer func() {
if closeErr := r.Close(); closeErr != nil {
log.Warningf("Error in unzip closing zip file: %s", closeErr)
}
}()
err = os.MkdirAll(destinationPath, 0755)
if err != nil {
return err
}
// Closure to address file descriptors issue with all the deferred .Close() methods
extractAndWriteFile := func(f *zip.File) error {
rc, err := f.Open()
if err != nil {
return err
}
defer func() {
if err := rc.Close(); err != nil {
log.Warningf("Error in unzip closing file: %s", err)
}
}()
filePath := filepath.Join(destinationPath, f.Name)
fileInfo := f.FileInfo()
if fileInfo.IsDir() {
err := os.MkdirAll(filePath, fileInfo.Mode())
if err != nil {
return err
}
} else {
err := os.MkdirAll(filepath.Dir(filePath), 0755)
if err != nil {
return err
}
if fileInfo.Mode()&os.ModeSymlink != 0 {
linkName, readErr := io.ReadAll(rc)
if readErr != nil {
return readErr
}
return os.Symlink(string(linkName), filePath)
}
fileCopy, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fileInfo.Mode())
if err != nil {
return err
}
defer Close(fileCopy)
_, err = io.Copy(fileCopy, rc)
if err != nil {
return err
}
}
return nil
}
for _, f := range r.File {
err := extractAndWriteFile(f)
if err != nil {
return err
}
}
return nil
}