1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-27 09:55:22 +00:00

Able to zip folder when building

This commit is contained in:
V2Ray 2015-10-18 11:42:00 +02:00
parent 0de60adf3c
commit 74cba8b177
2 changed files with 79 additions and 1 deletions

62
tools/build/archive.go Normal file
View File

@ -0,0 +1,62 @@
package main
import (
"archive/zip"
"io"
"os"
"path/filepath"
)
type ZipWorker struct {
zipWriter *zip.Writer
root string
}
func NewZipWorker(zipFile io.Writer, root string) *ZipWorker {
return &ZipWorker{
zipWriter: zip.NewWriter(zipFile),
root: root,
}
}
func (worker *ZipWorker) run() error {
defer worker.close()
return filepath.Walk(worker.root, worker.zipAllFiles)
}
func (worker *ZipWorker) zipAllFiles(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
fileWriter, err := worker.zipWriter.Create(path)
if err != nil {
return err
}
fileReader, err := os.Open(path)
if err != nil {
return err
}
_, err = io.Copy(fileWriter, fileReader)
if err != nil {
return err
}
return nil
}
func (worker *ZipWorker) close() {
worker.zipWriter.Close()
}
func zipFolder(folder string, file string) error {
if _, err := os.Stat(file); err == nil {
os.Remove(file)
}
zipFile, err := os.Create(file)
if err != nil {
return err
}
defer zipFile.Close()
return NewZipWorker(zipFile, folder).run()
}

View File

@ -23,7 +23,7 @@ func createTargetDirectory(version string, goOS GoOS, goArch GoArch) (string, er
}
GOPATH := os.Getenv("GOPATH")
targetDir := filepath.Join(GOPATH, "bin", "v2ray"+suffix)
targetDir := filepath.Join(GOPATH, "bin", "v2ray-"+version+suffix)
if version != "custom" {
os.RemoveAll(targetDir)
}
@ -72,4 +72,20 @@ func main() {
if err != nil {
fmt.Println("Unable to copy config files: " + err.Error())
}
if *archive {
GOPATH := os.Getenv("GOPATH")
binPath := filepath.Join(GOPATH, "bin")
err := os.Chdir(binPath)
if err != nil {
fmt.Printf("Unable to switch to directory (%s): %v\n", binPath, err)
}
suffix := getSuffix(v2rayOS, v2rayArch)
zipFile := "v2ray" + suffix + ".zip"
root := filepath.Base(targetDir)
err = zipFolder(root, zipFile)
if err != nil {
fmt.Println("Unable to create archive (%s): %v\n", zipFile, err)
}
}
}