AddAllFromDirectory does not ignore files locked by other process #388

Closed
opened 2026-01-29 22:11:06 +00:00 by claunia · 1 comment
Owner

Originally created by @CaledoniaProject on GitHub (Jan 21, 2020).

I'm using the following code to compress a directory:

        using (var archive = ZipArchive.Create())
        {
            archive.AddAllFromDirectory(dirname);
            archive.SaveTo(zipname, options);
        }

When one of the files is not readable, it throws exception.

How can I let it continue and ignore files that can't be read?

Originally created by @CaledoniaProject on GitHub (Jan 21, 2020). I'm using the following code to compress a directory: using (var archive = ZipArchive.Create()) { archive.AddAllFromDirectory(dirname); archive.SaveTo(zipname, options); } When one of the files is not readable, it throws exception. How can I let it continue and ignore files that can't be read?
Author
Owner

@turbolocust commented on GitHub (Jan 21, 2020):

Use the AddEntry method instead.
Take a look at the actual implementation:

public static void AddAllFromDirectory(
	this IWritableArchive writableArchive,
	string filePath, string searchPattern = "*.*", SearchOption searchOption = SearchOption.AllDirectories)
{
	foreach (var path in Directory.EnumerateFiles(filePath, searchPattern, searchOption))
	{
		var fileInfo = new FileInfo(path);
		writableArchive.AddEntry(path.Substring(filePath.Length), fileInfo.OpenRead(), true, fileInfo.Length,
								 fileInfo.LastWriteTime);
	}
}
@turbolocust commented on GitHub (Jan 21, 2020): Use the **AddEntry** method instead. Take a look at the actual implementation: ```csharp public static void AddAllFromDirectory( this IWritableArchive writableArchive, string filePath, string searchPattern = "*.*", SearchOption searchOption = SearchOption.AllDirectories) { foreach (var path in Directory.EnumerateFiles(filePath, searchPattern, searchOption)) { var fileInfo = new FileInfo(path); writableArchive.AddEntry(path.Substring(filePath.Length), fileInfo.OpenRead(), true, fileInfo.Length, fileInfo.LastWriteTime); } } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#388