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

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

我使用.Net Framework 4.0;对比 2015; Ionic.Zip.Reduced (DotNetZip.Reduced) v1.9.1.8。当我尝试将文件夹添加到存档时,出现文本异常:

The path is too long

示例代码:

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();
}

文件夹中有一个名为:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.zip

的文件

由于错误:

The path is too long

在基于文件的存档中重写(使用相对路径):

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 path is too long

我试图调用 Path.GetDirectoryName() 方法,但它也 returns 一个错误:

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.

我找了很多解决方案但是弄到手都没用(因为具体应用到新版Framework'a下不了)。

也许有人遇到过这样的问题。我很乐意接受任何建议。谢谢。

如果您的路径太长,您将无能为力。即使您可以将 Windows 限制更进一步,您的应用程序在那种情况下也无法在非临时配置的系统上正常运行。

您可以将必须​​使用的文件复制到 C:\temp 等临时文件夹,然后从那里将文件添加到存档。

您甚至可以模仿相同的文件夹树结构,目录名称仅由 1 或 2 个字母组成,然后将完整(但实际上更短)的目录路径映射到某处(例如文件上)的原始路径,因此以后可以用相同的名称重建原始文件夹树结构。