How to approach a situation when you have a simple zip like file.zip
, but inside you have a file which is longer than the Microsoft's allowed 260 characters (it is actually little bit less but that does not matter in our case)?
If you get from some source (like Google) a file which is, for example, 400 characters long.
How to properly unzip it via powershell in Windows (preferably 7 -> powershell 4.x)?
You could do it via some unzip tool and use the unicode path trick (\\?\
) like:
unzip test.zip -d \\?\c:\<path>
I would like to do the same via powershell script (version 4.x).
Below is my script (inspired by some others), which normally works.
# .NET Framework 4.5 required (at least PowerShell -Version 2)
Add-Type -AssemblyName System.IO.Compression.FileSystem # -ErrorAction Stop
$source_zip_file= 'C:\<path>\file.zip'
$destination_directory = 'c:\t\unpack'
# Does NOT support long path!!! with
# $destination_directory = '\\?\c:\t\unpack'
$overwrite_destination = $true
$archive = [IO.Compression.ZipFile]::OpenRead($source_zip_file)
foreach ($entry in $archive.entries) {
# better than Join-Path when spaces are present
$entry_file_path = [System.IO.Path]::Combine($destination_directory, $entry.FullName)
$entry_directory = [System.IO.Path]::GetDirectoryName($entry_file_path)
# create destination directory if not present
If (!(Test-Path $entry_directory)) {
New-Item -ItemType Directory -Path $entry_directory | Out-Null
}
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($entry, $entry_file_path, $overwrite_destination)
}
How to expand it to work with long path \\?\c:
?
Edit - sample file
https://anonfile.com/p94fG6d7b7/1.zip
The error message generated:
Exception calling "GetDirectoryName" with "1" argument(s): "The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the dir ectory name must be less than 248 characters."
Edit due to comments
I'm aware that it will not be easy task and the .NET support is grody.
My idea was that it would be possible like here on the Hey, Scripting Guy! Blog Weekend Scripter: Remove a Long Path File via Windows API.