none
Creating ZIP archives with PowerShell and Windows tools RRS feed

  • Question

  • I would like to backup data of client computers with PowerShell. To do so I create a ZIP archive of the requested data which basically is no problem at all. But when it comes to special characters which use Unicode or the users build very long path names I run into trouble. I have tried different things now but haven’t found a solution yet. My client computers run Windows 10 (Build 1511). Installing the anniversary update (Build 1607) isn’t a solution due to other dependencies. Also the usage of 3rd party software to create ZIP files can’t be one.

     

    Below are three methods I’ve found and tried already. They all have the same problems in common: when it comes to long path names they immediately stop the execution or they just skip the rest of the folder structure.

     

    1. Create a ZIP file with PowerShell V5 CmdLet Expand-Archive

    $Target = "C:\Temp\Test.zip"

    $Source = "C:\Test"

    Compress-Archive -Path $Source -DestinationPath $Target

     

    2. Create a ZIP file with .net class

    Add-Type -assembly "system.io.compression.filesystem"

    $Target = "C:\Temp\Test.zip"

    $Source = "C:\Test"

    [io.compression.zipfile]::CreateFromDirectory($Source, $Target);

     

    3. Create a ZIP file with Windows Explorer (Compressed Folders)

    $Source = Get-ChildItem "C:\Test" -Recurse

    $Target = "C:\Temp\Test.zip"

     

    if (-not (Test-Path $Target)) {

     Set-Content $Target ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))

     (dir $Target).IsReadOnly = $false 

    }

    $objShell = New-Object -Com Shell.Application

    $objZIP = $objShell.NameSpace($Target)

     

    foreach($File in $Source) {

     $objZIP.CopyHere($File.FullName)

     Start-sleep -milliseconds 500

    }

     

    Then I found out, that it should be possible to access the local drives via UNC namespace which could look like “\\?\C:\Test”. But this doesn’t work with my Windows 10 build 1511 version. With build 1607 it is possible. What I don’t understand: why? I tried it with installing the latest .net version 4.6.2 on build 1511 but the problem still exists.

     

    Can anybody help me with the access to (local) UNC namespaces or creating ZIP archives?


    Wednesday, October 26, 2016 3:02 PM

Answers

  • Your problems are:

    1. You say you can't use a freeware downloaded tool like 7-Zip, but you don't say why. This is probably the simplest solution.

    2. We have known for many years now that the .NET framework doesn't support very long path names. Workarounds include substituting drives and the like. This means you can't use the compression cmdlets or the .NET classes because the limitation is in the underlying .NET code.

    3. Your #3 is a non-starter because it is simply not designed for automation - you're trying to use GUI objects in a script. The main problem with it is there's no error checking, and when you have to manually insert pauses to try to get things to work, you're asking for trouble. (It is not designed for scripting.)

    The best solution, if you need this functionality, is to get approval for a zip application that does not suffer from these problems.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, October 26, 2016 3:09 PM
    Moderator

All replies

  • Your problems are:

    1. You say you can't use a freeware downloaded tool like 7-Zip, but you don't say why. This is probably the simplest solution.

    2. We have known for many years now that the .NET framework doesn't support very long path names. Workarounds include substituting drives and the like. This means you can't use the compression cmdlets or the .NET classes because the limitation is in the underlying .NET code.

    3. Your #3 is a non-starter because it is simply not designed for automation - you're trying to use GUI objects in a script. The main problem with it is there's no error checking, and when you have to manually insert pauses to try to get things to work, you're asking for trouble. (It is not designed for scripting.)

    The best solution, if you need this functionality, is to get approval for a zip application that does not suffer from these problems.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, October 26, 2016 3:09 PM
    Moderator
  • There is no substitute for a real backup solution.  Many companies provide excellent software that should be used in a corporate environment.  Don't waste time re-inventing the wheel.  It is not worth it.  ZIP files are not considered robust or recoverable mechanisms for a backup.


    \_(ツ)_/


    • Edited by jrv Wednesday, October 26, 2016 3:30 PM
    Wednesday, October 26, 2016 3:28 PM
  • Hello Bill,

    thanks for your prompt response. I hope I can clarify things with this one.

    1) Freeware tools are not allowed by company policy.

    2) I understand that. However, my tool works perfectly with UNC paths on Windows 10 build 1607. My aim is now to get this working under Windows 10 build 1511. Although I installed the .net version 4.6.2 it still does not work.

    3) Ok, thank you for your explanation.


    Wednesday, October 26, 2016 3:40 PM
  • If it doesn't work, it doesn't work. Not sure what else we can tell you.

    If this is a requirement, you will need to get a waiver for your company policy and install something that works.

    Or, as jrv suggested, don't use zip files and invest in a more robust backup solution.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, October 26, 2016 3:45 PM
    Moderator