From f61eedc6532f6ce42f2dfb5f21940b40a03d8fba Mon Sep 17 00:00:00 2001 From: Darrick West Date: Mon, 18 Feb 2019 11:04:27 -0500 Subject: [PATCH 1/4] mrdrwest-hidden-files-bugfix --- .../Microsoft.PowerShell.Archive.psm1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1 b/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1 index d8af02f..7d20fdd 100644 --- a/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1 +++ b/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1 @@ -138,7 +138,7 @@ function Compress-Archive # if we have write access permission to update the existing archive file. if($archiveFileExist -and $Update -eq $true) { - $item = Get-Item -Path $DestinationPath + $item = Get-Item -Path $DestinationPath -Force if($item.Attributes.ToString().Contains("ReadOnly")) { $errorMessage = ($LocalizedData.ArchiveFileIsReadOnly -f $DestinationPath) @@ -236,7 +236,7 @@ function Compress-Archive } elseif ($PassThru) { - Get-Item -LiteralPath $DestinationPath + Get-Item -LiteralPath $DestinationPath -Force } } } @@ -423,7 +423,7 @@ function Expand-Archive # Return the expanded items, being careful to remove trailing directory separators from # any folder paths for consistency $trailingDirSeparators = '\' + [System.IO.Path]::DirectorySeparatorChar + '+$' - Get-Item -LiteralPath ($expandedItems -replace $trailingDirSeparators) + Get-Item -LiteralPath ($expandedItems -replace $trailingDirSeparators) -Force } } } @@ -689,7 +689,7 @@ function CompressSingleDirHelper $modifiedSourceDirFullName = $sourceDirFullName + [System.IO.Path]::DirectorySeparatorChar } - $dirContents = Get-ChildItem -LiteralPath $sourceDirPath -Recurse + $dirContents = Get-ChildItem -LiteralPath $sourceDirPath -Recurse -Force foreach($currentContent in $dirContents) { $isContainer = $currentContent -is [System.IO.DirectoryInfo] @@ -828,7 +828,7 @@ function ZipArchiveHelper # Updating the File Creation time so that the same timestamp would be retained after expanding the compressed file. # At this point we are sure that Get-ChildItem would succeed. - $lastWriteTime = (Get-Item -LiteralPath $currentFilePath).LastWriteTime + $lastWriteTime = (Get-Item -LiteralPath $currentFilePath -Force).LastWriteTime if ($lastWriteTime.Year -lt 1980) { Write-Warning "'$currentFilePath' has LastWriteTime earlier than 1980. Compress-Archive will store any files with LastWriteTime values earlier than 1980 as 1/1/1980 00:00." From c18387d714dae8f2c77c1735b7a6bdf7c0201994 Mon Sep 17 00:00:00 2001 From: Darrick West Date: Mon, 18 Feb 2019 11:17:07 -0500 Subject: [PATCH 2/4] mrdrwest-psanalyzer-cleanup-suggestions --- .../Microsoft.PowerShell.Archive.psm1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1 b/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1 index 7d20fdd..c0cc2a2 100644 --- a/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1 +++ b/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1 @@ -323,7 +323,7 @@ function Expand-Archive else { $createdItem = New-Item -Path $DestinationPath -ItemType Directory -Confirm:$isConfirm -Verbose:$isVerbose -ErrorAction Stop - if($createdItem -ne $null -and $createdItem.PSProvider.Name -ne "FileSystem") + if($null -ne $createdItem -and $createdItem.PSProvider.Name -ne "FileSystem") { Remove-Item "$DestinationPath" -Force -Recurse -ErrorAction SilentlyContinue $errorMessage = ($LocalizedData.ExpandArchiveInValidDestinationPath -f $DestinationPath) @@ -415,7 +415,7 @@ function Expand-Archive { # delete the expanded file/directory as the archive # file was not completely expanded. - $expandedItems | % { Remove-Item "$_" -Force -Recurse } + $expandedItems | ForEach-Object { Remove-Item "$_" -Force -Recurse } } } elseif ($PassThru -and $expandedItems.Count -gt 0) @@ -786,7 +786,7 @@ function ZipArchiveHelper } } - if($entryToBeUpdated -ne $null) + if($null -ne $entryToBeUpdated) { $addItemtoArchiveFileMessage = ($LocalizedData.AddItemtoArchiveFile -f $currentFilePath) $entryToBeUpdated.Delete() @@ -1094,7 +1094,7 @@ function ExpandArchiveHelper { # The ExtractToFile() method doesn't handle whitespace correctly, strip whitespace which is consistent with how Explorer handles archives # There is an edge case where an archive contains files whose only difference is whitespace, but this is uncommon and likely not legitimate - [string[]] $parts = $currentArchiveEntryPath.Split([System.IO.Path]::DirectorySeparatorChar) | % { $_.Trim() } + [string[]] $parts = $currentArchiveEntryPath.Split([System.IO.Path]::DirectorySeparatorChar) | ForEach-Object { $_.Trim() } $currentArchiveEntryPath = [string]::Join([System.IO.Path]::DirectorySeparatorChar, $parts) [System.IO.Compression.ZipFileExtensions]::ExtractToFile($currentArchiveEntry, $currentArchiveEntryPath, $false) From 564a6ba0a2c39cb843af3c02de39a8a8ff0b18ef Mon Sep 17 00:00:00 2001 From: Darrick West Date: Mon, 18 Feb 2019 12:36:00 -0500 Subject: [PATCH 3/4] mrdrwest change System.IO.FileMode::Open to OpenOrCreate to handle hidden files --- .../Microsoft.PowerShell.Archive.psm1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1 b/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1 index c0cc2a2..77aec59 100644 --- a/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1 +++ b/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1 @@ -734,7 +734,7 @@ function ZipArchiveHelper $result = Test-Path -LiteralPath $DestinationPath -Type Leaf if($result -eq $true) { - $fileMode = [System.IO.FileMode]::Open + $fileMode = [System.IO.FileMode]::OpenOrCreate } Add-CompressionAssemblies @@ -804,7 +804,7 @@ function ZipArchiveHelper { try { - $currentFileStream = [System.IO.File]::Open($currentFilePath, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read) + $currentFileStream = [System.IO.File]::Open($currentFilePath, [System.IO.FileMode]::OpenOrCreate, [System.IO.FileAccess]::Read) } catch { @@ -940,7 +940,7 @@ function ExpandArchiveHelper { # The existence of archive file has already been validated by ValidateArchivePathHelper # before calling this helper function. - $archiveFileStreamArgs = @($archiveFile, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read) + $archiveFileStreamArgs = @($archiveFile, [System.IO.FileMode]::OpenOrCreate, [System.IO.FileAccess]::Read) $archiveFileStream = New-Object -TypeName System.IO.FileStream -ArgumentList $archiveFileStreamArgs $zipArchiveArgs = @($archiveFileStream, [System.IO.Compression.ZipArchiveMode]::Read, $false) From f80e2eef36fd9f749a8ef20b717c40617efc8e1a Mon Sep 17 00:00:00 2001 From: Darrick West Date: Mon, 18 Feb 2019 17:05:00 -0500 Subject: [PATCH 4/4] mrdrwest-modified script to process hidden files --- .../Microsoft.PowerShell.Archive.psm1 | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1 b/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1 index 77aec59..b48149a 100644 --- a/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1 +++ b/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1 @@ -138,7 +138,7 @@ function Compress-Archive # if we have write access permission to update the existing archive file. if($archiveFileExist -and $Update -eq $true) { - $item = Get-Item -Path $DestinationPath -Force + $item = Get-Item -Path $DestinationPath if($item.Attributes.ToString().Contains("ReadOnly")) { $errorMessage = ($LocalizedData.ArchiveFileIsReadOnly -f $DestinationPath) @@ -236,7 +236,7 @@ function Compress-Archive } elseif ($PassThru) { - Get-Item -LiteralPath $DestinationPath -Force + Get-Item -LiteralPath $DestinationPath } } } @@ -423,7 +423,7 @@ function Expand-Archive # Return the expanded items, being careful to remove trailing directory separators from # any folder paths for consistency $trailingDirSeparators = '\' + [System.IO.Path]::DirectorySeparatorChar + '+$' - Get-Item -LiteralPath ($expandedItems -replace $trailingDirSeparators) -Force + Get-Item -LiteralPath ($expandedItems -replace $trailingDirSeparators) } } } @@ -734,7 +734,7 @@ function ZipArchiveHelper $result = Test-Path -LiteralPath $DestinationPath -Type Leaf if($result -eq $true) { - $fileMode = [System.IO.FileMode]::OpenOrCreate + $fileMode = [System.IO.FileMode]::Open } Add-CompressionAssemblies @@ -744,10 +744,10 @@ function ZipArchiveHelper # At this point we are sure that the archive file has write access. $archiveFileStreamArgs = @($destinationPath, $fileMode) $archiveFileStream = New-Object -TypeName System.IO.FileStream -ArgumentList $archiveFileStreamArgs - + $zipArchiveArgs = @($archiveFileStream, [System.IO.Compression.ZipArchiveMode]::Update, $false) $zipArchive = New-Object -TypeName System.IO.Compression.ZipArchive -ArgumentList $zipArchiveArgs - + $currentEntryCount = 0 $progressBarStatus = ($LocalizedData.CompressProgressBarText -f $destinationPath) $bufferSize = 4kb @@ -765,7 +765,6 @@ function ZipArchiveHelper { $relativeFilePath = [System.IO.Path]::GetFileName($currentFilePath) } - # Update mode is selected. # Check to see if archive file already contains one or more zip files in it. if($isUpdateMode -eq $true -and $zipArchive.Entries.Count -gt 0) @@ -804,7 +803,7 @@ function ZipArchiveHelper { try { - $currentFileStream = [System.IO.File]::Open($currentFilePath, [System.IO.FileMode]::OpenOrCreate, [System.IO.FileAccess]::Read) + $currentFileStream = [System.IO.File]::Open($currentFilePath, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read) } catch { @@ -825,7 +824,7 @@ function ZipArchiveHelper $srcStream = New-Object System.IO.BinaryReader $currentFileStream $currentArchiveEntry = $zipArchive.CreateEntry($relativeFilePath, $compression) - + # Updating the File Creation time so that the same timestamp would be retained after expanding the compressed file. # At this point we are sure that Get-ChildItem would succeed. $lastWriteTime = (Get-Item -LiteralPath $currentFilePath -Force).LastWriteTime @@ -841,7 +840,7 @@ function ZipArchiveHelper while($numberOfBytesRead = $srcStream.Read($buffer, 0, $bufferSize)) { - $destStream.Write($buffer, 0, $numberOfBytesRead) + $destStream.Write($buffer, 0, $numberOfBytesRead) # can file attributes be specified here $destStream.Flush() } @@ -940,7 +939,7 @@ function ExpandArchiveHelper { # The existence of archive file has already been validated by ValidateArchivePathHelper # before calling this helper function. - $archiveFileStreamArgs = @($archiveFile, [System.IO.FileMode]::OpenOrCreate, [System.IO.FileAccess]::Read) + $archiveFileStreamArgs = @($archiveFile, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read) $archiveFileStream = New-Object -TypeName System.IO.FileStream -ArgumentList $archiveFileStreamArgs $zipArchiveArgs = @($archiveFileStream, [System.IO.Compression.ZipArchiveMode]::Read, $false)