Search code examples
c#.netdotnetzip

When I try to add a folder to the archive get an error: the path is too long


I using .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 get an exception with the text:

The path is too long

Sample code:

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 an error:

The path is too long

Rewritten in the file-based addition to the archive (using a 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();
}

The error is the same:

The path is too long

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

The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

I found a lot of solutions but to get to work and did not work (because of the specifics of the application to the new version Framework'a can not go).

  • Use Framework 4.6.2. Set UseLegacyPathHandling = false option in App.Config or Switch.System.IO.UseLegacyPathHandling = false; Switch.System.IO.BlockLongPaths = false
  • With the mention of a Group Policy and the inclusion of the option Configuration> Administrative Templates> System> Filesystem> Enable NTFS long paths, or to enable the option via the manifest <ws2:longPathAware>true</ws2:longPathAware>
  • Use the prefix \\?\ In the path (I understand that for the new version of Framework)
  • Convert path to the file in 8.3 format using GetShortPathName function .... (Error remains)

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


Solution

  • If your path is too long there's not much you can do about it. Even if you can move Windows limits a step further your application won't work well on a non ad-hoc configured system in that scenario.

    You can workaround copying the files you have to work with to a temp folder like C:\temp and add the files to the archive from there.

    You can even mimic the same folder tree structure with directory names composed of only 1 or 2 letters and then map the complete (but really shorter) directory path to the original path somewhere (on a file for example), so that you can rebuild the original folder tree structure with the same names later on.