MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / extractZipOnWindows

Function extractZipOnWindows

pkg/npm/install.js:598–687  ·  view source on GitHub ↗
(
  archivePath, destPath, requiredNames, extractNames,
  maxMembers = MAX_ARCHIVE_MEMBERS, shell = 'powershell',
)

Source from the content-addressed store, hash-verified

596}
597
598function extractZipOnWindows(
599 archivePath, destPath, requiredNames, extractNames,
600 maxMembers = MAX_ARCHIVE_MEMBERS, shell = 'powershell',
601) {
602 requireRegularArchive(archivePath);
603 // -EncodedCommand is a constant program. Paths travel only through the child
604 // environment and are consumed with -LiteralPath, so PowerShell never parses
605 // user/TEMP path bytes as source code or wildcard syntax.
606 const script = [
607 "$ErrorActionPreference = 'Stop'",
608 'Add-Type -AssemblyName System.IO.Compression.FileSystem',
609 '$maxMembers = [int]$env:CBM_NPM_MAX_MEMBERS',
610 '$memberCount = 0',
611 '$zip = [System.IO.Compression.ZipFile]::OpenRead($env:CBM_NPM_ARCHIVE_PATH)',
612 "$seen = New-Object 'System.Collections.Generic.HashSet[string]' ([System.StringComparer]::OrdinalIgnoreCase)",
613 "$requiredNames = @($env:CBM_NPM_REQUIRED_NAMES.Split('|'))",
614 '$targetCounts = @{}',
615 'foreach ($requiredName in $requiredNames) { $targetCounts[$requiredName] = 0 }',
616 'try { foreach ($entry in $zip.Entries) { ' +
617 '$memberCount = $memberCount + 1; ' +
618 'if ($memberCount -gt $maxMembers) { ' +
619 'throw "archive exceeds the $maxMembers-member safety limit" }; ' +
620 "$name = $entry.FullName.Replace('\\', '/'); " +
621 "$directory = $name.EndsWith('/'); " +
622 "$segmentsPath = if ($directory) { $name.TrimEnd('/') } else { $name }; " +
623 "$segments = @($segmentsPath.Split('/')); " +
624 "if ([string]::IsNullOrEmpty($segmentsPath) -or $name.StartsWith('/') -or " +
625 "$name.Contains(':') -or $segments -contains '' -or " +
626 "$segments -contains '.' -or $segments -contains '..' -or " +
627 "@($segments | Where-Object { $_.EndsWith('.') -or $_.EndsWith(' ') }).Count -gt 0) { " +
628 "throw \"unsafe zip entry path: $($entry.FullName)\" }; " +
629 'if (-not $seen.Add($segmentsPath)) { ' +
630 "throw \"duplicate or case-conflicting zip entry: $($entry.FullName)\" }; " +
631 'foreach ($requiredName in $requiredNames) { ' +
632 'if ($name -ceq $requiredName) { ' +
633 '$targetCounts[$requiredName] = $targetCounts[$requiredName] + 1 } } ' +
634 '} } finally { $zip.Dispose() }',
635 'foreach ($requiredName in $requiredNames) { ' +
636 'if ($targetCounts[$requiredName] -ne 1) { ' +
637 'throw "archive must contain exactly one $requiredName" } }',
638 'if ($seen.Count -ne $requiredNames.Count) { ' +
639 'throw "archive does not match the exact release root-file allowlist" }',
640 "$extractNames = @($env:CBM_NPM_EXTRACT_NAMES.Split('|'))",
641 '$buffer = New-Object byte[] 65536',
642 '$extractZip = [System.IO.Compression.ZipFile]::OpenRead($env:CBM_NPM_ARCHIVE_PATH)',
643 // Copy by hand so the bytes written can be compared with the declared
644 // size; ExtractToFile would accept whatever the compressed stream yields.
645 'try { foreach ($extractName in $extractNames) { ' +
646 '$entry = @($extractZip.Entries | Where-Object { $_.FullName -ceq $extractName })[0]; ' +
647 '$targetPath = Join-Path $env:CBM_NPM_DEST_PATH $extractName; ' +
648 '$copied = [long]0; ' +
649 '$source = $entry.Open(); ' +
650 '$target = [System.IO.File]::Open($targetPath, [System.IO.FileMode]::CreateNew, [System.IO.FileAccess]::Write); ' +
651 'try { while ($true) { ' +
652 '$count = $source.Read($buffer, 0, $buffer.Length); ' +
653 'if ($count -le 0) { break }; ' +
654 '$copied = $copied + $count; ' +
655 '$target.Write($buffer, 0, $count) } ' +

Callers 2

mainFunction · 0.85

Calls 1

requireRegularArchiveFunction · 0.70

Tested by

no test coverage detected