Reputation: 23
I'm attempting to zip up files that are very long. I've read other posts that state that you should prefix long filenames with '\\?\' in order for this to work. This certainly allows the zipping process to complete properly. However, upon unzipping the file I am given the error "invalid file \\?\myfile.txt". Which means it is literally zipping my files with a pre-pended long filename indicator. Can zipping be done at all with long file names?
using (ZipArchive zip = ZipFile.Open(zipfinalpath, ZipArchiveMode.Create))
{
for (int i = 0; i < fullfilepaths.Count; i++)
{
zip.CreateEntryFromFile(fullfilepaths[i], zipfilepaths[i]);
}
}
Upvotes: 2
Views: 1122
Reputation: 2184
The maximum number of characters in a Windows file name is limited to 260 characters. In .NET 4.6.2 and later, the PathTooLong
exception is no longer thrown. So your code is working correctly. Read more here.
In 4.6.2 we will no longer throw PathTooLongException if we see a path that is >= MAX_PATH. If the OS doesn't like it we'll surface whatever the OS returns as an error (which may be PathTooLong), but we won't second guess what the OS will do.
In the Local Group Policy Editor, you can "Enable Win32 long paths" and then it won't be truncated.
There is library written to overcome these issues. See the Delimon.Win32.IO library.
Per the library information:
This Library is written specifically to overcome the limitation of the .NET Framework to use long Path & File Names. With this Library you can programmatically browse, access, write, delete, etc Files and Folders that are not accessible by the System.IO namespace. Delimon.Win32.IO replaces basic file functions of System.IO and supports File & Folder names up to up to 32,767 characters. This Libarary is written on .NET Framework 4.0 and can be used either on x86 & x64 systems.
Upvotes: 1