I use .Net Framework 4.0; VS 2015; Ionic.Zip.Reduced (DotNetZip.Reduced) v1.9.1.8. When I try to add a folder to the archive, I get an exception with the text:

The path is too long

Code example:

using (var zipFile = new ZipFile(zipFilePath)) { zipFile.UseZip64WhenSaving = Zip64Option.AsNecessary; zipFile.AlternateEncodingUsage = ZipOption.Always; zipFile.AlternateEncoding = Encoding.UTF8; zipFile.ParallelDeflateThreshold = -1; var dirPath = @"C:\AAAAAAAAAAA\AAAAAA\AAAAAAAAAAAAAAA\AAAAAAAAA\AAAAAAAAAAAAA\AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"; zipFile.AddDirectory(dirPath); <-Exception zipFile.Save(); } 

In the folder is a file named: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.zip

As a result of the execution, the error:

The path is too long

Rewritten to file add to archive (using relative path):

 using (var zipFile = new ZipFile(zipFilePath)) { zipFile.UseZip64WhenSaving = Zip64Option.AsNecessary; zipFile.AlternateEncodingUsage = ZipOption.Always; zipFile.AlternateEncoding = Encoding.UTF8; zipFile.ParallelDeflateThreshold = -1; var dirPath = @"C:\AAAAAAAAAAA\AAAAAA\AAAAAAAAAAAAAAA\AAAAAAAAA\AAAAAAAAAAAAA\AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"; Directory.SetCurrentDirectory(dirPath); var files = Directory.GetFiles(dirPath, "*", SearchOption.AllDirectories).ToArray(); foreach (var fullFilePath in files) { var fileName = Path.GetFileName(fullFilePath); var relatedPath = fullFilePath.Substring(0, fullFilePath.LastIndexOf(fileName, StringComparison.InvariantCultureIgnoreCase)).Replace(zipDir, ""); var relatedFilePath = Path.Combine(relatedPath, fileName); zipFile.AddFile(relatedFilePath); <-Exception } zipFile.Save(); } AAAAAAAAAAA \ AAAAAA \ AAAAAAAAAAAAAAA \ AAAAAAAAA \ AAAAAAAAAAAAA \ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA \"; using (var zipFile = new ZipFile(zipFilePath)) { zipFile.UseZip64WhenSaving = Zip64Option.AsNecessary; zipFile.AlternateEncodingUsage = ZipOption.Always; zipFile.AlternateEncoding = Encoding.UTF8; zipFile.ParallelDeflateThreshold = -1; var dirPath = @"C:\AAAAAAAAAAA\AAAAAA\AAAAAAAAAAAAAAA\AAAAAAAAA\AAAAAAAAAAAAA\AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"; Directory.SetCurrentDirectory(dirPath); var files = Directory.GetFiles(dirPath, "*", SearchOption.AllDirectories).ToArray(); foreach (var fullFilePath in files) { var fileName = Path.GetFileName(fullFilePath); var relatedPath = fullFilePath.Substring(0, fullFilePath.LastIndexOf(fileName, StringComparison.InvariantCultureIgnoreCase)).Replace(zipDir, ""); var relatedFilePath = Path.Combine(relatedPath, fileName); zipFile.AddFile(relatedFilePath); <-Exception } zipFile.Save(); } 

The same error:

The path is too long

I tried to call the Path.GetDirectoryName() method, but it also returns an error:

The specified path, file name, or both are too long. It must be less than 248 characters.

I found many solutions, but I couldn’t make it work (because of the specifics of the application, I cannot switch to the new version of the Framework).

  • Use Framework 4.6.2. Set the option UseLegacyPathHandling=false in App.Config or even Switch.System.IO.UseLegacyPathHandling=false; Switch.System.IO.BlockLongPaths=false Switch.System.IO.UseLegacyPathHandling=false; Switch.System.IO.BlockLongPaths=false
  • Something is mentioned about group policy and the inclusion of the option Configuration> Administrative Templates> System> Filesystem> Enable NTFS long paths , or enable the option using the <ws2:longPathAware>true</ws2:longPathAware>
  • Use the prefix \\?\ In the path (I understand that, too, for the new version of the Framework)
  • Convert the file path to 8.3 format using the GetShortPathName ... function. (error remained)

Maybe someone faced this problem. I will be glad to any advice.

  • It is possible, before adding to the archive, to copy files to a temporary directory with a shorter path, and then add it to the archive from it? - tCode
  • And why, in principle, need such a long way? - sp7
  • @ sp7 This path length already exists, I'm trying to simply archive the data. - androschuk
  • @tCode As an option, only if there are a lot of such files it will be a little expensive. - androschuk
  • @androschuk DotNetZip does not support long paths. Change the library - try github.com/icsharpcode/SharpZipLib , it suddenly pulls a long way. - PashaPash

1 answer 1

This is the problem of the operating system itself, it was fixed literally the other day for windows 10 in the next update.

Read this article here on Habr: https://habrahabr.ru/post/307186/ - in principle, the options you mentioned are well summarized there.

Использовать Framework 4.6.2 - this option is not very good. Not everyone has such a framework, and what to do on other operating systems?

Использовать префикс \\?\ - will be the same errors, if you do not include support for long paths. Yes, only a new framework, alas.

The easiest option is to discard the message to the user that the operating system does not support such long paths, let them think about modifying the directory structure and / or shorten the file names.

Why don't you honestly want to catch the exception and honestly handle it? Even if you find a temporary folder where one specific name will fit in - tomorrow there will be another three characters longer and not enough again.