enter code here
using namespace System.IO
using namespace System.IO.Compression
function Compress-CompatibleArchive {
param (
[String[]] $Path,
[String] $DestinationPath,
[ValidateSet(“Fastest”, “NoCompression”, “Optimal”, “Maximum”)]
[String] $CompressionLevel
)
ValidatePaths($Path)
ValidateDestinationPath($DestinationPath)
$outputStream = [FileStream]::new($DestinationPath, [FileMode]::Create, [FileAccess]::Write)
$archive = [Compression.ZipArchive]::new($outputStream, [Compression.ZipArchiveMode]::Create)
if ("Fastest" -eq $CompressionLevel) {
$level = [CompressionLevel]::Fastest
} elseif ("Optimal" -eq $CompressionLevel) {
$level = [CompressionLevel]::Optimal
} elseif ("Maximum" -eq $CompressionLevel) {
$level = [CompressionLevel]::SmallestSize
} else {
$level = [CompressionLevel]::NoCompression
}
foreach ($file in $Path) {
$fileName = [Path]::GetFileName($file)
$entry = $archive.CreateEntry($fileName, $level)
$entryStream = $entry.Open()
$inputStream = [FileStream]::new($file, [FileMode]::Open, [FileAccess]::Read)
$inputStream.CopyTo($entryStream)
$inputStream.Dispose()
$entryStream.Dispose()
}
$archive.Dispose()
$outputStream.Dispose()
}
}
Export-ModuleMember -Function Compress-CompatibleArchive
command used :Compress-CompatibleArchive (args[1]) (args[2] in Test.ps1 file
Arguments passed from cmdline :
./test1.ps1 “source path” “destination path”
ERROR :
File not found (C:UsersUSEROneDrivePicturestesting)
At C:UsersUSEROneDriveDocumentsWindowsPowerShellmodulesCompress-CompatibleArchive_Pranav.psm1:50 char:3
-
throw [FileNotFoundException] "File not found ($f)"
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : OperationStopped: (:) [], FileNotFoundException
- FullyQualifiedErrorId : File not found (C:UsersUSEROneDrivePicturestesting)
Folder structure :
folder1
folder2
folder3 -1.txt, 2.txt