110 lines
4.4 KiB
PowerShell
110 lines
4.4 KiB
PowerShell
# تابع فشردهسازی فایل متنی
|
|
function Compress-File {
|
|
param(
|
|
[Parameter(Mandatory = $true, Position = 0)]
|
|
[string]$InputPath,
|
|
|
|
[Parameter(Mandatory = $true)]
|
|
[string]$OutputPath,
|
|
|
|
[ValidateSet("UTF8", "UTF16", "UTF32")]
|
|
[string]$Encoding = "UTF8"
|
|
)
|
|
|
|
# بررسی وجود فایل ورودی
|
|
if (-not (Test-Path -Path $InputPath)) {
|
|
Write-Error "فایل ورودی یافت نشد: $InputPath"
|
|
return
|
|
}
|
|
|
|
# خواندن محتوای فایل
|
|
Write-Host "در حال خواندن فایل: $InputPath..." -ForegroundColor Cyan
|
|
$rawContent = Get-Content -Path $InputPath -Raw -Encoding $Encoding
|
|
|
|
Write-Host "حجم فایل اصلی: $([math]::Round((($rawContent.Length) / 1KB), 2)) KB" -ForegroundColor Yellow
|
|
|
|
# تبدیل رشته به بایت
|
|
$bytes = [System.Text.Encoding]::UTF8.GetBytes($rawContent)
|
|
|
|
# ایجاد جریانهای حافظه برای فشردهسازی
|
|
$memoryStream = New-Object System.IO.MemoryStream
|
|
$gzipStream = New-Object System.IO.Compression.GzipStream($memoryStream, [System.IO.Compression.CompressionMode]::Compress)
|
|
|
|
try {
|
|
# نوشتن و فشردهسازی
|
|
$gzipStream.Write($bytes, 0, $bytes.Length)
|
|
$gzipStream.Flush()
|
|
|
|
# خواندن دادههای فشرده شده
|
|
$memoryStream.Position = 0
|
|
$compressedBytes = $memoryStream.ToArray()
|
|
|
|
# تبدیل به Base64
|
|
$base64Output = [Convert]::ToBase64String($compressedBytes)
|
|
|
|
# نوشتن خروجی در فایل جدید
|
|
Write-Host "در حال نوشتن خروجی به: $OutputPath..." -ForegroundColor Cyan
|
|
Set-Content -Path $OutputPath -Value $base64Output -Encoding UTF8
|
|
|
|
# نمایش آمار فشردهسازی
|
|
$compressionRatio = ($compressedBytes.Length / $bytes.Length) * 100
|
|
|
|
Write-Host ""
|
|
Write-Host "=============================================" -ForegroundColor Green
|
|
Write-Host " گزارش فشردهسازی" -ForegroundColor Green
|
|
Write-Host "=============================================" -ForegroundColor Green
|
|
Write-Host "فایل اصلی: $InputPath" -ForegroundColor Gray
|
|
Write-Host "فایل خروجی: $OutputPath" -ForegroundColor Gray
|
|
Write-Host "حجم اصلی: $([math]::Round($bytes.Length / 1KB, 2)) KB" -ForegroundColor Gray
|
|
Write-Host "حجم فشرده: $([math]::Round($compressedBytes.Length / 1KB, 2)) KB" -ForegroundColor Gray
|
|
Write-Host "نسبت فشرده: $([math]::Round($compressionRatio, 2))%" -ForegroundColor Gray
|
|
Write-Host "=============================================" -ForegroundColor Green
|
|
|
|
}
|
|
finally {
|
|
if ($gzipStream) { $gzipStream.Dispose() }
|
|
if ($memoryStream) { $memoryStream.Dispose() }
|
|
}
|
|
}
|
|
|
|
# --- اجرای اسکریپت با ورودی از خط فرمان ---
|
|
|
|
# اگر کاربر ورودی نداده باشد، راهنما نمایش داده شود
|
|
if (-not $args[0]) {
|
|
Write-Host "=============================================" -ForegroundColor Cyan
|
|
Write-Host " اسکریپت فشردهسازی فایلهای متنی" -ForegroundColor Cyan
|
|
Write-Host "=============================================" -ForegroundColor Cyan
|
|
Write-Host "نحوه استفاده:"
|
|
Write-Host " .\Compress.ps1 -InputPath 'پاتhto_file.txt' -OutputPath 'output_Compressed.txt'"
|
|
Write-Host ""
|
|
Write-Host "مثال:"
|
|
Write-Host " .\Compress.ps1 -InputPath 'C:\Data\article.txt' -OutputPath 'C:\Data\article_Compressed.txt'"
|
|
Write-Host ""
|
|
exit 0
|
|
}
|
|
|
|
# دریافت پارامترها از خط فرمان
|
|
$InputPath = $args[0]
|
|
$OutputPath = $args[1]
|
|
|
|
# اعتبارسنجی پارامترها
|
|
if (-not (Test-Path -Path $InputPath)) {
|
|
Write-Error "فایل ورودی وجود ندارد: $InputPath"
|
|
exit 1
|
|
}
|
|
|
|
# اگر خروجی مشخص نشده، پیشفرض ایجاد کن
|
|
if (-not $OutputPath) {
|
|
$OutputPath = [System.IO.Path]::ChangeExtension($InputPath, "_Compressed.txt")
|
|
Write-Host "خروجی پیشفرض: $OutputPath" -ForegroundColor Yellow
|
|
}
|
|
|
|
# اجرای تابع
|
|
try {
|
|
Compress-File -InputPath $InputPath -OutputPath $OutputPath
|
|
}
|
|
catch {
|
|
Write-Error "خطا در پردازش فایل: $_"
|
|
exit 1
|
|
}
|