File size: 1,174 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
package main
import (
"archive/zip"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
)
// extractFromZip will extract `name` from the zip file and write it to `out`.
// If `exec` is set, the file will be marked executable.
func extractFromZip(z *zip.Reader, name, out string, exec bool) error {
err := os.MkdirAll(filepath.Dir(out), 0755)
if err != nil {
return fmt.Errorf("create output dir '%s': %w", filepath.Dir(out), err)
}
tmpFile := out + ".tmp"
mode := fs.FileMode(0666)
if exec {
mode = 0755
}
outFile, err := os.OpenFile(tmpFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode)
if err != nil {
return fmt.Errorf("create tmp file '%s': %w", tmpFile, err)
}
defer outFile.Close()
bin, err := z.Open(name)
if err != nil {
return fmt.Errorf("find '%s' in zip: %w", name, err)
}
defer bin.Close()
_, err = io.Copy(outFile, bin)
if err != nil {
return fmt.Errorf("extract '%s' to '%s': %w", name, tmpFile, err)
}
err = outFile.Close()
if err != nil {
return fmt.Errorf("close '%s': %w", tmpFile, err)
}
err = os.Rename(tmpFile, out)
if err != nil {
return fmt.Errorf(`ERROR: rename '%s' to '%s': %w`, tmpFile, out, err)
}
return nil
}
|