mirror of
https://github.com/duke-git/lancet.git
synced 2025-11-03 11:21:23 +08:00
feat: add IsZipFile
This commit is contained in:
@@ -38,7 +38,7 @@ import (
|
|||||||
- [ReadFileByLine](#ReadFileByLine)
|
- [ReadFileByLine](#ReadFileByLine)
|
||||||
- [Zip](#Zip)
|
- [Zip](#Zip)
|
||||||
- [UnZip](#UnZip)
|
- [UnZip](#UnZip)
|
||||||
- [UnZip](#UnZip)
|
- [IsZipFile](#IsZipFile)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -496,3 +496,30 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="IsZipFile">IsZipFile</span>
|
||||||
|
|
||||||
|
<p>Checks if file is zip file or not.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsZipFile(filepath string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
isZip := IsZipFile("./zipfile.zip")
|
||||||
|
fmt.Println(isZip)
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -38,6 +38,7 @@ import (
|
|||||||
- [ReadFileByLine](#ReadFileByLine)
|
- [ReadFileByLine](#ReadFileByLine)
|
||||||
- [Zip](#Zip)
|
- [Zip](#Zip)
|
||||||
- [UnZip](#UnZip)
|
- [UnZip](#UnZip)
|
||||||
|
- [IsZipFile](#IsZipFile)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -472,7 +473,7 @@ func main() {
|
|||||||
|
|
||||||
<p>zip解压缩文件并保存在目录中</p>
|
<p>zip解压缩文件并保存在目录中</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func UnZip(zipFile string, destPath string) error
|
func UnZip(zipFile string, destPath string) error
|
||||||
@@ -495,3 +496,29 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="IsZipFile">IsZipFile</span>
|
||||||
|
|
||||||
|
<p>判断文件是否是zip压缩文件。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func IsZipFile(filepath string) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
isZip := IsZipFile("./zipfile.zip")
|
||||||
|
fmt.Println(isZip)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ package fileutil
|
|||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -174,6 +175,23 @@ func ListFileNames(path string) ([]string, error) {
|
|||||||
return result, nil
|
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.
|
// Zip create zip file, fpath could be a single file or a directory.
|
||||||
// Play: https://go.dev/play/p/j-3sWBp8ik_P
|
// Play: https://go.dev/play/p/j-3sWBp8ik_P
|
||||||
func Zip(fpath string, destPath string) error {
|
func Zip(fpath string, destPath string) error {
|
||||||
@@ -276,6 +294,7 @@ func UnZip(zipFile string, destPath string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -223,3 +223,15 @@ func ExampleUnZip() {
|
|||||||
// Output:
|
// Output:
|
||||||
// application/octet-stream
|
// application/octet-stream
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleIsZipFile() {
|
||||||
|
result1 := IsZipFile("./file.go")
|
||||||
|
result2 := IsZipFile("./test/file.go.zip")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
|||||||
@@ -246,3 +246,10 @@ func TestCurrentPath(t *testing.T) {
|
|||||||
absPath := CurrentPath()
|
absPath := CurrentPath()
|
||||||
t.Log(absPath)
|
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"))
|
||||||
|
}
|
||||||
|
|||||||
@@ -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) {
|
func makeTestFailed(t *testing.T, caseName string, expected, actual any) {
|
||||||
_, file, line, _ := runtime.Caller(2)
|
_, file, line, _ := runtime.Caller(2)
|
||||||
errInfo := fmt.Sprintf("Case %v failed. file: %v, line: %v, expected: %v, actual: %v.", caseName, file, line, expected, actual)
|
errInfo := fmt.Sprintf("Case %v failed. file: %v, line: %v, expected: %v, actual: %v.", caseName, file, line, expected, actual)
|
||||||
|
|||||||
Reference in New Issue
Block a user