2

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.

androschuk.a
  • 289
  • 10
  • 22
  • 1
    Have you tried using a path shorter than 248 characters? – James Gould Aug 12 '16 at 08:21
  • @JayGould Yes. Works fine. – androschuk.a Aug 12 '16 at 08:24
  • Sorry I was being sarcastic. If your directory is over 248 characters there's not work around, you need a directory that isn't over the limit. – James Gould Aug 12 '16 at 08:25
  • I can't change catalog. I try add to archive user folder. There can be any directory structure. Is that where that copy to a temporary folder. However, the data may be several gigabytes – androschuk.a Aug 12 '16 at 08:28
  • Well, you basically have two options - one is to iterate the folder structure yourself and build the catalog manually instead of relying on the `ZipFile.AddDirectory`, another to find a different zip library (you could try the one included in .NET). Sadly, many applications still don't support long paths, so a lot of the infrastructure has to be very conservative. – Luaan Aug 12 '16 at 08:39

1 Answers1

4

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.

Mauro Sampietro
  • 2,739
  • 1
  • 24
  • 50