gf/os/gres/gres_func.go

101 lines
2.7 KiB
Go
Raw Normal View History

2019-08-13 13:45:01 +08:00
// Copyright 2019 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 gres
import (
"archive/zip"
"bytes"
"fmt"
"github.com/gogf/gf/encoding/gcompress"
"github.com/gogf/gf/internal/utilbytes"
"github.com/gogf/gf/os/gfile"
)
2019-08-30 20:29:12 +08:00
const (
gPACKAGE_TEMPLATE = `package %s
import "github.com/gogf/gf/os/gres"
func init() {
if err := gres.Add(%s); err != nil {
panic(err)
}
}
`
)
// Pack packs the path specified by <srcPaths> into bytes.
2019-08-13 21:06:11 +08:00
// The unnecessary parameter <keyPrefix> indicates the prefix for each file
// packed into the result bytes.
//
// Note that parameter <srcPaths> supports multiple paths join with ','.
func Pack(srcPaths string, keyPrefix ...string) ([]byte, error) {
2019-08-13 13:45:01 +08:00
buffer := bytes.NewBuffer(nil)
headerPrefix := ""
if len(keyPrefix) > 0 && keyPrefix[0] != "" {
headerPrefix = keyPrefix[0]
}
err := gcompress.ZipPathWriter(srcPaths, buffer, headerPrefix)
2019-08-13 13:45:01 +08:00
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
// PackToFile packs the path specified by <srcPaths> to target file <dstPath>.
2019-08-13 21:06:11 +08:00
// The unnecessary parameter <keyPrefix> indicates the prefix for each file
// packed into the result bytes.
//
// Note that parameter <srcPaths> supports multiple paths join with ','.
func PackToFile(srcPaths, dstPath string, keyPrefix ...string) error {
data, err := Pack(srcPaths, keyPrefix...)
2019-08-13 13:45:01 +08:00
if err != nil {
return err
}
return gfile.PutBytes(dstPath, data)
}
// PackToGoFile packs the path specified by <srcPaths> to target go file <goFilePath>
2019-08-13 21:06:11 +08:00
// with given package name <pkgName>.
//
// The unnecessary parameter <keyPrefix> indicates the prefix for each file
// packed into the result bytes.
//
// Note that parameter <srcPaths> supports multiple paths join with ','.
2019-08-13 13:45:01 +08:00
func PackToGoFile(srcPath, goFilePath, pkgName string, keyPrefix ...string) error {
data, err := Pack(srcPath, keyPrefix...)
if err != nil {
return err
}
return gfile.PutContents(
goFilePath, fmt.Sprintf(gPACKAGE_TEMPLATE, pkgName, utilbytes.Export(data)),
)
}
2019-08-13 21:06:11 +08:00
// Unpack unpacks the content specified by <path> to []*File.
2019-08-13 13:45:01 +08:00
func Unpack(path string) ([]*File, error) {
realPath, err := gfile.Search(path)
if err != nil {
return nil, err
}
return UnpackContent(gfile.GetBytes(realPath))
}
2019-08-13 21:06:11 +08:00
// UnpackContent unpacks the content to []*File.
2019-08-13 13:45:01 +08:00
func UnpackContent(content []byte) ([]*File, error) {
reader, err := zip.NewReader(bytes.NewReader(content), int64(len(content)))
if err != nil {
return nil, err
}
array := make([]*File, len(reader.File))
for i, file := range reader.File {
2019-08-14 22:03:52 +08:00
array[i] = &File{file: file}
2019-08-13 13:45:01 +08:00
}
return array, nil
}