diff --git a/g/encoding/gcompress/gcompress_file.go b/g/encoding/gcompress/gcompress_file.go new file mode 100644 index 000000000..236c8b8a3 --- /dev/null +++ b/g/encoding/gcompress/gcompress_file.go @@ -0,0 +1,120 @@ +// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gcompress + +import ( + "archive/zip" + "github.com/gogf/gf/g/os/gfile" + "io" + "os" + "path/filepath" +) + +// Zip compresses to using zip compressing algorithm. +func ZipPath(path, dest string, prefix ...string) error { + d, err := os.Create(dest) + if err != nil { + return err + } + defer d.Close() + w := zip.NewWriter(d) + defer w.Close() + files, err := gfile.ScanDir(path, "*.*", true) + if err != nil { + return err + } + pathRealPath := gfile.RealPath(path) + destRealPath := gfile.RealPath(dest) + headerPrefix := "" + if len(prefix) > 0 { + headerPrefix = prefix[0] + } + for _, file := range files { + if destRealPath == file { + continue + } + err := zipFile(file, headerPrefix+gfile.Dir(file[len(pathRealPath):]), w) + if err != nil { + return err + } + } + return nil +} + +// UnZipFile decompresses to using zip compressing algorithm. +func UnZipFile(archive, dest string) error { + reader, err := zip.OpenReader(archive) + if err != nil { + return err + } + if err := os.MkdirAll(dest, 0755); err != nil { + return err + } + for _, file := range reader.File { + path := filepath.Join(dest, file.Name) + if file.FileInfo().IsDir() { + os.MkdirAll(path, file.Mode()) + continue + } + dir := filepath.Dir(path) + if len(dir) > 0 { + if _, err = os.Stat(dir); os.IsNotExist(err) { + err = os.MkdirAll(dir, 0755) + if err != nil { + return err + } + } + } + fileReader, err := file.Open() + if err != nil { + return err + } + defer fileReader.Close() + + targetFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode()) + if err != nil { + return err + } + defer targetFile.Close() + + if _, err := io.Copy(targetFile, fileReader); err != nil { + return err + } + } + return nil +} + +func zipFile(path string, prefix string, zw *zip.Writer) error { + file, err := os.Open(path) + if err != nil { + return nil + } + defer file.Close() + info, err := file.Stat() + if err != nil { + return err + } + header, err := zip.FileInfoHeader(info) + if err != nil { + return err + } + if len(prefix) > 0 { + header.Name = prefix + "/" + header.Name + } else { + header.Name = header.Name + } + + writer, err := zw.CreateHeader(header) + if err != nil { + return err + } + if _, err = io.Copy(writer, file); err != nil { + return err + } + + return nil +} diff --git a/g/os/gfile/gfile.go b/g/os/gfile/gfile.go index d4fc60b54..62a91b012 100644 --- a/g/os/gfile/gfile.go +++ b/g/os/gfile/gfile.go @@ -415,11 +415,20 @@ func SelfDir() string { // Basename returns the last element of path. // Trailing path separators are removed before extracting the last element. // If the path is empty, Base returns ".". -// If the path consists entirely of separators, Base returns a single separator. +// If the path consists entirely of separators, Basename returns a single separator. func Basename(path string) string { return filepath.Base(path) } +// Name returns the last element of path without extension. +func Name(path string) string { + base := filepath.Base(path) + if i := strings.LastIndexByte(base, '.'); i != -1 { + return base[:i] + } + return base +} + // Dir returns all but the last element of path, typically the path's directory. // After dropping the final element, Dir calls Clean on the path and trailing // slashes are removed. diff --git a/geg/encoding/gcompress/data.zip b/geg/encoding/gcompress/data.zip new file mode 100644 index 000000000..5da348056 Binary files /dev/null and b/geg/encoding/gcompress/data.zip differ diff --git a/geg/encoding/gcompress/unzip.go b/geg/encoding/gcompress/unzip.go new file mode 100644 index 000000000..e88e7bebd --- /dev/null +++ b/geg/encoding/gcompress/unzip.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + "github.com/gogf/gf/g/encoding/gcompress" +) + +func main() { + err := gcompress.UnZipFile( + `D:\Workspace\Go\GOPATH\src\github.com\gogf\gf\geg\encoding\gcompress\data.zip`, + `D:\Workspace\Go\GOPATH\src\github.com\gogf\gf\geg`, + ) + fmt.Println(err) +} diff --git a/geg/encoding/gcompress/zip.go b/geg/encoding/gcompress/zip.go new file mode 100644 index 000000000..48e35fec1 --- /dev/null +++ b/geg/encoding/gcompress/zip.go @@ -0,0 +1,15 @@ +package main + +import ( + "fmt" + "github.com/gogf/gf/g/encoding/gcompress" +) + +func main() { + err := gcompress.ZipPath( + `D:\Workspace\Go\GOPATH\src\github.com\gogf\gf\geg`, + `D:\Workspace\Go\GOPATH\src\github.com\gogf\gf\geg\encoding\gcompress\data.zip`, + "my-dir", + ) + fmt.Println(err) +} diff --git a/geg/other/test.go b/geg/other/test.go index f71bc27b8..75516c525 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -2,9 +2,9 @@ package main import ( "fmt" - "github.com/gogf/gf/g" + "github.com/gogf/gf/g/os/gfile" ) func main() { - fmt.Println(g.Config().Get("log-path")) + fmt.Println(gfile.Basename("main.go")) }