2 回答
TA贡献1777条经验 获得超10个赞
package main
import (
"archive/zip"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
func main() {
var (
Path = os.Args[1]
Name = os.Args[2]
)
File, _ := os.Create(Name)
PS := strings.Split(Path, "\\")
PathName := strings.Join(PS[:len(PS)-1], "\\")
os.Chdir(PathName)
Path = PS[len(PS)-1]
defer File.Close()
Zip := zip.NewWriter(File)
defer Zip.Close()
walk := func(Path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println(err)
return err
}
if info.IsDir() {
return nil
}
Src, _ := os.Open(Path)
defer Src.Close()
//FileName, _ := Zip.Create(Path)
h := &zip.FileHeader{Name: Path, Method: zip.Deflate, Flags: 0x800}
FileName, _ := Zip.CreateHeader(h)
io.Copy(FileName, Src)
Zip.Flush()
return nil
}
if err := filepath.Walk(Path, walk); err != nil {
fmt.Println(err)
}
}
- 2 回答
- 0 关注
- 183 浏览
添加回答
举报