ZipFile.CreateFromDirectory (source_dir, target_dir) will throw an exception and stop zipping when accessing any file in the directory. how can i do this zipping for the rest of the files?
Killing processes is not allowed; they are vital.
thanks
It is decided:
This is how I do it. Divide the task into 2 phases.
(Note: Use only Zipfile and ZipArchive from System.IO.Compression)
Step 1. Create a dummy zip file; (this should not affect the access problem)
Step 2. Scan and add files to a dummy zip file
Optional: keep the original directory hierarchy by adding a folder to the dummy zip file
Here is Step 2. Copy the file only if an exception is thrown.
private void UpdateBallFile(String source_dir, String target_zipfile)
{
using (ZipArchive archive = ZipFile.Open(target_zipfile + suffix, ZipArchiveMode.Update))
{
foreach (String subdir in Directory.GetDirectories(source_dir, "*.*", SearchOption.AllDirectories))
{
String relatedPath = subdir.Replace(source_dir, String.Empty);
String entry = relatedPath.Replace("\\", "/").Substring(1);
foreach (String file in Directory.GetFiles(subdir))
{
if (File.Exists(file))
{
FileInfo info = new FileInfo(file);
try
{
archive.CreateEntryFromFile(file, entry+"/"+info.Name);
}
catch
{
try
{
String copied_item = Path.Combine(@"c:\", info.Name);
File.Copy(file, copied_item, true);
archive.CreateEntryFromFile(copied_item, entry + "/" + info.Name);
File.Delete(copied_item);
}
catch (Exception ex)
{
UpdateLog(String.Format("Fails to zip: {0}, {1} ", file, ex.Message));
}
}
}
}
}
}
}
# . , , .