From 4fc391895b18b78e285ef614fd35589ee95f0392 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 26 Apr 2023 11:12:48 +0800 Subject: [PATCH] feat: add new function for fileutil package --- README.md | 5 + README_zh-CN.md | 5 + docs/fileutil.md | 168 +++++++++++++++++++++++++++++++++- docs/fileutil_zh-CN.md | 166 +++++++++++++++++++++++++++++++++ fileutil/file.go | 89 ++++++++++++++++++ fileutil/file_test.go | 51 +++++++++++ fileutil/testdata/file.go.zip | Bin 0 -> 2523 bytes fileutil/testdata/test.csv | 3 + fileutil/testdata/test.txt | 1 + 9 files changed, 487 insertions(+), 1 deletion(-) create mode 100644 fileutil/testdata/file.go.zip create mode 100644 fileutil/testdata/test.csv create mode 100644 fileutil/testdata/test.txt diff --git a/README.md b/README.md index c8683fd..5ae5691 100644 --- a/README.md +++ b/README.md @@ -190,6 +190,11 @@ import "github.com/duke-git/lancet/fileutil" - [Zip](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#Zip) - [UnZip](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#UnZip) - [CurrentPath](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#CurrentPath) +- [IsZipFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#IsZipFile) +- [FileSize](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#FileSize) +- [MTime](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#MTime) +- [Sha](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#Sha) +- [ReadCsvFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil.md#ReadCsvFile) ### 5. Formatter contains some functions for data formatting. diff --git a/README_zh-CN.md b/README_zh-CN.md index 040af17..d0717ac 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -189,6 +189,11 @@ import "github.com/duke-git/lancet/fileutil" - [Zip](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#Zip) - [UnZip](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#UnZip) - [CurrentPath](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#CurrentPath) +- [IsZipFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#IsZipFile) +- [FileSize](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#FileSize) +- [MTime](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#MTime) +- [Sha](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#Sha) +- [ReadCsvFile](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#ReadCsvFile) ### 5. formatter 格式化器包含一些数据格式化处理方法。 diff --git a/docs/fileutil.md b/docs/fileutil.md index ec0a12f..ff31605 100644 --- a/docs/fileutil.md +++ b/docs/fileutil.md @@ -38,6 +38,11 @@ import ( - [Zip](#Zip) - [UnZip](#UnZip) - [CurrentPath](#CurrentPath) +- [IsZipFile](#IsZipFile) +- [FileSize](#FileSize) +- [MTime](#MTime) +- [Sha](#Sha) +- [ReadCsvFile](#ReadCsvFile)
@@ -494,4 +499,165 @@ func main() { absPath := CurrentPath() fmt.Println(absPath) } -``` \ No newline at end of file +``` + + +### IsZipFile + +

Checks if file is zip file or not.

+ +Signature: + +```go +func IsZipFile(filepath string) bool +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/fileutil" +) + +func main() { + isZip := IsZipFile("./zipfile.zip") + fmt.Println(isZip) +} +``` + +### FileSize + +

Returns file size in bytes.

+ +Signature: + +```go +func FileSize(path string) (int64, error) +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/fileutil" +) + +func main() { + size, err := fileutil.FileSize("./testdata/test.txt") + + fmt.Println(size) + fmt.Println(err) + + // Output: + // 20 + // +} +``` + +### MTime + +

Returns file modified time(unix timestamp).

+ +Signature: + +```go +func MTime(filepath string) (int64, error) +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/fileutil" +) + +func main() { + mtime, err := fileutil.MTime("./testdata/test.txt") + + fmt.Println(mtime) + fmt.Println(err) + + // Output: + // 1682391110 + // +} +``` + +### Sha + +

returns file sha value, param `shaType` should be 1, 256 or 512.

+ +Signature: + +```go +func Sha(filepath string, shaType ...int) (string, error) +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/fileutil" +) + +func main() { + sha1, err := fileutil.Sha("./testdata/test.txt", 1) + sha256, _ := fileutil.Sha("./testdata/test.txt", 256) + sha512, _ := fileutil.Sha("./testdata/test.txt", 512) + + fmt.Println(sha1) + fmt.Println(sha256) + fmt.Println(sha512) + fmt.Println(err) + + // Output: + // dda3cf10c5a6ff6c6659a497bf7261b287af2bc7 + // aa6d0a3fbc3442c228d606da09e0c1dc98c69a1cac3da1909199e0266171df35 + // d22aba2a1b7a2e2f512756255cc1c3708905646920cb1eb95e45b531ba74774dbbb89baebf1f716220eb9cf4908f1cfc5b2a01267704d9a59f59d77cab609870 + // +} +``` + +### ReadCsvFile + +

Reads file content into slice.

+ +Signature: + +```go +func ReadCsvFile(filepath string) ([][]string, error) +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/fileutil" +) + +func main() { + content, err := fileutil.ReadCsvFile("./testdata/test.csv") + + fmt.Println(content) + fmt.Println(err) + + // Output: + // [[Bob 12 male] [Duke 14 male] [Lucy 16 female]] + // +} +``` diff --git a/docs/fileutil_zh-CN.md b/docs/fileutil_zh-CN.md index 457dff7..15f9138 100644 --- a/docs/fileutil_zh-CN.md +++ b/docs/fileutil_zh-CN.md @@ -38,6 +38,11 @@ import ( - [Zip](#Zip) - [UnZip](#UnZip) - [CurrentPath](#CurrentPath) +- [IsZipFile](#IsZipFile) +- [FileSize](#FileSize) +- [MTime](#MTime) +- [Sha](#Sha) +- [ReadCsvFile](#ReadCsvFile)
@@ -495,3 +500,164 @@ func main() { fmt.Println(absPath) } ``` + + +### IsZipFile + +

判断文件是否是zip压缩文件。

+ +函数签名: + +```go +func IsZipFile(filepath string) bool +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/fileutil" +) + +func main() { + isZip := IsZipFile("./zipfile.zip") + fmt.Println(isZip) +} +``` + +### FileSize + +

返回文件字节大小。

+ +函数签名: + +```go +func FileSize(path string) (int64, error) +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/fileutil" +) + +func main() { + size, err := fileutil.FileSize("./testdata/test.txt") + + fmt.Println(size) + fmt.Println(err) + + // Output: + // 20 + // +} +``` + +### MTime + +

返回文件修改时间(unix timestamp).

+ +函数签名: + +```go +func MTime(filepath string) (int64, error) +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/fileutil" +) + +func main() { + mtime, err := fileutil.MTime("./testdata/test.txt") + + fmt.Println(mtime) + fmt.Println(err) + + // Output: + // 1682391110 + // +} +``` + +### Sha + +

返回文件sha值,参数`shaType` 应传值为: 1, 256,512.

+ +函数签名: + +```go +func Sha(filepath string, shaType ...int) (string, error) +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/fileutil" +) + +func main() { + sha1, err := fileutil.Sha("./testdata/test.txt", 1) + sha256, _ := fileutil.Sha("./testdata/test.txt", 256) + sha512, _ := fileutil.Sha("./testdata/test.txt", 512) + + fmt.Println(sha1) + fmt.Println(sha256) + fmt.Println(sha512) + fmt.Println(err) + + // Output: + // dda3cf10c5a6ff6c6659a497bf7261b287af2bc7 + // aa6d0a3fbc3442c228d606da09e0c1dc98c69a1cac3da1909199e0266171df35 + // d22aba2a1b7a2e2f512756255cc1c3708905646920cb1eb95e45b531ba74774dbbb89baebf1f716220eb9cf4908f1cfc5b2a01267704d9a59f59d77cab609870 + // +} +``` + +### ReadCsvFile + +

读取csv文件内容到切片

+ +函数签名: + +```go +func ReadCsvFile(filepath string) ([][]string, error) +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/fileutil" +) + +func main() { + content, err := fileutil.ReadCsvFile("./testdata/test.csv") + + fmt.Println(content) + fmt.Println(err) + + // Output: + // [[Bob 12 male] [Duke 14 male] [Lucy 16 female]] + // +} +``` diff --git a/fileutil/file.go b/fileutil/file.go index 8c1a6b3..88918bc 100644 --- a/fileutil/file.go +++ b/fileutil/file.go @@ -7,6 +7,11 @@ package fileutil import ( "archive/zip" "bufio" + "bytes" + "crypto/sha1" + "crypto/sha256" + "crypto/sha512" + "encoding/csv" "errors" "fmt" "io" @@ -330,3 +335,87 @@ func CurrentPath() string { return absPath } + +// IsZipFile checks if file is zip or not. +func IsZipFile(filepath string) bool { + f, err := os.Open(filepath) + if err != nil { + return false + } + defer f.Close() + + buf := make([]byte, 4) + if n, err := f.Read(buf); err != nil || n < 4 { + return false + } + + return bytes.Equal(buf, []byte("PK\x03\x04")) +} + +// FileSize returns file size in bytes. +func FileSize(path string) (int64, error) { + f, err := os.Stat(path) + if err != nil { + return 0, err + } + return f.Size(), nil +} + +// MTime returns file modified time. +func MTime(filepath string) (int64, error) { + f, err := os.Stat(filepath) + if err != nil { + return 0, err + } + return f.ModTime().Unix(), nil +} + +// MTime returns file sha value, param `shaType` should be 1, 256 or 512. +func Sha(filepath string, shaType ...int) (string, error) { + file, err := os.Open(filepath) + if err != nil { + return "", err + } + defer file.Close() + + h := sha1.New() + if len(shaType) > 0 { + if shaType[0] == 1 { + h = sha1.New() + } else if shaType[0] == 256 { + h = sha256.New() + } else if shaType[0] == 512 { + h = sha512.New() + } else { + return "", errors.New("param `shaType` should be 1, 256 or 512.") + } + } + + _, err = io.Copy(h, file) + + if err != nil { + return "", err + } + + sha := fmt.Sprintf("%x", h.Sum(nil)) + + return sha, nil + +} + +// ReadCsvFile read file content into slice. +func ReadCsvFile(filepath string) ([][]string, error) { + f, err := os.Open(filepath) + if err != nil { + return nil, err + } + defer f.Close() + + csvReader := csv.NewReader(f) + records, err := csvReader.ReadAll() + if err != nil { + return nil, err + } + + return records, nil +} diff --git a/fileutil/file_test.go b/fileutil/file_test.go index 945bcf5..f22f052 100644 --- a/fileutil/file_test.go +++ b/fileutil/file_test.go @@ -226,3 +226,54 @@ func TestCurrentPath(t *testing.T) { absPath := CurrentPath() t.Log(absPath) } + +func TestIsZipFile(t *testing.T) { + assert := internal.NewAssert(t, "TestIsZipFile") + + assert.Equal(false, IsZipFile("./file.go")) + assert.Equal(true, IsZipFile("./testdata/file.go.zip")) +} + +func TestFileSize(t *testing.T) { + assert := internal.NewAssert(t, "TestFileSize") + + size, err := FileSize("./testdata/test.txt") + + assert.IsNil(err) + assert.Equal(int64(20), size) +} + +func TestMTime(t *testing.T) { + assert := internal.NewAssert(t, "TestMTime") + + mtime, err := MTime("./testdata/test.txt") + + assert.IsNil(err) + assert.Equal(int64(1682478195), mtime) +} + +func TestSha(t *testing.T) { + assert := internal.NewAssert(t, "TestSha") + + sha1, err := Sha("./testdata/test.txt", 1) + sha256, err := Sha("./testdata/test.txt", 256) + sha512, err := Sha("./testdata/test.txt", 512) + + assert.IsNil(err) + assert.Equal("dda3cf10c5a6ff6c6659a497bf7261b287af2bc7", sha1) + assert.Equal("aa6d0a3fbc3442c228d606da09e0c1dc98c69a1cac3da1909199e0266171df35", sha256) + assert.Equal("d22aba2a1b7a2e2f512756255cc1c3708905646920cb1eb95e45b531ba74774dbbb89baebf1f716220eb9cf4908f1cfc5b2a01267704d9a59f59d77cab609870", sha512) +} + +func TestReadCsvFile(t *testing.T) { + assert := internal.NewAssert(t, "TestReadCsvFile") + + content, err := ReadCsvFile("./testdata/test.csv") + t.Log(content) + + assert.IsNil(err) + + assert.Equal(3, len(content)) + assert.Equal(3, len(content[0])) + assert.Equal("Bob", content[0][0]) +} diff --git a/fileutil/testdata/file.go.zip b/fileutil/testdata/file.go.zip new file mode 100644 index 0000000000000000000000000000000000000000..2383a1ee1701cde0242b6e74f8fd2793bab3eda0 GIT binary patch literal 2523 zcma);cQhLe7seyhu8-R5HB#e^S+mu&s4YT>7_l`qLK|v_QoFIGT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diff --git a/fileutil/testdata/test.csv b/fileutil/testdata/test.csv new file mode 100644 index 0000000..191054c --- /dev/null +++ b/fileutil/testdata/test.csv @@ -0,0 +1,3 @@ +Bob, 12, male +Duke, 14, male +Lucy, 16, female \ No newline at end of file diff --git a/fileutil/testdata/test.txt b/fileutil/testdata/test.txt new file mode 100644 index 0000000..c1cac2d --- /dev/null +++ b/fileutil/testdata/test.txt @@ -0,0 +1 @@ +this is a test file. \ No newline at end of file