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
inApp.Config
or evenSwitch.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.