1

I'm trying to zip specific files however on some files powershell breaks due to the charachter limit of 256, I know I can can alter this y changing registry keys. But I kind of want to ignore this error and write it to a file and zip this error file into the created archive.

However it seems whenever I run this code and an errors appears it does not continue with the code but rather breaks it midway.

If anyone could help it would be much appriciated. Here is the code:

try { Add-Type -assembly "system.io.compression.filesystem"; 
                [io.compression.zipfile]::CreateFromDirectory('C:\Windows\Temp\XYZ\directoryToZip', 
                'C:\zippedXYZ.zip')} 
catch { Write-Output $_ | Out-File -Append -FilePath C:\Windows\Temp\XYZ\Error.txt;
                Compress-Archive -Update C:\Windows\Temp\XYZ\Error.txt C:\zippedXYZ.zip}
Kirito
  • 49
  • 1
  • 6
  • Yes, this happens with methods, you can ignore the errors but in any case it will break the pipeline. What's the problem with using `Compress-Archive` as your first option? – Santiago Squarzon Jan 10 '23 at 14:16
  • If i recall correctly there is a limit on that command where i could only zip up to 2GB, the files I zip could be larger then this – Kirito Jan 10 '23 at 14:19
  • 1
    this is true only for powershell 5.1, if you're running latest powershell version the problem has been solved already. if you must run your code in 5.1 you can use the function posted here: https://stackoverflow.com/questions/72607926/how-do-i-compress-files-using-powershell-that-are-over-2-gb/72611161#72611161 – Santiago Squarzon Jan 10 '23 at 14:20
  • 2
    Alternatively - and I don't know if it'll make a difference - you can try with the long-path opt-in prefix: ``'\\?\C:\Windows\Temp\XYZ\directoryToZip'`` – mklement0 Jan 10 '23 at 15:53
  • 1
    Thanks, @mklement0. I'd seen the `\\?\C:` syntax several times, but didn't think to look it up until now: [what-does-mean-when-prepended-to-a-file-path](https://stackoverflow.com/questions/21194530/what-does-mean-when-prepended-to-a-file-path) – Rich Moss Feb 02 '23 at 22:37

0 Answers0