ZipFile.CreateFromDirectory will continue to work for the rest of the file if some files are not available in the directory

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));
                            }
                        }
                    }
                }
            }
        }   
    }

# . , , .

+4
2

, , :

ZipFile myzip = new ZipFile("myzipFile");
foreach (string file in Directory.GetFiles(@"D:\sample"))
{
    try
    {
        var stream = File.Open(file, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
        stream.Dispose();
        myzip.AddFile(file);// add file to zip only if it is accessible. else it will throw some exception 
        //hence it wont added to the zipped folder.
    }
    catch 
    { }              
}
myzip.Save(@"D:\sample\myfile.zip");// this zip file contains only file that have access

, ,

+3

copy/temp User/AppData/Local, . , . zip- . , . zip-...

+1

Source: https://habr.com/ru/post/1606949/


All Articles