简体   繁体   中英

How to expand ZIP archive using PowerShell (UTF-8 filenames)

My zip archive has a single file:

Père-Noël.txt

The zip expands nicely with Windows File Explorer, 7-Zip or any other tool I've tried. But I cannot figure out how to do it from PowerShell. Obviously I've tried Expand-Archive but it cannot handle the file name and garbles it into PŠre-N”el.txt . Note: The problem isn't specifically with this example, but indeed with any file name which uses characters outside of the ASCII-127 range. Or so it seems.

Any solution which uses PowerShell and which doesn't rely on an external tool - whose presence cannot be guaranteed - will be accepted. Windows 10 is the platform. I cannot do system-level changes and cannot rely on users of the script having any specific global setting on their system. It has to be a solution within the script.

Is there another way, besides Expand-Archive ? Or is there a setting in PowerShell which will magically do the trick?

Steps to reproduce:

On your Windows 10 host:

  1. Create an empty file named Père-Noël.txt .

  2. ZIP the file using Windows Explorer ("Compressed Folders" feature) into an ZIP archive of your choice, say myarchive.zip .

  3. Delete the Père-Noël.txt file.

  4. Now try to unpack the myarchive.zip using PowerShell. This operation should create the file Père-Noël.txt again.

Compressing using PowerShell Compress-Archive cmdlet

True, if the ZIP was originally created using Compress-Archive cmdlet then it actually works as intended when decompressing using Expand-Archive . So you can say that PowerShell is compatible with itself. It is just not compatible with Windows Explorer ZIPs.

You'll likely need to check the encoding [System.Text.Encoding]::GetEncodings() but the below works with your example Père-Noël

$zipfile = 'C:\test\Père-Noël.zip' #Contains Père-Noël.txt
$outpath = 'C:\test\out'
$enc = [System.Text.Encoding]::GetEncoding(29001) #29001, x-Europa, Europa
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath, $enc)

Hope this helps,

Although we arrived in 2021 I stumbled upon the same problem. Like the accepted answer my solution is based on the System.IO.Compression namespace. The expand-archive command accepts pipeline-input and a -Force switch. I had the same goals for my implementation - still in work and not thoroughly tested.

$encoding = [System.Text.Encoding]::GetEncoding(437)
Write-Output $encoding
Get-ChildItem -Path ".\*.zip"  | Unzip -target "C:\unzipped" -f -encoding $encoding -v

Find unzip function here

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM