Exclude all directories with a specific name

Dec 22, 2011 at 3:30 AM

I've a folder called Development. In that folder I've a lot of Visual Studio solutions. I like to zip my Development folder; however, I don't want to include the bin folders and obj folders in the zip file. Is this possible? 

Coordinator
Dec 22, 2011 at 10:53 PM

Sure,

there are a number of ways to do it. One is to create a list of directories within your app and call ZipFile.AddItems(), passing that list.

Another way is to use the FileSelector class within DotNetZip.  Call ZipFile.AddSelectedFiles("name != .\bin\*.* and name != .\obj\*.*") . (There are other options for that method, see the documentation for full details.

 

Coordinator
Dec 22, 2011 at 10:58 PM

One way to select a set of files from the filesystem is to use LINQ.  For example.

using (var zip = new ZipFile())
{
    // select all files that are not in the bin or obj dir
    var fileNames = (from fn in Directory.GetFiles(".", "*.*", SearchOption.AllDirectories)
                where !fn.StartsWith(".\bin\") && !fn.StartsWith(".\obj\")
                select fn).ToList();
    zip.AddFiles(fileNames);
    zip.Save(archiveName);
}

Dec 22, 2011 at 11:45 PM
Edited Dec 22, 2011 at 11:47 PM

Thanks for the fast reply. I tried both suggestions, but they are not working. This is my code:

using (var zipFile = new ZipFile())

{                                

        zipFile.AddDirectory(Config.FullPathSourceDir);

        zipFile.AddSelectedFiles(@"name != .\bin\*.* and name != .\obj\*.*");

        zipFile.Save(Config.FullPathTargetFile);

}

Config.FullPathSourceDir and Config.FullPathTargetFile are strings 
with directory and file information.

The content of the bin folder and the obj folder is still copied to the zip file.
Coordinator
Dec 29, 2011 at 9:42 PM

Your code is not right, for what you want to do.  Check the documentation for DotNetZip.

AddDirectory adds all files.  Bin and obj will be added if you use that call.

To NOT add all those files, use the AddSelectedFiles() method, with the appropriate overload. Check the documentation to determine which overload you need.

Check the reference documentation to understand what these methods do.

Jan 8, 2012 at 1:41 AM

I tried out the overloads from the documentation and I didn't get any of them to work my way. However, I modified your LINQ code sample a bit (the where clause) and got it working my way now. My working code:

			using (var zipFile = new ZipFile())
			{
				// select all files that are not in the bin or obj dir
				var fileNames = (from fn in Directory.GetFiles(Config.FullPathSourceDir, "*.*"SearchOption.AllDirectories)
								 where !fn.Contains("\\bin\\") && !fn.Contains("\\obj\\")
								 select fn).ToList();
 
				zipFile.AddFiles(fileNames);
				zipFile.Save(Config.FullPathTargetFile);
			}
Coordinator
Jan 10, 2012 at 11:03 PM
Edited Jan 10, 2012 at 11:04 PM

Glad it's working for you.

My code should have used "EndsWith()" instead of StartsWith(), but now I'm not sure EndsWith is a method on System.String.

But you have it sorted, so, it's all good.