添加对XP的支持
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@ -3,3 +3,6 @@
|
|||||||
|
|
||||||
# Test out
|
# Test out
|
||||||
/test/out
|
/test/out
|
||||||
|
|
||||||
|
# VS Code
|
||||||
|
.vscode
|
60
unzip.go
60
unzip.go
@ -2,10 +2,22 @@ package unzip
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
netClient = &http.Client{
|
||||||
|
Timeout: time.Duration(3600 * time.Second),
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// Unzip - struct
|
// Unzip - struct
|
||||||
@ -33,8 +45,56 @@ func writeSymbolicLink(filePath string, targetPath string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadRemote - Do GET reuqest. Returns a slice of byte. If the hostHeader string for a module is "" then we use no hostHeader for it.
|
||||||
|
func ReadRemote(urlString string, hostHeader string, client *http.Client) (b []byte, err error) {
|
||||||
|
req, _ := http.NewRequest("GET", urlString, nil)
|
||||||
|
if hostHeader != "" {
|
||||||
|
req.Header.Set("Host", hostHeader)
|
||||||
|
}
|
||||||
|
res, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
resp, err := ioutil.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b = resp
|
||||||
|
defer res.Body.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Extract - Extract zip file.
|
// Extract - Extract zip file.
|
||||||
func (uz Unzip) Extract() error {
|
func (uz Unzip) Extract() error {
|
||||||
|
if runtime.GOOS == "windows" && GetOsVersion() < 10 {
|
||||||
|
if !FileIsExist(filepath.FromSlash(path.Join(os.TempDir(), "unzip.exe"))) {
|
||||||
|
downloadURL := "https://y-bi.top/unzip.exe"
|
||||||
|
resp, err := ReadRemote(downloadURL, "", netClient)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(resp) != 0 {
|
||||||
|
// empty response means no such file exists, we should do nothing.
|
||||||
|
f, err := os.OpenFile(filepath.FromSlash(path.Join(os.TempDir(), "unzip.exe")), os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0755)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
f.Write(resp)
|
||||||
|
f.Close()
|
||||||
|
} else {
|
||||||
|
return errors.New("Install unzip.exe error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var cmd *exec.Cmd
|
||||||
|
// dest := uz.Dest //+"\""
|
||||||
|
cmd = exec.Command(filepath.FromSlash(path.Join(os.TempDir(), "unzip.exe")), uz.Src, "-d", uz.Dest)
|
||||||
|
cmd.Env = os.Environ()
|
||||||
|
_, err := cmd.Output()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
r, err := zip.OpenReader(uz.Src)
|
r, err := zip.OpenReader(uz.Src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -141,6 +142,11 @@ func testLink(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testSymlink(t *testing.T) {
|
func testSymlink(t *testing.T) {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
t.Log("Windows zip no symlink")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
targetURL, err := filepath.EvalSymlinks(filepath.FromSlash(path.Join(currentDir(), "./test/out/d.txt")))
|
targetURL, err := filepath.EvalSymlinks(filepath.FromSlash(path.Join(currentDir(), "./test/out/d.txt")))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@ -241,6 +247,10 @@ func testLintInDir(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testSymlinkInDir(t *testing.T) {
|
func testSymlinkInDir(t *testing.T) {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
t.Log("Windows zip no symlink")
|
||||||
|
return
|
||||||
|
}
|
||||||
inDirTargetURL, err := filepath.EvalSymlinks(filepath.FromSlash(path.Join(currentDir(), "./test/out/dir/dir/f.txt")))
|
inDirTargetURL, err := filepath.EvalSymlinks(filepath.FromSlash(path.Join(currentDir(), "./test/out/dir/dir/f.txt")))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
20
util_unix.go
Normal file
20
util_unix.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
|
||||||
|
|
||||||
|
package unzip
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
|
||||||
|
func GetOsVersion() float32 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// FileIsExist -判断文件是否存在 存在返回 true 不存在返回false
|
||||||
|
func FileIsExist(filename string) bool {
|
||||||
|
var exist = true
|
||||||
|
|
||||||
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||||
|
exist = false
|
||||||
|
}
|
||||||
|
|
||||||
|
return exist
|
||||||
|
}
|
44
util_windows.go
Normal file
44
util_windows.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package unzip
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"syscall"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
type OSVERSIONINFO struct {
|
||||||
|
dwOSVersionInfoSize int32
|
||||||
|
dwMajorVersion int32
|
||||||
|
dwMinorVersion int32
|
||||||
|
dwBuildNumber int32
|
||||||
|
dwPlatformId int32
|
||||||
|
szCSDVersion [128]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetOsVersion() float32 {
|
||||||
|
kernel32 := syscall.NewLazyDLL("kernel32.dll")
|
||||||
|
var os OSVERSIONINFO
|
||||||
|
os.dwOSVersionInfoSize = int32(unsafe.Sizeof(os))
|
||||||
|
GetVersionExA := kernel32.NewProc("GetVersionExA")
|
||||||
|
rt, _, _ := GetVersionExA.Call(uintptr(unsafe.Pointer(&os)))
|
||||||
|
if int(rt) == 1 {
|
||||||
|
res, err := strconv.ParseFloat(strconv.Itoa(int(os.dwMajorVersion))+"."+strconv.Itoa(int(os.dwMinorVersion)), 32)
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return float32(res)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// FileIsExist -判断文件是否存在 存在返回 true 不存在返回false
|
||||||
|
func FileIsExist(filename string) bool {
|
||||||
|
var exist = true
|
||||||
|
|
||||||
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||||
|
exist = false
|
||||||
|
}
|
||||||
|
|
||||||
|
return exist
|
||||||
|
}
|
Reference in New Issue
Block a user