1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

Merge branch 'main' into v2

This commit is contained in:
dudaodong
2023-06-17 19:38:53 +08:00
3 changed files with 132 additions and 17 deletions

View File

@@ -208,7 +208,11 @@ func Zip(fpath string, destPath string) error {
archive := zip.NewWriter(zipFile)
defer archive.Close()
err = filepath.Walk(fpath, func(path string, info os.FileInfo, err error) error {
return addFileToArchive(fpath, archive)
}
func addFileToArchive(fpath string, archive *zip.Writer) error {
err := filepath.Walk(fpath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
@@ -224,34 +228,24 @@ func Zip(fpath string, destPath string) error {
header.Name += "/"
} else {
header.Method = zip.Deflate
}
writer, err := archive.CreateHeader(header)
if err != nil {
return err
}
if !info.IsDir() {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(writer, file)
if err != nil {
if _, err := io.Copy(writer, file); err != nil {
return err
}
}
return nil
})
if err != nil {
return err
}
return nil
}
// UnZip unzip the file and save it to destPath.
// Play: https://go.dev/play/p/g0w34kS7B8m
func UnZip(zipFile string, destPath string) error {
@@ -302,6 +296,64 @@ func UnZip(zipFile string, destPath string) error {
return nil
}
// ZipAppendEntry append a single file or directory by fpath to an existing zip file.
// Play: https://go.dev/play/p/cxvaT8TRNQp
func ZipAppendEntry(fpath string, destPath string) error {
tempFile, err := os.CreateTemp("", "temp.zip")
if err != nil {
return err
}
defer os.Remove(tempFile.Name())
zipReader, err := zip.OpenReader(destPath)
if err != nil {
return err
}
archive := zip.NewWriter(tempFile)
for _, zipItem := range zipReader.File {
zipItemReader, err := zipItem.Open()
if err != nil {
return err
}
header, err := zip.FileInfoHeader(zipItem.FileInfo())
if err != nil {
return err
}
header.Name = zipItem.Name
targetItem, err := archive.CreateHeader(header)
if err != nil {
return err
}
_, err = io.Copy(targetItem, zipItemReader)
if err != nil {
return err
}
}
err = addFileToArchive(fpath, archive)
if err != nil {
return err
}
err = zipReader.Close()
if err != nil {
return err
}
err = archive.Close()
if err != nil {
return err
}
err = tempFile.Close()
if err != nil {
return err
}
return CopyFile(tempFile.Name(), destPath)
}
func safeFilepathJoin(path1, path2 string) (string, error) {
relPath, err := filepath.Rel(".", path2)
if err != nil || strings.HasPrefix(relPath, "..") {

View File

@@ -224,6 +224,30 @@ func ExampleUnZip() {
// application/octet-stream
}
func ExampleZipAppendEntry() {
zipFile := "./test.zip"
CopyFile("./testdata/file.go.zip", zipFile)
ZipAppendEntry("./testdata", zipFile)
unZipPath := "./unzip"
UnZip(zipFile, unZipPath)
fmt.Println(IsExist("./unzip/file.go"))
fmt.Println(IsExist("./unzip/testdata/file.go.zip"))
fmt.Println(IsExist("./unzip/testdata/test.csv"))
fmt.Println(IsExist("./unzip/testdata/test.txt"))
os.Remove(zipFile)
os.RemoveAll(unZipPath)
// Output:
// true
// true
// true
// true
}
func ExampleIsZipFile() {
result1 := IsZipFile("./file.go")
result2 := IsZipFile("./testdata/file.go.zip")

View File

@@ -191,6 +191,45 @@ func TestZipAndUnZip(t *testing.T) {
os.RemoveAll(unZipPath)
}
func TestZipAppendEntry(t *testing.T) {
assert := internal.NewAssert(t, "TestZipAppendEntry")
zipFile := "./text.zip"
err := CopyFile("./testdata/file.go.zip", zipFile)
assert.IsNil(err)
srcFile := "./text.txt"
CreateFile(srcFile)
file, _ := os.OpenFile(srcFile, os.O_WRONLY|os.O_TRUNC, os.ModePerm)
_, err = file.WriteString("hello\nworld")
if err != nil {
t.Fail()
}
file.Close()
err = ZipAppendEntry(srcFile, zipFile)
assert.IsNil(err)
err = ZipAppendEntry("./testdata", zipFile)
assert.IsNil(err)
unZipPath := "./unzip"
err = UnZip(zipFile, unZipPath)
assert.IsNil(err)
assert.Equal(true, IsExist("./unzip/text.txt"))
assert.Equal(true, IsExist("./unzip/file.go"))
assert.Equal(true, IsExist("./unzip/testdata/file.go.zip"))
assert.Equal(true, IsExist("./unzip/testdata/test.csv"))
assert.Equal(true, IsExist("./unzip/testdata/test.txt"))
os.Remove(srcFile)
os.Remove(zipFile)
os.RemoveAll(unZipPath)
}
func TestFileMode(t *testing.T) {
assert := internal.NewAssert(t, "TestFileMode")