Skip to content

Commit

Permalink
zip extract - introduce a function to copy each file to destination
Browse files Browse the repository at this point in the history
This will ensure each zip file handler is closed just after the copy is done.
  • Loading branch information
💥Hedi Ghediri committed Nov 16, 2023
1 parent 8daa945 commit 4f646d1
Showing 1 changed file with 49 additions and 41 deletions.
90 changes: 49 additions & 41 deletions internal/pkg/zip-package.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,47 +50,8 @@ func (pkg *zipPackage) InstallTo(targetDir string) (command.PackageManifest, err
zipReader, _ := zip.OpenReader(pkg.ZipFile)
defer zipReader.Close()
for _, file := range zipReader.Reader.File {
zippedFile, err := file.Open()
if err != nil {
return nil, fmt.Errorf("installation failed: %s", err)
}
defer zippedFile.Close()

extractedFilePath := filepath.Join(targetDir, file.Name)
if file.FileInfo().IsDir() {
log.Println("Directory Created:", extractedFilePath)
err := os.MkdirAll(extractedFilePath, file.Mode())
if err != nil {
return nil, fmt.Errorf("directory extraction failed: %s", err)
}

fileStats, err := os.Stat(extractedFilePath)
if err != nil {
return nil, fmt.Errorf("failed to stat %s: %s", extractedFilePath, err)
}
permissions := fileStats.Mode().Perm()
if permissions != 0o755 {
// chmod to 755
if err := os.Chmod(extractedFilePath, 0755); err != nil {
return nil, fmt.Errorf("failed to chmod %s to 0755: %s", extractedFilePath, err)
}
}
} else {
log.Println("File extracted:", file.Name)
outputFile, err := os.OpenFile(
extractedFilePath,
os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
file.Mode(),
)
if err != nil {
return nil, fmt.Errorf("file extraction failed: %s", err)
}
defer outputFile.Close()

_, err = io.Copy(outputFile, zippedFile)
if err != nil {
return nil, fmt.Errorf("file data extraction failed: %s", err)
}
if err := extractZipEntry(targetDir, file); err != nil {
return nil, err
}
}

Expand Down Expand Up @@ -133,3 +94,50 @@ func packageChecksum(pkgFile string) ([]byte, error) {

return h.Sum(nil), nil
}

func extractZipEntry(targetDir string, file *zip.File) error {
zippedFile, err := file.Open()
if err != nil {
return fmt.Errorf("installation failed: %s", err)
}
defer zippedFile.Close()

extractedFilePath := filepath.Join(targetDir, file.Name)
if file.FileInfo().IsDir() {
log.Println("Directory Created:", extractedFilePath)
err := os.MkdirAll(extractedFilePath, file.Mode())
if err != nil {
return fmt.Errorf("directory extraction failed: %s", err)
}

fileStats, err := os.Stat(extractedFilePath)
if err != nil {
return fmt.Errorf("failed to stat %s: %s", extractedFilePath, err)
}
permissions := fileStats.Mode().Perm()
if permissions != 0o755 {
// chmod to 755
if err := os.Chmod(extractedFilePath, 0755); err != nil {
return fmt.Errorf("failed to chmod %s to 0755: %s", extractedFilePath, err)
}
}
} else {
log.Println("File extracted:", file.Name)
outputFile, err := os.OpenFile(
extractedFilePath,
os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
file.Mode(),
)
if err != nil {
return fmt.Errorf("file extraction failed: %s", err)
}
defer outputFile.Close()

_, err = io.Copy(outputFile, zippedFile)
if err != nil {
return fmt.Errorf("file data extraction failed: %s", err)
}
}

return nil
}

0 comments on commit 4f646d1

Please sign in to comment.