-
Notifications
You must be signed in to change notification settings - Fork 844
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TUN-8726: implement compression routine to be used in diagnostic proc…
…edure ## Summary Basic routine to compress the diagnostic files into the root of a zipfile. Closes TUN-8726
- Loading branch information
Luis Neto
committed
Dec 3, 2024
1 parent
b3304bf
commit f884b29
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package diagnostic | ||
|
||
import ( | ||
"archive/zip" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// CreateDiagnosticZipFile create a zip file with the contents from the all | ||
// files paths. The files will be written in the root of the zip file. | ||
// In case of an error occurs after whilst writing to the zip file | ||
// this will be removed. | ||
func CreateDiagnosticZipFile(base string, paths []string) (zipFileName string, err error) { | ||
// Create a zip file with all files from paths added to the root | ||
suffix := time.Now().Format(time.RFC3339) | ||
zipFileName = base + "-" + suffix + ".zip" | ||
zipFileName = strings.ReplaceAll(zipFileName, ":", "-") | ||
|
||
archive, cerr := os.Create(zipFileName) | ||
if cerr != nil { | ||
return "", fmt.Errorf("error creating file %s: %w", zipFileName, cerr) | ||
} | ||
|
||
archiveWriter := zip.NewWriter(archive) | ||
|
||
defer func() { | ||
archiveWriter.Close() | ||
archive.Close() | ||
|
||
if err != nil { | ||
os.Remove(zipFileName) | ||
} | ||
}() | ||
|
||
for _, file := range paths { | ||
if file == "" { | ||
continue | ||
} | ||
|
||
var handle *os.File | ||
|
||
handle, err = os.Open(file) | ||
if err != nil { | ||
return "", fmt.Errorf("error opening file %s: %w", zipFileName, err) | ||
} | ||
|
||
defer handle.Close() | ||
|
||
// Keep the base only to not create sub directories in the | ||
// zip file. | ||
var writer io.Writer | ||
|
||
writer, err = archiveWriter.Create(filepath.Base(file)) | ||
if err != nil { | ||
return "", fmt.Errorf("error creating archive writer from %s: %w", file, err) | ||
} | ||
|
||
if _, err = io.Copy(writer, handle); err != nil { | ||
return "", fmt.Errorf("error copying file %s: %w", file, err) | ||
} | ||
} | ||
|
||
zipFileName = archive.Name() | ||
return zipFileName, nil | ||
} |