MCPcopy
hub / github.com/cloudfoundry/cli / writeZipFile

Function writeZipFile

cf/appfiles/zipper.go:142–200  ·  view source on GitHub ↗
(dir string, targetFile *os.File)

Source from the content-addressed store, hash-verified

140}
141
142func writeZipFile(dir string, targetFile *os.File) error {
143 isEmpty, err := fileutils.IsDirEmpty(dir)
144 if err != nil {
145 return err
146 }
147
148 if isEmpty {
149 return errors.NewEmptyDirError(dir)
150 }
151
152 writer := zip.NewWriter(targetFile)
153 defer writer.Close()
154
155 appfiles := ApplicationFiles{}
156 return appfiles.WalkAppFiles(dir, func(fileName string, fullPath string) error {
157 fileInfo, err := os.Stat(fullPath)
158 if err != nil {
159 return err
160 }
161
162 header, err := zip.FileInfoHeader(fileInfo)
163 if err != nil {
164 return err
165 }
166
167 if runtime.GOOS == "windows" {
168 header.SetMode(header.Mode() | 0700)
169 }
170
171 header.Name = filepath.ToSlash(fileName)
172 header.Method = zip.Deflate
173
174 if fileInfo.IsDir() {
175 header.Name += "/"
176 }
177
178 zipFilePart, err := writer.CreateHeader(header)
179 if err != nil {
180 return err
181 }
182
183 if fileInfo.IsDir() {
184 return nil
185 }
186
187 file, err := os.Open(fullPath)
188 if err != nil {
189 return err
190 }
191 defer file.Close()
192
193 _, err = io.Copy(zipFilePart, file)
194 if err != nil {
195 return err
196 }
197
198 return nil
199 })

Callers 1

ZipMethod · 0.85

Calls 5

WalkAppFilesMethod · 0.95
NewEmptyDirErrorFunction · 0.92
ModeMethod · 0.80
IsDirMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected