1

In GNU/Linux, I can just do this:

tar cfz hierarchy.tgz hierarchy

and it just works, even if some of the items in hierarchy are being used by other processes.

In Windows, I can right-click on a folder and send it to a compressed zip file, so long as it's in another place, like the Desktop, and that works.

But in Windows PowerShell, something you would think would be so simple, isn't:

PS E:\> Compress-Archive -Path e:\lib -DestinationPath .\e-lib-all.zip
ZipArchiveHelper : The process cannot access the file
'E:\lib\company\data\data-azure-java\1.0.1\data-azure-java-1.0.1.jar' because it is being used by another process.
At
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:697
char:30
+ ... sArchived = ZipArchiveHelper $subDirFiles.ToArray() $destinationPath  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : PermissionDenied: (E:\lib\company...-java-1.0.1.jar:String) [Write-Error], IOException
+ FullyQualifiedErrorId : CompressArchiveUnauthorizedAccessError,ZipArchiveHelper

New-Object : Exception calling ".ctor" with "1" argument(s): "Stream was not readable."
At
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:808
char:38
+ ...     $srcStream = New-Object System.IO.BinaryReader $currentFileStream
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

I asked a coworker, what's the PowerShell equivalent of the GUI way that just works? He thought what the GUI does in the background is create a copy using service level rather than user permissions, and then zips that.

Is there a PowerShell way to do this? I've tried some other things that did not work, namely:

Copy-Item -Path "E:\lib" -Force -PassThru | `
  Get-ChildItem | `
  Compress-Archive -DestinationPath "E:\e-lib-all.zip"

Get-ChildItem -Recurse -Path $sourcePath | Compress-Archive -DestinationPath $destinationPath
Get-ChildItem -Recurse -Path "E:\lib"    | Compress-Archive -DestinationPath "E:\e-lib-all.zip"

Yes, I looked at previous SO post Compress-Archive error: PermissionDenied but it didn't help (I got the same errors).

gknauth
  • 2,310
  • 2
  • 29
  • 44
  • Following the thread. ANd also will see if there is anything I can help with. – Ranadip Dutta Oct 19 '22 at 17:48
  • 1
    Could you give [this function](https://stackoverflow.com/a/72611161/15339544) a try ? I believe the key is opening the FileStream with ReadWrite FileShare permissions. I tested while writing to a file and works for me – Santiago Squarzon Oct 19 '22 at 18:01

0 Answers0