MCPcopy Create free account
hub / github.com/cli/cli / writeZip

Function writeZip

script/ghmock/archive.go:16–58  ·  view source on GitHub ↗
(w io.Writer, root string)

Source from the content-addressed store, hash-verified

14)
15
16func writeZip(w io.Writer, root string) error {
17 info, err := os.Stat(root)
18 if err != nil {
19 return err
20 }
21 if !info.IsDir() {
22 return fmt.Errorf("%s is not a directory", root)
23 }
24 zw := zip.NewWriter(w)
25 walkErr := filepath.WalkDir(root, func(path string, entry fs.DirEntry, walkErr error) error {
26 if walkErr != nil || entry.IsDir() {
27 return walkErr
28 }
29 info, err := entry.Info()
30 if err != nil {
31 return err
32 }
33 if !info.Mode().IsRegular() {
34 return fmt.Errorf("cannot ZIP %s: not a regular file", path)
35 }
36 rel, err := filepath.Rel(root, path)
37 if err != nil {
38 return err
39 }
40 header, err := zip.FileInfoHeader(info)
41 if err != nil {
42 return err
43 }
44 header.Name = filepath.ToSlash(rel)
45 header.Method = zip.Deflate
46 dst, err := zw.CreateHeader(header)
47 if err != nil {
48 return err
49 }
50 file, err := os.Open(path)
51 if err != nil {
52 return err
53 }
54 _, copyErr := io.Copy(dst, file)
55 return errors.Join(copyErr, file.Close())
56 })
57 return errors.Join(walkErr, zw.Close())
58}
59
60func writeExportArchive(path string, write func(string) error) (err error) {
61 if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {

Callers 1

cmdSetFunction · 0.85

Calls 7

IsDirMethod · 0.80
ModeMethod · 0.80
OpenMethod · 0.80
JoinMethod · 0.80
ErrorfMethod · 0.65
CloseMethod · 0.65
CopyMethod · 0.45

Tested by

no test coverage detected