0

I'm extracting a medium sized ZIP file which contains thousands of tiny files with different names. Some of these files have names which include illegal character like ":". This causes Windows to be rather unhappy when it tries to extract them and copy them to the destination folder.

It throws Error 0x80070057. Some sources online calim this is due to a lack of disk space, but that is definitely not true in my case. This error is most liekly due to the file containing illegal caharcters in its name, as the other files copy with no problems.

Windows then asks me to Try Again or Skip, once I select try again, it simply replaces any illegal characters with "_" which fixes the porblem just fine.

My question is how do I prevent Windows from asking me thousands of times? I tried checking the Do this for all current items checkbox, but that has no effect.

This question is completely different from this one. One because I am not using 7Zip and instead am using Windows built in extracter. Two, because that question is about resolving name conflicts and this question is about removing illegal characters from file names.

11
  • You might get more info on the error here. Commented Sep 16, 2023 at 4:42
  • 1
    And it looks like via PowerShell, you could examine individual FileArchiveEntry names and change the destination file name. Would that work??? Commented Sep 16, 2023 at 4:48
  • @KeithMiller I now know for a fact that the error is due to illegal characters being present in the file entry names in the ZIP file. However, I have no experience using .NET framework and I'm not sure how to subsitute the illegal characters, while also copying the files.
    – YEp d
    Commented Sep 16, 2023 at 5:22
  • One of the most powerful things about PowerShell is that it can access the entire .net framework to scripting via the Add-Type cmdlet. Let me play with this (as I only found this when your question aroused my curiosity) and I'll post an answer when I have some sample code. Commented Sep 16, 2023 at 10:46
  • Apparently, they thought my question was too similar to some unrelated one, thank you for your help though. Hopefully, this question will be reopened and I can see your solution!
    – YEp d
    Commented Sep 16, 2023 at 13:03

1 Answer 1

-1

.Net prvides several classes within the System.IO.Compression namespace. They provide varous methods that give you a higher level of control over creation, modification, and extraction.

To convert any illegai filenames to legal filenames during extration, the methods/properties used will be:


Edit: PowerShell code to correct illegal filenames during extraction

Since you found a Python solution that worked for you, I took my time playing with these classes, as they're new to me. But for the sake of the curious and future readers, here's straight PowerShell/.net code.

This code opens a .zip file as a ZipArchive and replaces any illegal characters in the Name/FullName of each ZipArchiveEntry with the underscore(_) character prior to extraction.

Input is hard-coded here to keep the focus on the file manipulation, but the code can serve as the basis for a function or context menu entry.

###   User Input   ###
$ZipPath        = 'C:\Users\Whoever\Wherever\Illegal.zip'   ###   Path to .zip file
$ExtractPath    = 'C:\Users\Whoever\NonExisting'            ###   Path of target directory

###   Load required assemblies
Add-Type -AssemblyName System.IO.Compression
Add-Type -AssemblyName System.IO.Compression.FileSystem

###   Construct regex character class to match illegal characters
$IllegalChars   = '[{0}]' -f ( -join [IO.Path]::GetInvalidFileNameChars().ForEach{[Regex]::Escape($_)})

###   Create target directory if it doesn't exist
If (!(Test-Path $ExtractPath))
{
    mkdir $ExtractPath -Force | out-null
}

###   Ensure ExtractPath is fully-qualified for subsequent operations
$ExtractPath    = [IO.Path]::GetFullPath($ExtractPath)

$Archive        = [IO.Compression.ZipFile]::OpenRead($ZipPath)

$Archive.Entries | ForEach{
    If ($_.Name -eq $_.FullName)   ###   no subdirectory
    {
        [IO.Compression.ZipFileExtensions]::ExtractToFile($_, (Join-Path $ExtractPath ($_.Name -replace ($IllegalChars,'_'))))
    }
    Else                           ###   subdirectory 
    {
        $ChildPath   = $_.FullName.Split('/') -replace ($IllegalChars,'_') -join [io.Path]::DirectorySeparatorChar
        $subFolder   = Split-Path $ChildPath
        If (!(Test-Path $subFolder))
        {
            mkdir $subFolder -Force | out-null
        }
        [IO.Compression.ZipFileExtensions]::ExtractToFile($_, (Join-Path $ExtractPath $ChildPath))
    }
}

4
  • In progress -- but have to break and didn't want to risk losing links/formatinng... :D Commented Sep 19, 2023 at 0:12
  • I wrote a Python script to extract the files, apparently the Python zipfile library just replaces any illegal characters silently unlike Windows. Anyways, your answer will still be helpful, since not everyone has Python installed and realistically there should be some much easier way to just extract a damn ZIP file with illegal characters in it. Maybe in just one command?
    – YEp d
    Commented Sep 19, 2023 at 1:24
  • Dowh-voted with no explanation makes corrections or improvements impossible. Should I delete??? Commented May 15 at 17:30
  • 1
    I have no idea who down-voted you, I will accept your answer since it works right out of the box on a fresh Windows install.
    – YEp d
    Commented Jun 2 at 2:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .