[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$SourcePath, [Parameter(Mandatory = $true)] [string]$OutputPath, [string[]]$Exclude = @('*.tmp', '*.log', '.git\*', '*\.git\*', 'node_modules\*', '*\node_modules\*', '__pycache__\*', '*\__pycache__\*', '.venv\*', '*\.venv\*'), [ValidateSet('Optimal', 'Fastest', 'NoCompression')] [string]$CompressionLevel = 'Optimal', [switch]$Force ) $ErrorActionPreference = 'Stop' Add-Type -AssemblyName System.IO.Compression.FileSystem if (-not (Test-Path -LiteralPath $SourcePath -PathType Container)) { throw "Папка не найдена: $SourcePath" } $sourceFull = (Resolve-Path -LiteralPath $SourcePath).Path.TrimEnd([char[]]@([char]'\', [char]'/')) $outputFull = [IO.Path]::GetFullPath($OutputPath) if (Test-Path -LiteralPath $outputFull) { if (-not $Force) { throw "Файл уже существует: $outputFull. Используйте -Force" } Remove-Item -LiteralPath $outputFull -Force } $parent = Split-Path -Parent $outputFull if ($parent -and -not (Test-Path -LiteralPath $parent)) { New-Item -ItemType Directory -Path $parent -Force | Out-Null } $level = [System.Enum]::Parse([System.IO.Compression.CompressionLevel], $CompressionLevel) $files = @(Get-ChildItem -LiteralPath $sourceFull -File -Recurse -Force | Where-Object { $_.FullName -ne $outputFull }) if ($files.Count -eq 0) { throw "В папке нет файлов" } $stream = [IO.File]::Open($outputFull, [IO.FileMode]::CreateNew) $zip = [System.IO.Compression.ZipArchive]::new($stream, [System.IO.Compression.ZipArchiveMode]::Create) $count = 0 $skipped = 0 try { foreach ($file in $files) { $relative = $file.FullName.Substring($sourceFull.Length) $relative = $relative.TrimStart([char[]]@([IO.Path]::DirectorySeparatorChar, [IO.Path]::AltDirectorySeparatorChar)) $relative = $relative.Replace('\', '/') $relativeWindows = $relative.Replace('/', '\') $excluded = $false foreach ($pattern in $Exclude) { if ($relative -like $pattern -or $relativeWindows -like $pattern) { $excluded = $true break } } if ($excluded) { $skipped++ continue } $entry = $zip.CreateEntry($relative, $level) $entry.LastWriteTime = $file.LastWriteTime $input = [IO.File]::OpenRead($file.FullName) try { $output = $entry.Open() try { $input.CopyTo($output) } finally { $output.Dispose() } } finally { $input.Dispose() } $count++ } } finally { $zip.Dispose() $stream.Dispose() } if ($count -eq 0) { Remove-Item -LiteralPath $outputFull -Force throw "После исключений не осталось файлов" } "Добавлено файлов: $count, исключено: $skipped" "Готово: $outputFull"