Bartosz
Bartosz

Reputation: 4766

Check if zip archive contains an empty folder by name using .net ZipArchive class

Is there any way to determine if a .net 4.5 ZipArchive contains an empty folder with a specified name?

As far as I see, there is only a collection of entries that ignores empty folders.

Upvotes: 0

Views: 1527

Answers (1)

Jakob
Jakob

Reputation: 1216

I don't have the problem where empty folders are being ignored, the following works for me:

string zipPath = @"C:\test.zip";

using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
    /* Find all folders (elements ending with "/"), and exclude 
    * those containing elements (FullName starts with folder name, FullName is
    * longer than folder name) */
    var entries = archive.Entries.Where(o1 => o1.FullName.EndsWith("/")
        && !archive.Entries
        .Any(o2 => o2.FullName.StartsWith(o1.FullName) 
            && o2.FullName.Length > o1.FullName.Length)).ToList<ZipArchiveEntry>();
        } 

Upvotes: 2

Related Questions