MCPcopy Index your code
hub / github.com/claude-code-best/claude-code / extractZip

Function extractZip

scripts/postinstall.cjs:211–281  ·  view source on GitHub ↗
(buffer, binaryPath, extractedBinary)

Source from the content-addressed store, hash-verified

209}
210
211async function extractZip(buffer, binaryPath, extractedBinary) {
212 const binaryDir = path.dirname(binaryPath)
213 // Try fflate first (bundled dep)
214 let fflateError
215 try {
216 const { unzipSync } = require('fflate')
217 const unzipped = unzipSync(new Uint8Array(buffer))
218 const key = findZipEntryKey(unzipped, extractedBinary)
219 if (!key) {
220 throw new Error(`Binary ${extractedBinary} not found in zip`)
221 }
222 writeFileSync(binaryPath, Buffer.from(unzipped[key]))
223 return
224 } catch (e) {
225 fflateError = e
226 }
227
228 // Fallback: PowerShell Expand-Archive or unzip CLI
229 const tmpDir = path.join(binaryDir, '.tmp-download')
230 rmSync(tmpDir, { recursive: true, force: true })
231 mkdirSync(tmpDir, { recursive: true })
232 try {
233 const assetName = `archive.zip`
234 const archivePath = path.join(tmpDir, assetName)
235 writeFileSync(archivePath, buffer)
236
237 let extracted = false
238 if (process.platform === 'win32') {
239 const psCmd = `Expand-Archive -Path '${archivePath.replace(/'/g, "''")}' -DestinationPath '${tmpDir.replace(/'/g, "''")}' -Force`
240 const psResult = spawnSync(
241 'powershell.exe',
242 [
243 '-NoProfile',
244 '-NonInteractive',
245 '-ExecutionPolicy',
246 'Bypass',
247 '-Command',
248 psCmd,
249 ],
250 { stdio: 'pipe', windowsHide: true },
251 )
252 if (psResult.status === 0) {
253 extracted = true
254 }
255 }
256
257 if (!extracted) {
258 const result = spawnSync('unzip', ['-o', archivePath, '-d', tmpDir], {
259 stdio: 'pipe',
260 })
261 if (result.status !== 0) {
262 const unzipErr = result.stderr?.toString().trim() || 'command not found'
263 const fflateMsg =
264 fflateError instanceof Error
265 ? fflateError.message
266 : String(fflateError)
267 throw new Error(
268 `zip extraction failed (fflate: ${fflateMsg}; unzip: ${unzipErr})`,

Callers 1

downloadAndExtractFunction · 0.85

Calls 6

findZipEntryKeyFunction · 0.85
rmSyncFunction · 0.85
mkdirSyncFunction · 0.85
existsSyncFunction · 0.85
renameSyncFunction · 0.85
toStringMethod · 0.65

Tested by

no test coverage detected