none
Why does the ZipFile Class ExtractToDirectory lack an OverWrite switch? RRS feed

  • General discussion

  • Okay,

    Why does ZipFile's ExtractToDirectory not have an OverWrite switch, so you can decompress a ZIP file into a Directory, that may already have files in it? And Why does ZipArchive's ExtractToFile not have the ability to create the Sub-Directories, a file may be in?

    I created a workaround for the first using the second, but is brut force, it not elegant.

    For every entry I am running it through to find the sub directories, then testing if they are there and if not create them.

    Not all Zip programs will make a Subdirectory only entry and others do. (Not the Windows Compress does it both ways with the same files I am guessing it depend on if you are Compressing a folder or a group of folders. a Single folder seems to make the folder entries and group of folders and files does not.)

    Here is my brute force code if people are looking for something to do this.

    using System;
    using System.IO;
    using System.IO.Compression;

    class Program

    ///
    /// This unzips a file with sub-directories into a directory and sub-directories
    ///
    {
        
        static void CreateFolder(string _DestinationFolder)
        {
            string targetPath = _DestinationFolder;
            try
            {
                // Determine whether the directory exists.
                if (Directory.Exists(targetPath))
                {
                    return;
                }
                // Try to create the directory.
                DirectoryInfo di = Directory.CreateDirectory(targetPath);
            }
            catch (Exception Z)
            {

            }
            finally { }
        }

        static void CheckPathExist(string Path, string FullName)
        {
            int StartPosition = 0;
            int EndPosition = 260; // maximum path length
            int FullNameLength;
            string FolderName;
            string TempPath = Path;
            string NameTemp = FullName;
            FullNameLength = FullName.Length;
            do
            {
                EndPosition = NameTemp.IndexOf(@"/", StartPosition);
                if (EndPosition != -1)
                {
                    FolderName = NameTemp.Substring(StartPosition, (EndPosition - StartPosition));
                    TempPath = TempPath + @"\" + FolderName;
                    CreateFolder(TempPath);
                    StartPosition = EndPosition + 1; // Stepping over the previous @"/"
                }
            } while (EndPosition != -1);

        }

        static void Main(string[] args)
        {
            string zipPath = @"C:\Test\Test.zip";
            string extractPath = @"C:\Test\extract";
            using (ZipArchive archive = ZipFile.OpenRead(zipPath))
            {
                foreach (ZipArchiveEntry entry in archive.Entries)
                {
                    string fullPath = Path.Combine(extractPath, entry.FullName);
                    string TestName = entry.FullName;
                    int TestNameLength = TestName.Length;
                    if (TestName.Substring(TestNameLength - 1) != @"/")
                    {
                        entry.ExtractToFile(Path.Combine(extractPath, entry.FullName), true);
                    }
                    Console.WriteLine("Extrated " + entry.FullName); // Proves it is doing something
                }
                Console.ReadLine();
            }
        }
    }

    • Edited by Haslor Saturday, August 12, 2017 7:53 PM Forgot the Using statements
    Saturday, August 12, 2017 7:51 PM