Handling PathTooLongException

Aug 3, 2011 at 3:16 PM

You are right about replying to two year old discussions, my miss. By the way, congrats for the new release. Just downloaded it.

Original Post:

Hey Cheeso,

first things first, dotNetZip is awesome. I recently implemented a backup & restore function. Works like charm.

When debugging the code I also encountered the PathTooLongException, using ZipFile.AddDirectory() to save a zip. Problem here, the resulting exception doesn't even say which path was to long or whatever. Using the DotNetZip devkit I saw this happened in ZipEntry.Create when File (Directory) will be first accessed with File.GetLastWriteTime(filename) (line 2402). Well, the path really was too long :).

On the other hand two functions in dotNetZip could be helpfull.

1. throw custom Exception that actually tells you the path.

2. A flag like IgnoreInvalidPaths (perhaps combined with an event InvalidPathEncountered) could help scenarious were you just want to zip what you get out of a directory. 

Just a suggestion. Thx again, saved me tons of time.

Aug 3, 2011 at 3:38 PM

Thanks again for the kind words. I'm glad you like the library.

You said you just downloaded the new release.  What version of the library were you using when you encountered the problems you are reporting?

Can you give me the full exception stacktrace on the "path too long" exception?  Even better, can you give me a simple test case that reproduces the problem?  What does it mean "the path really was too long"?  How long was the path? 

Also I am interested in your suggestion, but... I don't know what you mean by  "scenarious were you just want to zip what you get out of a directory".   Can you elaborate on that?  How is it different than what dotnetzip does today?





Aug 6, 2011 at 5:30 PM

The exception I got zipping a directory was the System.IO.PathTooLongException. - http://msdn.microsoft.com/de-de/library/system.io.pathtoolongexception.aspx

A subdirectory of the directory I wanted to zip had a path longer than 259 characters (267 :) ), apparently the maximum path length the .NET IO system can handle. So the error was due to the .net limit. I found a nice blog post about this and how to use Win32 to access file/folders with >260 path length. 


If you zip such a directory, the whole process fails. What really bothered me was to find out which file/directory was too long, because the PathTooLongException doesn't return the path value. So it would be nice if you would catch this PathTooLongException and return a custom exception that tells u the path.

What I meant with "scenarious were you just want to zip what you get out of a directory", that u cannot use the ZipFile.AddDirectory() if one of the subdirectories or files exceeds the 259 character limit.  If there would be flag like IgnoreInvalidPaths, everything would be zipped except pathes that are too long or u got no access permission or whatever. 

On the other hand u could try using the Win32 api to zip/unzip pathes with length up to 32k. I admit, most dotnetzip users perhaps never encounter this limit, so I don't know if it's worth the trouble using Win32 calls. 

Aug 6, 2011 at 6:19 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.