mirror of
https://gitee.com/johng/gf
synced 2026-07-07 06:15:15 +08:00
add Name function for gfile; add ZipPath/UnZipFile functions for gcompress
This commit is contained in:
120
g/encoding/gcompress/gcompress_file.go
Normal file
120
g/encoding/gcompress/gcompress_file.go
Normal file
@ -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 <path> to <dest> 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 <archive> to <dest> 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
|
||||
}
|
||||
@ -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.
|
||||
|
||||
BIN
geg/encoding/gcompress/data.zip
Normal file
BIN
geg/encoding/gcompress/data.zip
Normal file
Binary file not shown.
14
geg/encoding/gcompress/unzip.go
Normal file
14
geg/encoding/gcompress/unzip.go
Normal file
@ -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)
|
||||
}
|
||||
15
geg/encoding/gcompress/zip.go
Normal file
15
geg/encoding/gcompress/zip.go
Normal file
@ -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)
|
||||
}
|
||||
@ -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"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user