Skip to content

Commit c6bba4f

Browse files
committed
CHG: archiveDirAsZip with fileinfo
1 parent 0fb3c32 commit c6bba4f

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

router/api.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,29 @@ func rawHandler(store *httpd.Store) {
220220
}
221221

222222
func archiveDirAsZip(dirPath string, zipWriter *zip.Writer) error {
223-
walkFunc := func(fullPath string, info fs.DirEntry, err error) error {
223+
walkFunc := func(fullPath string, info fs.FileInfo, err error) error {
224224
if err != nil {
225225
return err
226226
}
227+
228+
relativePath, err := filepath.Rel(dirPath, fullPath)
229+
if err != nil || relativePath == "" || relativePath == "." {
230+
return err
231+
}
227232
if info.IsDir() {
228-
return nil
233+
// https://unix.stackexchange.com/q/743511
234+
// https://github.com/python/cpython/pull/9419
235+
fh := &zip.FileHeader{
236+
Name: relativePath,
237+
Modified: info.ModTime(),
238+
Method: zip.Store,
239+
}
240+
fh.SetMode(info.Mode())
241+
if fh.Name[len(fh.Name)-1] != '/' {
242+
fh.Name += "/"
243+
}
244+
_, err = zipWriter.CreateHeader(fh)
245+
return err
229246
}
230247

231248
file, err := openFile(fullPath)
@@ -234,25 +251,22 @@ func archiveDirAsZip(dirPath string, zipWriter *zip.Writer) error {
234251
}
235252
defer file.Close()
236253

237-
relativePath, err := filepath.Rel(dirPath, fullPath)
238-
if err != nil {
239-
return err
254+
fh := &zip.FileHeader{
255+
Name: relativePath,
256+
UncompressedSize64: uint64(info.Size()),
257+
Modified: info.ModTime(),
258+
Method: zip.Deflate,
240259
}
241-
zipFile, err := zipWriter.CreateHeader(&zip.FileHeader{
242-
Name: relativePath,
243-
Method: zip.Deflate,
244-
})
260+
fh.SetMode(info.Mode())
261+
zipFile, err := zipWriter.CreateHeader(fh)
245262
if err != nil {
246263
return err
247264
}
248265

249-
if _, err := io.Copy(zipFile, file); err != nil {
250-
return err
251-
}
252-
253-
return nil
266+
_, err = io.Copy(zipFile, file)
267+
return err
254268
}
255-
if err := filepath.WalkDir(dirPath, walkFunc); err != nil {
269+
if err := filepath.Walk(dirPath, walkFunc); err != nil {
256270
return err
257271
}
258272
return zipWriter.Close()

0 commit comments

Comments
 (0)