diff --git a/docs/fileutil.md b/docs/fileutil.md index 9cd04d3..e039a61 100644 --- a/docs/fileutil.md +++ b/docs/fileutil.md @@ -38,7 +38,7 @@ import ( - [ReadFileByLine](#ReadFileByLine) - [Zip](#Zip) - [UnZip](#UnZip) -- [UnZip](#UnZip) +- [IsZipFile](#IsZipFile)
@@ -496,3 +496,30 @@ func main() { } } ``` + + +### 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/v2/fileutil" +) + +func main() { + isZip := IsZipFile("./zipfile.zip") + fmt.Println(isZip) +} +``` \ No newline at end of file diff --git a/docs/fileutil_zh-CN.md b/docs/fileutil_zh-CN.md index 180cc02..5b5896e 100644 --- a/docs/fileutil_zh-CN.md +++ b/docs/fileutil_zh-CN.md @@ -38,6 +38,7 @@ import ( - [ReadFileByLine](#ReadFileByLine) - [Zip](#Zip) - [UnZip](#UnZip) +- [IsZipFile](#IsZipFile) @@ -472,7 +473,7 @@ func main() {zip解压缩文件并保存在目录中
-Signature: +函数签名: ```go func UnZip(zipFile string, destPath string) error @@ -495,3 +496,29 @@ func main() { } } ``` + +### IsZipFile + +判断文件是否是zip压缩文件。
+ +函数签名: + +```go +func IsZipFile(filepath string) bool +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/fileutil" +) + +func main() { + isZip := IsZipFile("./zipfile.zip") + fmt.Println(isZip) +} +``` diff --git a/fileutil/file.go b/fileutil/file.go index 4feb7ad..b8dd825 100644 --- a/fileutil/file.go +++ b/fileutil/file.go @@ -7,6 +7,7 @@ package fileutil import ( "archive/zip" "bufio" + "bytes" "errors" "fmt" "io" @@ -174,6 +175,23 @@ func ListFileNames(path string) ([]string, error) { return result, nil } +// IsZipFile checks if file is zip or not. +// Play: todo +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")) +} + // Zip create zip file, fpath could be a single file or a directory. // Play: https://go.dev/play/p/j-3sWBp8ik_P func Zip(fpath string, destPath string) error { @@ -276,6 +294,7 @@ func UnZip(zipFile string, destPath string) error { } } } + return nil } diff --git a/fileutil/file_example_test.go b/fileutil/file_example_test.go index 44b10c7..7e8e350 100644 --- a/fileutil/file_example_test.go +++ b/fileutil/file_example_test.go @@ -223,3 +223,15 @@ func ExampleUnZip() { // Output: // application/octet-stream } + +func ExampleIsZipFile() { + result1 := IsZipFile("./file.go") + result2 := IsZipFile("./test/file.go.zip") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // false + // true +} diff --git a/fileutil/file_test.go b/fileutil/file_test.go index 0a38a5f..0b0921f 100644 --- a/fileutil/file_test.go +++ b/fileutil/file_test.go @@ -246,3 +246,10 @@ 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("./test/file.go.zip")) +} diff --git a/internal/assert.go b/internal/assert.go index d51af3c..7e2656c 100644 --- a/internal/assert.go +++ b/internal/assert.go @@ -211,7 +211,7 @@ func compare(x, y any) int { } -// logFailedInfo make test failed and log error info +// makeTestFailed make test failed and log error info func makeTestFailed(t *testing.T, caseName string, expected, actual any) { _, file, line, _ := runtime.Caller(2) errInfo := fmt.Sprintf("Case %v failed. file: %v, line: %v, expected: %v, actual: %v.", caseName, file, line, expected, actual)