1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-23 13:52:26 +08:00

Compare commits

..

7 Commits

Author SHA1 Message Date
dudaodong
80cbbdc787 docs: format index content 2022-11-26 17:37:09 +08:00
dudaodong
be148e07ba fix: fix chunk slice bug 2022-11-26 16:21:57 +08:00
dudaodong
d36ab5cc3a fix: go report issue ineffassign 2022-11-18 17:05:54 +08:00
dudaodong
2b17329094 release v2.1.10 2022-11-17 16:11:48 +08:00
dudaodong
f869a0a670 fix: issue#62: fix ZipSlip bug 2022-11-16 16:04:38 +08:00
dudaodong
be000a4bd6 fix: issue#62: fix ZipSlip bug 2022-11-16 15:08:42 +08:00
DuDaoDong
81efa800ea Create SECURITY.md 2022-11-16 11:31:50 +08:00
32 changed files with 61 additions and 57 deletions

View File

@@ -4,7 +4,7 @@
<br/> <br/>
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf) ![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf)
[![Release](https://img.shields.io/badge/release-2.1.9-green.svg)](https://github.com/duke-git/lancet/releases) [![Release](https://img.shields.io/badge/release-2.1.10-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2) [![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
@@ -38,7 +38,7 @@ English | [简体中文](./README_zh-CN.md)
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
``` ```
2. <b>For users who use version below go1.18, you should install v1.x.x. now latest v1 is v1.3.3. </b> 2. <b>For users who use version below go1.18, you should install v1.x.x. now latest v1 is v1.3.4. </b>
```go ```go
go get github.com/duke-git/lancet@v1.3.3 // below go1.18, install latest version of v1.x.x go get github.com/duke-git/lancet@v1.3.3 // below go1.18, install latest version of v1.x.x

View File

@@ -4,7 +4,7 @@
<br/> <br/>
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf) ![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf)
[![Release](https://img.shields.io/badge/release-2.1.9-green.svg)](https://github.com/duke-git/lancet/releases) [![Release](https://img.shields.io/badge/release-2.1.10-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2) [![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
@@ -37,7 +37,7 @@
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
``` ```
2. <b>使用 go1.18 以下版本的用户,必须安装 v1.x.x。目前最新的 v1 版本是 v1.3.3。</b> 2. <b>使用 go1.18 以下版本的用户,必须安装 v1.x.x。目前最新的 v1 版本是 v1.3.4。</b>
```go ```go
go get github.com/duke-git/lancet@v1.3.3 // 使用go1.18以下版本, 必须安装v1.x.x版本 go get github.com/duke-git/lancet@v1.3.3 // 使用go1.18以下版本, 必须安装v1.x.x版本

15
SECURITY.md Normal file
View File

@@ -0,0 +1,15 @@
# Security Policy
## Supported Versions
Here is the lancet version and compatibility with go language version.
| Version | Supported |
| ------- | ------------------|
| 2.x.x | +go v1.18 |
| 1.x.x | +go v1.12 |
## Reporting a Vulnerability
For now, there is no public website to report a vulnerability, If you find security issue in lancet, you can send it to me via my email `lanliddd.2007@163.com`.
we can discuss it. I am appreciate if someone can create a public page for reporting vulnerability.

View File

@@ -21,6 +21,7 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Index ## Index
- [BubbleSort](#BubbleSort) - [BubbleSort](#BubbleSort)
- [InsertionSort](#InsertionSort) - [InsertionSort](#InsertionSort)
- [SelectionSort](#SelectionSort) - [SelectionSort](#SelectionSort)
@@ -31,7 +32,6 @@ import (
- [CountSort](#CountSort) - [CountSort](#CountSort)
- [BinarySearch](#BinarySearch) - [BinarySearch](#BinarySearch)
- [BinaryIterativeSearch](#BinaryIterativeSearch) - [BinaryIterativeSearch](#BinaryIterativeSearch)
- [LinearSearch](#LinearSearch) - [LinearSearch](#LinearSearch)
- [LRUCache](#LRUCache) - [LRUCache](#LRUCache)

View File

@@ -29,7 +29,6 @@ import (
- [QuickSort](#QuickSort) - [QuickSort](#QuickSort)
- [HeapSort](#HeapSort) - [HeapSort](#HeapSort)
- [MergeSort](#MergeSort) - [MergeSort](#MergeSort)
- [CountSort](#CountSort) - [CountSort](#CountSort)
- [BinarySearch](#BinarySearch) - [BinarySearch](#BinarySearch)
- [BinaryIterativeSearch](#BinaryIterativeSearch) - [BinaryIterativeSearch](#BinaryIterativeSearch)

View File

@@ -25,7 +25,6 @@ import (
- [ToBytes](#ToBytes) - [ToBytes](#ToBytes)
- [ToChar](#ToChar) - [ToChar](#ToChar)
- [ToChannel](#ToChannel) - [ToChannel](#ToChannel)
- [ToFloat](#ToFloat) - [ToFloat](#ToFloat)
- [ToInt](#ToInt) - [ToInt](#ToInt)
- [ToJson](#ToJson) - [ToJson](#ToJson)

View File

@@ -27,7 +27,6 @@ import (
- [ToBytes](#ToBytes) - [ToBytes](#ToBytes)
- [ToChar](#ToChar) - [ToChar](#ToChar)
- [ToChannel](#ToChannel) - [ToChannel](#ToChannel)
- [ToFloat](#ToFloat) - [ToFloat](#ToFloat)
- [ToInt](#ToInt) - [ToInt](#ToInt)
- [ToJson](#ToJson) - [ToJson](#ToJson)

View File

@@ -47,7 +47,6 @@ import (
- [HmacSha1](#HmacSha1) - [HmacSha1](#HmacSha1)
- [HmacSha256](#HmacSha256) - [HmacSha256](#HmacSha256)
- [HmacSha512](#HmacSha512) - [HmacSha512](#HmacSha512)
- [Md5String](#Md5String) - [Md5String](#Md5String)
- [Md5File](#Md5File) - [Md5File](#Md5File)
- [Sha1](#Sha1) - [Sha1](#Sha1)

View File

@@ -33,7 +33,6 @@ import (
- [AesOfbDecrypt](#AesOfbDecrypt) - [AesOfbDecrypt](#AesOfbDecrypt)
- [Base64StdEncode](#Base64StdEncode) - [Base64StdEncode](#Base64StdEncode)
- [Base64StdDecode](#Base64StdDecode) - [Base64StdDecode](#Base64StdDecode)
- [DesEcbEncrypt](#DesEcbEncrypt) - [DesEcbEncrypt](#DesEcbEncrypt)
- [DesEcbDecrypt](#DesEcbDecrypt) - [DesEcbDecrypt](#DesEcbDecrypt)
- [DesCbcEncrypt](#DesCbcEncrypt) - [DesCbcEncrypt](#DesCbcEncrypt)
@@ -43,7 +42,6 @@ import (
- [DesCfbDecrypt](#DesCfbDecrypt) - [DesCfbDecrypt](#DesCfbDecrypt)
- [DesOfbEncrypt](#DesOfbEncrypt) - [DesOfbEncrypt](#DesOfbEncrypt)
- [DesOfbDecrypt](#DesOfbDecrypt) - [DesOfbDecrypt](#DesOfbDecrypt)
- [HmacMd5](#HmacMd5) - [HmacMd5](#HmacMd5)
- [HmacSha1](#HmacSha1) - [HmacSha1](#HmacSha1)
- [HmacSha256](#HmacSha256) - [HmacSha256](#HmacSha256)

View File

@@ -34,7 +34,6 @@ import (
- [IsEmpty](#IsEmpty) - [IsEmpty](#IsEmpty)
- [Union](#Union) - [Union](#Union)
- [Intersection](#Intersection) - [Intersection](#Intersection)
- [SymmetricDifference](#SymmetricDifference) - [SymmetricDifference](#SymmetricDifference)
- [Minus](#Minus) - [Minus](#Minus)

View File

@@ -34,7 +34,6 @@ import (
- [IsEmpty](#IsEmpty) - [IsEmpty](#IsEmpty)
- [Union](#Union) - [Union](#Union)
- [Intersection](#Intersection) - [Intersection](#Intersection)
- [SymmetricDifference](#SymmetricDifference) - [SymmetricDifference](#SymmetricDifference)
- [Minus](#Minus) - [Minus](#Minus)

View File

@@ -29,7 +29,6 @@ import (
- [PreOrderTraverse](#BSTree_PreOrderTraverse) - [PreOrderTraverse](#BSTree_PreOrderTraverse)
- [InOrderTraverse](#BSTree_InOrderTraverse) - [InOrderTraverse](#BSTree_InOrderTraverse)
- [PostOrderTraverse](#BSTree_PostOrderTraverse) - [PostOrderTraverse](#BSTree_PostOrderTraverse)
- [LevelOrderTraverse](#BSTree_LevelOrderTraverse) - [LevelOrderTraverse](#BSTree_LevelOrderTraverse)
- [Depth](#BSTree_Depth) - [Depth](#BSTree_Depth)
- [HasSubTree](#BSTree_HasSubTree) - [HasSubTree](#BSTree_HasSubTree)

View File

@@ -29,7 +29,6 @@ import (
- [PreOrderTraverse](#BSTree_PreOrderTraverse) - [PreOrderTraverse](#BSTree_PreOrderTraverse)
- [InOrderTraverse](#BSTree_InOrderTraverse) - [InOrderTraverse](#BSTree_InOrderTraverse)
- [PostOrderTraverse](#BSTree_PostOrderTraverse) - [PostOrderTraverse](#BSTree_PostOrderTraverse)
- [LevelOrderTraverse](#BSTree_LevelOrderTraverse) - [LevelOrderTraverse](#BSTree_LevelOrderTraverse)
- [Depth](#BSTree_Depth) - [Depth](#BSTree_Depth)
- [HasSubTree](#BSTree_HasSubTree) - [HasSubTree](#BSTree_HasSubTree)

View File

@@ -42,7 +42,6 @@ import (
- [GetNightTimestamp](#GetNightTimestamp) - [GetNightTimestamp](#GetNightTimestamp)
- [FormatTimeToStr](#FormatTimeToStr) - [FormatTimeToStr](#FormatTimeToStr)
- [FormatStrToTime](#FormatStrToTime) - [FormatStrToTime](#FormatStrToTime)
- [NewUnixNow](#NewUnixNow) - [NewUnixNow](#NewUnixNow)
- [NewUnix](#NewUnix) - [NewUnix](#NewUnix)
- [NewFormat](#NewFormat) - [NewFormat](#NewFormat)

View File

@@ -41,7 +41,6 @@ import (
- [GetNightTimestamp](#GetNightTimestamp) - [GetNightTimestamp](#GetNightTimestamp)
- [FormatTimeToStr](#FormatTimeToStr) - [FormatTimeToStr](#FormatTimeToStr)
- [FormatStrToTime](#FormatStrToTime) - [FormatStrToTime](#FormatStrToTime)
- [NewUnixNow](#NewUnixNow) - [NewUnixNow](#NewUnixNow)
- [NewUnix](#NewUnix) - [NewUnix](#NewUnix)
- [NewFormat](#NewFormat) - [NewFormat](#NewFormat)

View File

@@ -28,7 +28,6 @@ import (
- [IsExist](#IsExist) - [IsExist](#IsExist)
- [IsLink](#IsLink) - [IsLink](#IsLink)
- [IsDir](#IsDir) - [IsDir](#IsDir)
- [ListFileNames](#ListFileNames) - [ListFileNames](#ListFileNames)
- [RemoveFile](#RemoveFile) - [RemoveFile](#RemoveFile)
- [ReadFileToString](#ReadFileToString) - [ReadFileToString](#ReadFileToString)

View File

@@ -28,7 +28,6 @@ import (
- [IsExist](#IsExist) - [IsExist](#IsExist)
- [IsLink](#IsLink) - [IsLink](#IsLink)
- [IsDir](#IsDir) - [IsDir](#IsDir)
- [ListFileNames](#ListFileNames) - [ListFileNames](#ListFileNames)
- [RemoveFile](#RemoveFile) - [RemoveFile](#RemoveFile)
- [ReadFileToString](#ReadFileToString) - [ReadFileToString](#ReadFileToString)

View File

@@ -28,7 +28,6 @@ import (
- [MaxBy](#MaxBy) - [MaxBy](#MaxBy)
- [Min](#Min) - [Min](#Min)
- [MinBy](#MaxBy) - [MinBy](#MaxBy)
- [Percent](#Percent) - [Percent](#Percent)
- [RoundToFloat](#RoundToFloat) - [RoundToFloat](#RoundToFloat)
- [RoundToString](#RoundToString) - [RoundToString](#RoundToString)

View File

@@ -28,7 +28,6 @@ import (
- [MaxBy](#MaxBy) - [MaxBy](#MaxBy)
- [Min](#Min) - [Min](#Min)
- [MinBy](#MaxBy) - [MinBy](#MaxBy)
- [Percent](#Percent) - [Percent](#Percent)
- [RoundToFloat](#RoundToFloat) - [RoundToFloat](#RoundToFloat)
- [RoundToString](#RoundToString) - [RoundToString](#RoundToString)

View File

@@ -25,7 +25,6 @@ import (
## Index ## Index
- [ConvertMapToQueryString](#ConvertMapToQueryString) - [ConvertMapToQueryString](#ConvertMapToQueryString)
- [EncodeUrl](#EncodeUrl) - [EncodeUrl](#EncodeUrl)
- [GetInternalIp](#GetInternalIp) - [GetInternalIp](#GetInternalIp)
- [GetIps](#GetIps) - [GetIps](#GetIps)
- [GetMacAddrs](#GetMacAddrs) - [GetMacAddrs](#GetMacAddrs)
@@ -38,7 +37,6 @@ import (
- [SendRequest](#SendRequest) - [SendRequest](#SendRequest)
- [DecodeResponse](#DecodeResponse) - [DecodeResponse](#DecodeResponse)
- [StructToUrlValues](#StructToUrlValues) - [StructToUrlValues](#StructToUrlValues)
- [HttpGet<sup>Deprecated</sup>](#HttpGet) - [HttpGet<sup>Deprecated</sup>](#HttpGet)
- [HttpDelete<sup>Deprecated</sup>](#HttpDelete) - [HttpDelete<sup>Deprecated</sup>](#HttpDelete)
- [HttpPost<sup>Deprecated</sup>](#HttpPost) - [HttpPost<sup>Deprecated</sup>](#HttpPost)

View File

@@ -30,7 +30,6 @@ import (
- [GetMacAddrs](#GetMacAddrs) - [GetMacAddrs](#GetMacAddrs)
- [GetPublicIpInfo](#GetPublicIpInfo) - [GetPublicIpInfo](#GetPublicIpInfo)
- [GetRequestPublicIp](#GetRequestPublicIp) - [GetRequestPublicIp](#GetRequestPublicIp)
- [IsPublicIP](#IsPublicIP) - [IsPublicIP](#IsPublicIP)
- [IsInternalIP](#IsInternalIP) - [IsInternalIP](#IsInternalIP)
- [HttpRequest](#HttpRequest) - [HttpRequest](#HttpRequest)
@@ -38,13 +37,11 @@ import (
- [SendRequest](#SendRequest) - [SendRequest](#SendRequest)
- [DecodeResponse](#DecodeResponse) - [DecodeResponse](#DecodeResponse)
- [StructToUrlValues](#StructToUrlValues) - [StructToUrlValues](#StructToUrlValues)
- [HttpGet<sup>Deprecated</sup>](#HttpGet) - [HttpGet<sup>Deprecated</sup>](#HttpGet)
- [HttpDelete<sup>Deprecated</sup>](#HttpDelete) - [HttpDelete<sup>Deprecated</sup>](#HttpDelete)
- [HttpPost<sup>Deprecated</sup>](#HttpPost) - [HttpPost<sup>Deprecated</sup>](#HttpPost)
- [HttpPut<sup>Deprecated</sup>](#HttpPut) - [HttpPut<sup>Deprecated</sup>](#HttpPut)
- [HttpPatch<sup>Deprecated</sup>](#HttpPatch) - [HttpPatch<sup>Deprecated</sup>](#HttpPatch)
- [ParseHttpResponse](#ParseHttpResponse) - [ParseHttpResponse](#ParseHttpResponse)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>

View File

@@ -26,7 +26,6 @@ import (
- [RandInt](#RandInt) - [RandInt](#RandInt)
- [RandString](#RandString) - [RandString](#RandString)
- [RandUpper](#RandUpper) - [RandUpper](#RandUpper)
- [RandLower](#RandLower) - [RandLower](#RandLower)
- [RandNumeral](#RandNumeral) - [RandNumeral](#RandNumeral)
- [RandNumeralOrLetter](#RandNumeralOrLetter) - [RandNumeralOrLetter](#RandNumeralOrLetter)

View File

@@ -26,7 +26,6 @@ import (
- [RandInt](#RandInt) - [RandInt](#RandInt)
- [RandString](#RandString) - [RandString](#RandString)
- [RandUpper](#RandUpper) - [RandUpper](#RandUpper)
- [RandLower](#RandLower) - [RandLower](#RandLower)
- [RandNumeral](#RandNumeral) - [RandNumeral](#RandNumeral)
- [RandNumeralOrLetter](#RandNumeralOrLetter) - [RandNumeralOrLetter](#RandNumeralOrLetter)

View File

@@ -172,8 +172,8 @@ import (
func main() { func main() {
arr := []string{"a", "b", "c", "d", "e"} arr := []string{"a", "b", "c", "d", "e"}
res := slice.Chunk(InterfaceSlice(arr), 3) res := slice.Chunk((arr), 3)
fmt.Println(res) //[][]any{{"a", "b", "c"}, {"d", "e"}} fmt.Println(res) //[][]string{{"a", "b", "c"}, {"d", "e"}}
} }
``` ```

View File

@@ -20,8 +20,10 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 目录 ## 目录
- [AppendIfAbsent](#AppendIfAbsent) - [AppendIfAbsent](#AppendIfAbsent)
- [Contain](#Contain) - [Contain](#Contain)
- [ContainSubSlice](#ContainSubSlice) - [ContainSubSlice](#ContainSubSlice)
@@ -46,7 +48,6 @@ import (
- [GroupBy](#GroupBy) - [GroupBy](#GroupBy)
- [GroupWith](#GroupWith) - [GroupWith](#GroupWith)
- [IntSlice](#IntSlice) - [IntSlice](#IntSlice)
- [InterfaceSlice](#InterfaceSlice) - [InterfaceSlice](#InterfaceSlice)
- [Intersection](#Intersection) - [Intersection](#Intersection)
- [InsertAt](#InsertAt) - [InsertAt](#InsertAt)
@@ -72,6 +73,7 @@ import (
- [Without](#Without) - [Without](#Without)
- [KeyBy](#KeyBy) - [KeyBy](#KeyBy)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 文档 ## 文档
@@ -172,8 +174,8 @@ import (
func main() { func main() {
arr := []string{"a", "b", "c", "d", "e"} arr := []string{"a", "b", "c", "d", "e"}
res := slice.Chunk(InterfaceSlice(arr), 3) res := slice.Chunk(arr, 3)
fmt.Println(res) //[][]any{{"a", "b", "c"}, {"d", "e"}} fmt.Println(res) //[][]string{{"a", "b", "c"}, {"d", "e"}}
} }
``` ```

View File

@@ -36,7 +36,6 @@ import (
- [SnakeCase](#SnakeCase) - [SnakeCase](#SnakeCase)
- [SplitEx](#SplitEx) - [SplitEx](#SplitEx)
- [Wrap](#Wrap) - [Wrap](#Wrap)
- [Unwrap](#Unwrap) - [Unwrap](#Unwrap)

View File

@@ -36,7 +36,6 @@ import (
- [SnakeCase](#SnakeCase) - [SnakeCase](#SnakeCase)
- [SplitEx](#SplitEx) - [SplitEx](#SplitEx)
- [Wrap](#Wrap) - [Wrap](#Wrap)
- [Unwrap](#Unwrap) - [Unwrap](#Unwrap)

View File

@@ -34,7 +34,6 @@ import (
- [IsCreditCard](#IsCreditCard) - [IsCreditCard](#IsCreditCard)
- [IsDns](#IsDns) - [IsDns](#IsDns)
- [IsEmail](#IsEmail) - [IsEmail](#IsEmail)
- [IsEmptyString](#IsEmptyString) - [IsEmptyString](#IsEmptyString)
- [IsFloatStr](#IsFloatStr) - [IsFloatStr](#IsFloatStr)
- [IsNumberStr](#IsNumberStr) - [IsNumberStr](#IsNumberStr)

View File

@@ -34,7 +34,6 @@ import (
- [IsCreditCard](#IsCreditCard) - [IsCreditCard](#IsCreditCard)
- [IsDns](#IsDns) - [IsDns](#IsDns)
- [IsEmail](#IsEmail) - [IsEmail](#IsEmail)
- [IsEmptyString](#IsEmptyString) - [IsEmptyString](#IsEmptyString)
- [IsFloatStr](#IsFloatStr) - [IsFloatStr](#IsFloatStr)
- [IsNumberStr](#IsNumberStr) - [IsNumberStr](#IsNumberStr)

View File

@@ -8,6 +8,7 @@ import (
"archive/zip" "archive/zip"
"bufio" "bufio"
"errors" "errors"
"fmt"
"io" "io"
"io/fs" "io/fs"
"io/ioutil" "io/ioutil"
@@ -213,6 +214,7 @@ func Zip(fpath string, destPath string) error {
// UnZip unzip the file and save it to destPath // UnZip unzip the file and save it to destPath
func UnZip(zipFile string, destPath string) error { func UnZip(zipFile string, destPath string) error {
zipReader, err := zip.OpenReader(zipFile) zipReader, err := zip.OpenReader(zipFile)
if err != nil { if err != nil {
return err return err
@@ -220,7 +222,12 @@ func UnZip(zipFile string, destPath string) error {
defer zipReader.Close() defer zipReader.Close()
for _, f := range zipReader.File { for _, f := range zipReader.File {
path := filepath.Join(destPath, f.Name) //issue#62: fix ZipSlip bug
path, err := safeFilepathJoin(destPath, f.Name)
if err != nil {
return err
}
if f.FileInfo().IsDir() { if f.FileInfo().IsDir() {
os.MkdirAll(path, os.ModePerm) os.MkdirAll(path, os.ModePerm)
} else { } else {
@@ -249,6 +256,17 @@ func UnZip(zipFile string, destPath string) error {
return nil return nil
} }
func safeFilepathJoin(path1, path2 string) (string, error) {
relPath, err := filepath.Rel(".", path2)
if err != nil || strings.HasPrefix(relPath, "..") {
return "", fmt.Errorf("(zipslip) filepath is unsafe %q: %v", path2, err)
}
if path1 == "" {
path1 = "."
}
return filepath.Join(path1, filepath.Join("/", relPath)), nil
}
// IsLink checks if a file is symbol link or not // IsLink checks if a file is symbol link or not
func IsLink(path string) bool { func IsLink(path string) bool {
fi, err := os.Lstat(path) fi, err := os.Lstat(path)

View File

@@ -42,29 +42,18 @@ func Chunk[T any](slice []T, size int) [][]T {
return result return result
} }
length := len(slice) for _, item := range slice {
if size == 1 || size >= length { l := len(result)
for _, v := range slice { if l == 0 || len(result[l-1]) == size {
var tmp []T result = append(result, []T{})
tmp = append(tmp, v) l++
result = append(result, tmp)
} }
return result
}
// divide slice equally result[l-1] = append(result[l-1], item)
divideNum := length/size + 1
for i := 0; i < divideNum; i++ {
if i == divideNum-1 {
if len(slice[i*size:]) > 0 {
result = append(result, slice[i*size:])
}
} else {
result = append(result, slice[i*size:(i+1)*size])
}
} }
return result return result
} }
// Compact creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey // Compact creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey

View File

@@ -31,6 +31,10 @@ func TestChunk(t *testing.T) {
arr := []string{"a", "b", "c", "d", "e"} arr := []string{"a", "b", "c", "d", "e"}
assert.Equal([][]string{}, Chunk(arr, -1))
assert.Equal([][]string{}, Chunk(arr, 0))
r1 := [][]string{{"a"}, {"b"}, {"c"}, {"d"}, {"e"}} r1 := [][]string{{"a"}, {"b"}, {"c"}, {"d"}, {"e"}}
assert.Equal(r1, Chunk(arr, 1)) assert.Equal(r1, Chunk(arr, 1))
@@ -43,8 +47,11 @@ func TestChunk(t *testing.T) {
r4 := [][]string{{"a", "b", "c", "d"}, {"e"}} r4 := [][]string{{"a", "b", "c", "d"}, {"e"}}
assert.Equal(r4, Chunk(arr, 4)) assert.Equal(r4, Chunk(arr, 4))
r5 := [][]string{{"a"}, {"b"}, {"c"}, {"d"}, {"e"}} r5 := [][]string{{"a", "b", "c", "d", "e"}}
assert.Equal(r5, Chunk(arr, 5)) assert.Equal(r5, Chunk(arr, 5))
r6 := [][]string{{"a", "b", "c", "d", "e"}}
assert.Equal(r6, Chunk(arr, 6))
} }
func TestCompact(t *testing.T) { func TestCompact(t *testing.T) {