-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzip.ps1
85 lines (72 loc) · 2.78 KB
/
zip.ps1
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
param(
[string]$source_path,
[string]$plugin_name,
[string]$exclude
)
$sourcePath = $source_path
$destinationPath = Join-Path $sourcePath ($plugin_name + '.zip')
# Convert the exclude parameter into an array
$excludePatterns = $exclude -split ';' | Where-Object { $_ -ne '' }
# Define the function to create the zip file
function Add-Zip {
param(
[string]$sourcePath,
[string]$destinationPath,
[string[]]$excludePatterns,
[string]$containerFolder
)
# Remove existing zip file if it exists
if (Test-Path -Path $destinationPath) {
Remove-Item -Path $destinationPath
}
# Create a temporary directory
$tempDir = [System.IO.Path]::GetTempPath()
$tempCopyPath = Join-Path $tempDir ([System.IO.Path]::GetRandomFileName())
$containerPath = Join-Path $tempCopyPath $containerFolder
# Create the container folder
New-Item -ItemType Directory -Path $containerPath | Out-Null
# Function to check if a path should be excluded
function Should-Exclude {
param (
[string]$path,
[string[]]$patterns
)
foreach ($pattern in $patterns) {
if ($path -like "*$pattern*") {
return $true
}
}
return $false
}
# Copy source directory to the temporary folder, preserving structure
$allItems = Get-ChildItem -Path $sourcePath -Recurse -Force
foreach ($item in $allItems) {
if (-not (Should-Exclude -path $item.FullName -patterns $excludePatterns)) {
$destination = $item.FullName.Replace($sourcePath, $containerPath)
if ($item.PSIsContainer) {
if (-not (Test-Path -Path $destination)) {
New-Item -ItemType Directory -Path $destination -Force | Out-Null
}
} else {
$parentDir = Split-Path -Parent $destination
if (-not (Test-Path -Path $parentDir)) {
New-Item -ItemType Directory -Path $parentDir -Force | Out-Null
}
Copy-Item -Path $item.FullName -Destination $destination -Force
}
}
}
# Use 7-Zip to compress the container folder
$sevenZipPath = "C:\Program Files\7-Zip\7z.exe" # Adjust path if necessary
$arguments = "a", "`"$destinationPath`"", "`"$containerPath`""
try {
& $sevenZipPath @arguments
Write-Output "Zip file created successfully at $destinationPath"
} catch {
Write-Error "Failed to create zip file: $_"
}
# Clean up the temporary directory
Remove-Item -Path $tempCopyPath -Recurse -Force
}
# Call the function with defined variables
Add-Zip -sourcePath $sourcePath -destinationPath $destinationPath -excludePatterns $excludePatterns -containerFolder $plugin_name