I'm working on a script to extract all zips in my downloads folder to a directory on my desktop. I got it working fine for my purposes but there's a single .zip file I had tried that is giving me issues.
When trying to ExtractToDirectory, it returns that there are 2 arguments and that "Extracting to Zip entry would hvae resulted in a file outside the specified destination directory" I'm able to use WinZip as well as file explorer's Extract All to unzip this .zip correctly. I'm at a loss of what the issue might be.
#Script to Unzip .zip files in downloads folder.
#$src = file you're unzipping, $dest = destination for contents
$src = 'C:\Users\' + $env:USERNAME + '\Downloads\'
$dest = 'C:\Users\' + $env:USERNAME + '\Desktop\Zips\'
Function UnZipper($src, $dest)
{
Add-Type -AssemblyName System.IO.Compression.FileSystem
#specifies zip files in directory
$zps = Get-ChildItem $src -Filter *.zip
#unpacks each zip in directory to destination folder
foreach ($zp IN $zps)
{
#Sets File Path of original zip
$Zips = ($src + $zp)
#Sets destination of zip. Creates folder if it does not exist.
$NewFolder = $dest + $zp
$NewFolder = $NewFolder.TrimEnd(".zip")
#Checks if file already exists (has been unzipped)
$FileExists = Test-Path $NewFolder
If ($FileExists -eq $false)
{
Write-Host 'UnZipping -'$zp
[System.IO.Compression.ZipFile]::ExtractToDirectory($Zips, $NewFolder)
}
}
}
UnZipper -src $src -dest $dest
Write-Host "Your files have been Un-Zipped!"
Write-Host "Press the enter key to exit and open your file(s)" -ForegroundColor Green
Read-Host
explorer $dest
Ultimately if it's a problem with the specific zip that cannot be fixed, that's fine with me. I'm just trying to figure out why it works with WinZip but not PowerShell.
$zps = Get-ChildItem $src -Filter *.zip -File
produces an array of FileInfo objects and you should not treat them as simple file path strings as you do in the foreach loop.
Better use the Join-Path cmdlet to construct paths where needed:
Below a re-write of your function
function UnZipper {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)]
[string]$src,
[Parameter(Mandatory = $true, Position = 1)]
[string]$dest
)
Add-Type -AssemblyName System.IO.Compression.FileSystem
# get an array of FileInfo objects for zip files in the $src directory and loop through
Get-ChildItem $src -Filter *.zip -File | ForEach-Object {
# unpacks each zip in directory to destination folder
# the automatic variable '$_' here represents a single FileInfo object, each file at a time
# Get the destination folder for the zip file. Create the folder if it does not exist.
$destination = Join-Path -Path $dest -ChildPath $_.BaseName # $_.BaseName does not include the extension
# Check if the folder already exists
if (!(Test-Path $destination -PathType Container)) {
# create the destination folder
New-Item -Path $destination -ItemType Directory -Force | Out-Null
# unzip the file
Write-Host "UnZipping - $($_.FullName)"
[System.IO.Compression.ZipFile]::ExtractToDirectory($_.FullName, $destination)
}
else {
Write-Host "Folder '$destination' already exists. File skipped."
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.