6

I am extracting a zip file like this

ZipFile.ExtractToDirectory(zipFile, extractTo);

But I am getting a

{"Could not find a part of the path 'C:\\....many subfolders\\Extremely long filename'."}

The zip file contains a file with a very long path and filename c.a. 280 chars in total. I am not sure if that is the problem I enabled long path as demonstrated here https://blogs.msdn.microsoft.com/jeremykuhne/2016/07/30/net-4-6-2-and-long-paths-on-windows-10/

If I open the path in explorer it opens but the file with the long filename is not there. If I open the zip file the file is there so there seems to be a problem extracting file with long filename.

Is it possible to skip a file during the zip extraction or allowing extracting files with long filenames?

Agnel Amodia
  • 765
  • 8
  • 18
doorman
  • 15,707
  • 22
  • 80
  • 145
  • 1
    ZipFile.OpenRead – Steve Mar 08 '18 at 16:17
  • 1
    Note that skipping files is quite a dangerous thing to do, especially if you don't tell the user you've done that. – DavidG Mar 08 '18 at 16:29
  • Hi @DavidG yes I totally agree. It would be best if all the files could be extracted. Any clue why the file is not being extracted why the error is thrown? – doorman Mar 08 '18 at 16:33

2 Answers2

2
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
    foreach (ZipArchiveEntry entry in archive.Entries)
    {
        if (entry.FullName.Length > 280)
            continue;

        entry.ExtractToFile(Path.Combine("your path", entry.FullName));
    }
} 

ZipFile.OpenRead will allow you to inspect the content of the zip file. Then you can enumerate thru the .Entries property to find out all the files inside the archive and decide if you want to proceed with extract based on the file name length .

Steve
  • 11,696
  • 7
  • 43
  • 81
  • @CamiloTerevinto I think the code is self explanatory enough. Skip a file if the name is too long and proceed extract if its not as described in the question – Steve Mar 08 '18 at 16:27
  • Thanks @Steve for the solution. Of course it would be best if all the files could be extracted. Any clue why the file with a long name is not being extracted? Is there a limitation in the ZipFile archive? – doorman Mar 08 '18 at 16:32
  • @doorman there is a limitation on Windows (260 chars) and you can't bypass that. – Steve Mar 08 '18 at 16:33
  • Even though I allow it? Maybe I need to restart? https://blogs.msdn.microsoft.com/jeremykuhne/2016/07/30/net-4-6-2-and-long-paths-on-windows-10/ – doorman Mar 08 '18 at 16:34
  • 1
    Why is this marked as a solution? I am getting the same issue and I can't just skip extracting a file nor do I have control over how the zipped directories are named. There should be an actual solution and not "just skip it". – Dobob Apr 30 '21 at 20:49
  • @Dobob Windows has a restriction on path length. So you either skip it or rename it. There really isn't a 3rd way. – Steve Apr 30 '21 at 21:30
1

If you are using a Windows based file system, the maximum length of a filename is 255 characters. Note: the folder path is included in the file name when calculating the file name length. If you have a long folder path, then you might want to try extracting to c:\temp, which is only using 7 characters. Thus leaving you with 248 characters to work with. If you have file names longer than that within your archive, you might want to address that issue first that way your not having to exclude files from extract.

https://msdn.microsoft.com/en-us/library/windows/desktop/ee681827(v=vs.85).aspx

http://www.ntfs.com/ntfs_vs_fat.htm

Maximum filename length in NTFS (Windows XP and Windows Vista)?

J Weezy
  • 3,507
  • 3
  • 32
  • 88