writerZippedReport writes a zip with the various report parts to file s - file: the name of the zip file - folder: the top-level folder created in the zip to contain all sections
(sections []zipFileWriter, file, folder string)
| 62 | // - file: the name of the zip file |
| 63 | // - folder: the top-level folder created in the zip to contain all sections |
| 64 | func writeZippedReport(sections []zipFileWriter, file, folder string) (err error) { |
| 65 | if exists, _ := fileutils.FileExists(file); exists { |
| 66 | return fmt.Errorf("file already exist will not overwrite") |
| 67 | } |
| 68 | |
| 69 | outputFile, err := os.Create(filepath.Clean(file)) |
| 70 | if err != nil { |
| 71 | return fmt.Errorf("could not create zip file: %w", err) |
| 72 | } |
| 73 | |
| 74 | defer func() { |
| 75 | errF := outputFile.Sync() |
| 76 | if errF != nil && err == nil { |
| 77 | err = fmt.Errorf("could not flush the zip file: %w", errF) |
| 78 | } |
| 79 | |
| 80 | errF = outputFile.Close() |
| 81 | if errF != nil && err == nil { |
| 82 | err = fmt.Errorf("could not close the zip file: %w", errF) |
| 83 | } |
| 84 | }() |
| 85 | |
| 86 | zipper := zip.NewWriter(outputFile) |
| 87 | defer func() { |
| 88 | if errZ := zipper.Close(); errZ != nil { |
| 89 | if err == nil { |
| 90 | err = fmt.Errorf("could not close the zip: %w", errZ) |
| 91 | } |
| 92 | } |
| 93 | }() |
| 94 | |
| 95 | _, err = zipper.Create(folder + "/") |
| 96 | if err != nil { |
| 97 | return err |
| 98 | } |
| 99 | |
| 100 | for _, section := range sections { |
| 101 | err = section(zipper, folder) |
| 102 | if err != nil { |
| 103 | return err |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return err |
| 108 | } |
| 109 | |
| 110 | type namedObject struct { |
| 111 | Name string |