func RepackageZip()

in internal/extraction/extraction.go [142:173]


func RepackageZip(in io.Reader, outputDirPrefix string, zipFile *archive.ZipFile) error {
	// it seems the only way to repack a zip archive is to completely read it into memory first
	b := new(bytes.Buffer)
	if _, err := b.ReadFrom(in); err != nil {
		return err
	}

	zipReader, err := zip.NewReader(bytes.NewReader(b.Bytes()), int64(b.Len()))
	if err != nil {
		return err
	}
	// api-diagnostics creates a common top folder we don't need when repackaging
	topLevelDir := ""
	for _, f := range zipReader.File {
		// skip all the directory entries
		if f.UncompressedSize64 == 0 {
			continue
		}
		// extract the tld first time round
		if topLevelDir == "" {
			topLevelDir = archive.RootDir(f.Name)
		}
		out, err := zipFile.Create(toOutputPath(f.Name, topLevelDir, outputDirPrefix))
		if err != nil {
			return err
		}
		if err := copyFromZip(f, out); err != nil {
			return err
		}
	}
	return nil
}