Powershell Expand-archive problem.


  1. Posts : 502
    windows 10 (x64) Home 20H2 19042.844
       #1

    Powershell Expand-archive problem.


    I have a line as below in my script.
    Expand-Archive .\PR.zip -DestinationPath .\

    But it gives an error as below and does not do expand archive.
    ===
    calling ".ctor" with "3" argument(s): "End of Central Directory record could not be found."
    At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell .Archive.psm1:934 char:23
    + ... ipArchive = New-Object -TypeName System.IO.Compression.ZipArchive -Ar ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

    ===

    What should I do to make expand-archive to work?
      My Computer


  2. Posts : 3,453
       #2

    It's due to the compression type not being recognised.

    Try this - it should extract most types of compressed files

    Code:
    function Extract-Cab ($CabFilePath, $Destination){
        $Shell = New-Object -Comobject "Shell.Application"
        $SourceCab = $Shell.Namespace($CabFilePath).items()
        $DestinationFolder = $Shell.Namespace($Destination)
        $DestinationFolder.CopyHere($SourceCab)
    }
    
    Extract-Cab -CabFilePath .\PR.zip -Destination .\
    Just realised - it won't resolve relative paths - you will need to call Convert-Path

    Here's another example that works with short paths

    Code:
    Param($CabFilePath, $Destination)
    
    $Shell = New-Object -Comobject "Shell.Application"
    $SourceCab = $Shell.Namespace((Convert-Path $CabFilePath)).items()
    $DestinationFolder = $Shell.Namespace((Convert-Path $Destination))
    $DestinationFolder.CopyHere($SourceCab)
    Eg:
    .\ExtractCab.ps1 -CabFilePath .\PR.zip -Destination .\
    Powershell Expand-archive problem. Attached Files
    Last edited by Superfly; 06 Jan 2020 at 12:38.
      My Computer


  3. Posts : 502
    windows 10 (x64) Home 20H2 19042.844
    Thread Starter
       #3

    Superfly said:
    It's due to the compression type not being recognised.

    Try this - it should extract most types of compressed files

    Code:
    function Extract-Cab ($CabFilePath, $Destination){
        $Shell = New-Object -Comobject "Shell.Application"
        $SourceCab = $Shell.Namespace($CabFilePath).items()
        $DestinationFolder = $Shell.Namespace($Destination)
        $DestinationFolder.CopyHere($SourceCab)
    }
    
    Extract-Cab -CabFilePath .\PR.zip -Destination .\
    Just realised - it won't resolve relative paths - you will need to call Convert-Path

    Here's another example that works with short paths

    Code:
    Param($CabFilePath, $Destination)
    
    $Shell = New-Object -Comobject "Shell.Application"
    $SourceCab = $Shell.Namespace((Convert-Path $CabFilePath)).items()
    $DestinationFolder = $Shell.Namespace((Convert-Path $Destination))
    $DestinationFolder.CopyHere($SourceCab)
    Eg:
    .\ExtractCab.ps1 -CabFilePath .\PR.zip -Destination .\
    I get msg as
    D:\Data\Lvg\backup\bin\ExtractCab.ps1 is not digitally signed. What to do?
      My Computer


  4. Posts : 3,453
       #4

    lvgandhi said:
    I get msg as
    D:\Data\Lvg\backup\bin\ExtractCab.ps1 is not digitally signed. What to do?
    Run this in PS as Admin:

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
      My Computer


  5. Posts : 502
    windows 10 (x64) Home 20H2 19042.844
    Thread Starter
       #5

    Superfly said:
    Run this in PS as Admin:

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
    I ran this as admn in powershell. Still I get the same error when running Extract-Cab
      My Computer


  6. Posts : 3,453
       #6

    See if the file is blocked - right clicking the .PS1 file, Properties and choosing Unblock.

    Or try other execution policies eg -ExecutionPolicy Bypass

    You can send me your script and I'll add the function to it.

    The file I uploaded is just to test.




      My Computer


  7. Posts : 502
    windows 10 (x64) Home 20H2 19042.844
    Thread Starter
       #7

    Superfly said:
    See if the file is blocked - right clicking the .PS1 file, Properties and choosing Unblock.

    Or try other execution policies eg -ExecutionPolicy Bypass

    You can send me your script and I'll add the function to it.

    The file I uploaded is just to test.


    Thanks. Now Expand-Archive is working. Still a puzzle why it was not working yesterday , but working at 12 noon IST. After 5 hrs, it did not work. Now again it works. .

    - - - Updated - - -

    Now I have found problem is with downloaded file, not with expand-archive.
    When I download a file using firefox browser its size is as follows
    PS D:\Data\Downloads> dir PR0*


    Directory: D:\Data\Downloads


    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    -a---- 08/01/2020 19:41 312568 PR080120.zip

    I used this code below to download file.
    $url = "https://www.nseindia.com/archives/equities/bhavcopy/pr/$PR"
    $output = "PR.zip"
    Invoke-WebRequest -Uri $url -OutFile $output

    file size as below.
    PS D:\Data\Lvg> dir PR*


    Directory: D:\Data\Lvg


    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    -a---- 08/01/2020 20:08 180410 PR.zip

    7zip is able to unzip direct download. when try to unzip file downloaded by PS script I get comment it is not archive file.
    This happens only for last 3 days. Previously there was no problem.
    How to download file with PS to get file same as direct download.
    Last edited by lvgandhi; 07 Jan 2020 at 08:23.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 01:44.
Find Us




Windows 10 Forums