-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathZipFilesForChatGPT.ps1
More file actions
101 lines (84 loc) · 3.42 KB
/
ZipFilesForChatGPT.ps1
File metadata and controls
101 lines (84 loc) · 3.42 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
param(
[Parameter(Position=0)]
[string]$Root = (Get-Location).Path,
[Parameter(Position=1)]
[string]$DestinationZip = ("MarvinsAIRARefactored-{0}.zip" -f (Get-Date -Format 'yyyyMMdd-HHmm')),
# Optional: put every entry under a top-level folder inside the zip
[string]$RootFolderName = ""
)
# Include only these extensions
$extensions = @(
'.cs', '.xaml', '.resx', '.csproj', '.sln',
'.props', '.targets', '.json', '.xml', '.config',
'.iss', '.js', '.html', '.css', '.ino', '.py', '.php'
)
# Normalize and resolve the root path
$rootPath = (Resolve-Path -LiteralPath $Root).Path
# --- Resolve DestinationZip to a FULL path you have rights to ---
# If it's not rooted (just a file name or relative), put it under $rootPath
if (-not ([System.IO.Path]::IsPathRooted($DestinationZip))) {
$DestinationZip = Join-Path -Path $rootPath -ChildPath $DestinationZip
}
# Ensure destination directory exists
$zipDir = Split-Path -Parent $DestinationZip
if ($zipDir -and -not (Test-Path -LiteralPath $zipDir)) {
New-Item -ItemType Directory -Path $zipDir | Out-Null
}
Write-Host "Root: $rootPath"
Write-Host "Zip : $DestinationZip"
# Remove existing zip if present
if (Test-Path -LiteralPath $DestinationZip) {
Remove-Item -LiteralPath $DestinationZip -Force
}
# Collect files, excluding .git/.vs/bin/obj anywhere in the path
$excludeRegex = '(^|[\\/])(\.idea|\.git|\.vs|bin|obj|resx-backups)([\\/]|$)'
$files = Get-ChildItem -LiteralPath $rootPath -Recurse -File -Force |
Where-Object {
$extensions -contains $_.Extension.ToLowerInvariant() -and
($_.FullName -notmatch $excludeRegex)
}
if (-not $files) {
Write-Warning "No matching files found under '$rootPath'."
exit 0
}
# Load compression APIs (needed on Windows PowerShell 5.1)
$loaded = [AppDomain]::CurrentDomain.GetAssemblies() | ForEach-Object { $_.GetName().Name }
if ($loaded -notcontains 'System.IO.Compression') {
Add-Type -AssemblyName System.IO.Compression
}
if ($loaded -notcontains 'System.IO.Compression.FileSystem') {
Add-Type -AssemblyName System.IO.Compression.FileSystem
}
function Get-RelPath([string]$basePath, [string]$fullPath) {
if ($PSVersionTable.PSVersion.Major -ge 6) {
return [System.IO.Path]::GetRelativePath($basePath, $fullPath)
} else {
$baseUri = New-Object System.Uri ($basePath.TrimEnd('\') + '\')
$fullUri = New-Object System.Uri $fullPath
$rel = $baseUri.MakeRelativeUri($fullUri).ToString()
return [System.Uri]::UnescapeDataString($rel)
}
}
$prefix = if ([string]::IsNullOrWhiteSpace($RootFolderName)) { "" } else { ($RootFolderName.TrimEnd('\','/') + "/") }
# Create the zip and add each file with its relative path preserved
try {
$zip = [System.IO.Compression.ZipFile]::Open($DestinationZip, [System.IO.Compression.ZipArchiveMode]::Create)
} catch {
Write-Error "Failed to open zip at '$DestinationZip'. $_"
exit 1
}
try {
foreach ($f in $files) {
$rel = Get-RelPath $rootPath $f.FullName
$entryPath = ($prefix + $rel) -replace '\\','/' # zip uses forward slashes
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile(
$zip, $f.FullName, $entryPath, [System.IO.Compression.CompressionLevel]::Optimal
) | Out-Null
}
}
finally {
if ($zip) { $zip.Dispose() }
}
Write-Host "Created zip:" -ForegroundColor Green
Write-Host " $DestinationZip"
Write-Host "Files included:" $($files.Count)