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:
@@ -208,7 +208,11 @@ func Zip(fpath string, destPath string) error {
|
|||||||
archive := zip.NewWriter(zipFile)
|
archive := zip.NewWriter(zipFile)
|
||||||
defer archive.Close()
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -224,32 +228,22 @@ func Zip(fpath string, destPath string) error {
|
|||||||
header.Name += "/"
|
header.Name += "/"
|
||||||
} else {
|
} else {
|
||||||
header.Method = zip.Deflate
|
header.Method = zip.Deflate
|
||||||
}
|
writer, err := archive.CreateHeader(header)
|
||||||
|
if err != nil {
|
||||||
writer, err := archive.CreateHeader(header)
|
return err
|
||||||
if err != nil {
|
}
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if !info.IsDir() {
|
|
||||||
file, err := os.Open(path)
|
file, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
_, err = io.Copy(writer, file)
|
if _, err := io.Copy(writer, file); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
return err
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnZip unzip the file and save it to destPath.
|
// UnZip unzip the file and save it to destPath.
|
||||||
@@ -302,6 +296,64 @@ func UnZip(zipFile string, destPath string) error {
|
|||||||
return nil
|
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) {
|
func safeFilepathJoin(path1, path2 string) (string, error) {
|
||||||
relPath, err := filepath.Rel(".", path2)
|
relPath, err := filepath.Rel(".", path2)
|
||||||
if err != nil || strings.HasPrefix(relPath, "..") {
|
if err != nil || strings.HasPrefix(relPath, "..") {
|
||||||
|
|||||||
@@ -224,6 +224,30 @@ func ExampleUnZip() {
|
|||||||
// application/octet-stream
|
// 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() {
|
func ExampleIsZipFile() {
|
||||||
result1 := IsZipFile("./file.go")
|
result1 := IsZipFile("./file.go")
|
||||||
result2 := IsZipFile("./testdata/file.go.zip")
|
result2 := IsZipFile("./testdata/file.go.zip")
|
||||||
|
|||||||
@@ -191,6 +191,45 @@ func TestZipAndUnZip(t *testing.T) {
|
|||||||
os.RemoveAll(unZipPath)
|
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) {
|
func TestFileMode(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestFileMode")
|
assert := internal.NewAssert(t, "TestFileMode")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user