Skip to content

Commit 3df08f7

Browse files
Implement support for extracting ZIP archives on Windows in InstallNode function and add ExtractZip utility function for handling ZIP files.
1 parent c239110 commit 3df08f7

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

config/node-utils.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,14 @@ func InstallNode(r *Runtime) error {
105105
fmt.Println("Node is already present...")
106106
}
107107
fmt.Println("Extracting node...")
108-
// deflate node archive
109108

110-
err = utils.ExtractTarGz(t, Config.RuntimesDirectory())
109+
// Handle different archive types
110+
if runtime.GOOS == "windows" {
111+
err = utils.ExtractZip(t, Config.RuntimesDirectory())
112+
} else {
113+
err = utils.ExtractTarGz(t, Config.RuntimesDirectory())
114+
}
115+
111116
if err != nil {
112117
return fmt.Errorf("failed to extract Node.js archive: %w", err)
113118
}

utils/extract.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package utils
22

33
import (
44
"context"
5-
"github.com/mholt/archiver/v4"
65
"io"
76
"os"
87
"path/filepath"
8+
9+
"github.com/mholt/archiver/v4"
910
)
1011

1112
func ExtractTarGz(archive *os.File, targetDir string) error {
@@ -64,3 +65,44 @@ func ExtractTarGz(archive *os.File, targetDir string) error {
6465
}
6566
return nil
6667
}
68+
69+
func ExtractZip(archive *os.File, targetDir string) error {
70+
format := archiver.Zip{}
71+
72+
handler := func(ctx context.Context, f archiver.File) error {
73+
path := filepath.Join(targetDir, f.NameInArchive)
74+
75+
switch f.IsDir() {
76+
case true:
77+
// create a directory
78+
err := os.MkdirAll(path, 0777)
79+
if err != nil {
80+
return err
81+
}
82+
83+
case false:
84+
// write a file
85+
w, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, f.Mode())
86+
if err != nil {
87+
return err
88+
}
89+
90+
stream, _ := f.Open()
91+
defer stream.Close()
92+
93+
_, err = io.Copy(w, stream)
94+
if err != nil {
95+
return err
96+
}
97+
w.Close()
98+
}
99+
100+
return nil
101+
}
102+
103+
err := format.Extract(context.Background(), archive, nil, handler)
104+
if err != nil {
105+
return err
106+
}
107+
return nil
108+
}

0 commit comments

Comments
 (0)