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 0000000..2383a1e Binary files /dev/null and b/fileutil/testdata/file.go.zip differ 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