0

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.

tukan
  • 17,050
  • 1
  • 20
  • 48
  • 1
    can you provide a link to this long path zip? – Thom Schumacher Feb 28 '18 at 15:04
  • @thomschumacher of course, will update the question. – tukan Feb 28 '18 at 15:13
  • I found this link that provides some insights into things you could try in lieu of using System.io Classes from dot net: https://social.technet.microsoft.com/wiki/contents/articles/12179.net-powershell-path-too-long-exception-and-a-net-powershell-robocopy-clone.aspx – Thom Schumacher Feb 28 '18 at 15:40
  • IO in .NET only recently gained the option to use long paths. AFAIK it's actually disabled in the IO implementations so that paths with `\\?\` don't work. – Joey Feb 28 '18 at 15:49
  • Have you tried 7zip? https://stackoverflow.com/questions/42998669/unzip-file-using-7z-in-powershell/43044267 – HeedfulCrayon Feb 28 '18 at 17:48
  • @HeedfulCrayon this is not an issue of unpacking it. I can unpacking it by different means. I want to extract it via powershell. – tukan Mar 01 '18 at 14:49
  • @Joey yes it is. Would it be possible via Windows API? Please see the question update – tukan Mar 01 '18 at 15:04

0 Answers0