zipFile, if br is a file, adds br to the zip archive that zw writes to. If br is a directory, zipFile adds all its files descendants to the zip. parentPath is the path to the parent directory of br. It is only used if dh.pathByRef has not been populated (i.e. if dh does not use a caching fetcher).
(ctx context.Context, parentPath string, br blob.Ref, zw *zip.Writer)
| 567 | // the path to the parent directory of br. It is only used if dh.pathByRef has not |
| 568 | // been populated (i.e. if dh does not use a caching fetcher). |
| 569 | func (dh *DownloadHandler) zipFile(ctx context.Context, parentPath string, br blob.Ref, zw *zip.Writer) error { |
| 570 | if len(dh.pathByRef) == 0 { |
| 571 | // if dh.pathByRef is not populated, we have to check for ourselves now whether |
| 572 | // br is a directory. |
| 573 | di, err := dh.dirInfo(ctx, br) |
| 574 | if err != nil && err != errNotDir { |
| 575 | return err |
| 576 | } |
| 577 | if di.isDir { |
| 578 | for _, v := range di.children { |
| 579 | if err := dh.zipFile(ctx, filepath.Join(parentPath, di.name), v, zw); err != nil { |
| 580 | return err |
| 581 | } |
| 582 | } |
| 583 | return nil |
| 584 | } |
| 585 | } |
| 586 | fi, _, err := dh.fileInfo(ctx, br) |
| 587 | if err != nil { |
| 588 | return err |
| 589 | } |
| 590 | defer fi.close() |
| 591 | filename, ok := dh.pathByRef[br] |
| 592 | if !ok { |
| 593 | // because we're in the len(dh.pathByRef) == 0 case. |
| 594 | filename = filepath.Join(parentPath, fi.name) |
| 595 | } |
| 596 | zh := &zip.FileHeader{ |
| 597 | Name: filename, |
| 598 | Method: zip.Store, |
| 599 | Modified: fi.modtime.UTC(), |
| 600 | } |
| 601 | zh.SetMode(fi.mode) |
| 602 | zfh, err := zw.CreateHeader(zh) |
| 603 | if err != nil { |
| 604 | return err |
| 605 | } |
| 606 | _, err = io.Copy(zfh, fi.rs) |
| 607 | if err != nil { |
| 608 | return err |
| 609 | } |
| 610 | return nil |
| 611 | } |