Search code examples
c#zipextractupdatesarchive

Extract zip file and overwrite (in the same directory - C#)


I'm starting some C# stuff, and i would like to extract and force to overwrite all files from a zip archive. I know that there are many other solution in Stack, but nothing works for me :/

i've tried this method:

try
{
   string zipPath = (Directory.GetCurrentDirectory() + "\\" + "my_zip");
   Console.WriteLine("Zip's path: " + zipPath);
   string extractPath = Directory.GetCurrentDirectory();

   ZipFile.ExtractToDirectory(zipPath, extractPath);
   return (0); // 0 all fine 
}
catch (Exception)
{
   return (1); // 1 = extract error
}

This extractor works fine, but doesn't allow me to overwrite files meanwhile extraction, and it returns error and exceptions ... i've tried to take a look at MS-Documention, without success ...

someone know how does it work ?


Solution

  • Try something like this. Away from my dev box so this may require some tweaking, just writing it from memory.

    Edit: As someone mentioned you can use ExtractToFile which has an overwrite option. ExtractToDirectory does not.

    Essentially you unzip to a temporary folder then check if an unzipped file's name already exists in the destination folder. If so, it deletes the existing file and moves the newly unzipped one to the destination folder.

        try
        {
            string zipPath = (Directory.GetCurrentDirectory() + "\\" + "my_zip");
            Console.WriteLine("Zip's path: " + zipPath);
            //Declare a temporary path to unzip your files
            string tempPath = Path.Combine(Directory.GetCurrentDirectory(), "tempUnzip");
            string extractPath = Directory.GetCurrentDirectory();
            ZipFile.ExtractToDirectory(zipPath, tempPath);
    
            //build an array of the unzipped files
            string[] files = Directory.GetFiles(tempPath);
    
            foreach (string file in files)
            {
                FileInfo f = new FileInfo(file);
                //Check if the file exists already, if so delete it and then move the new file to the extract folder
                if (File.Exists(Path.Combine(extractPath,f.Name)))
                {
                    File.Delete(Path.Combine(extractPath, f.Name));
                    File.Move(f.FullName, Path.Combine(extractPath, f.Name));
                }
                else
                {
                    File.Move(f.FullName, Path.Combine(extractPath, f.Name));
                }
            }
            //Delete the temporary directory.
            Directory.Delete(tempPath);
            return (0); // 0 all fine 
        }
        catch (Exception)
        {
            return (1); // 1 = extract error
        }
    

    Edit, in the event directories are unzipped (again, may need to be tweaked, I didn't test it):

            try
            {
                string zipPath = (Directory.GetCurrentDirectory() + "\\" + "my_zip");
                Console.WriteLine("Zip's path: " + zipPath);
                //Declare a temporary path to unzip your files
                string tempPath = Path.Combine(Directory.GetCurrentDirectory(), "tempUnzip");
                string extractPath = Directory.GetCurrentDirectory();
                ZipFile.ExtractToDirectory(zipPath, tempPath);
    
                //build an array of the unzipped directories:
                string[] folders = Directory.GetDirectories(tempPath);
    
                foreach (string folder in folders)
                {
                    DirectoryInfo d = new DirectoryInfo(folder);
                    //If the directory doesn't already exist in the destination folder, move it to the destination.
                    if (!Directory.Exists(Path.Combine(extractPath,d.Name)))
                    {
                        Directory.Move(d.FullName, Path.Combine(extractPath, d.Name));
                        continue;
                    }
                    //If directory does exist, iterate through the files updating duplicates.
                    else
                    {
                        string[] subFiles = Directory.GetFiles(d.FullName);
                        foreach (string subFile in subFiles)
                        {
                            FileInfo f = new FileInfo(subFile);
                            //Check if the file exists already, if so delete it and then move the new file to the extract folder
                            if (File.Exists(Path.Combine(extractPath, d.Name, f.Name)))
                            {
                                File.Delete(Path.Combine(extractPath, d.Name, f.Name));
                                File.Move(f.FullName, Path.Combine(extractPath, d.Name, f.Name));
                            }
                            else
                            {
                                File.Move(f.FullName, Path.Combine(extractPath, d.Name, f.Name));
                            }
                        }
                    }
                }
    
                //build an array of the unzipped files in the parent directory
                string[] files = Directory.GetFiles(tempPath);
    
                foreach (string file in files)
                {
                    FileInfo f = new FileInfo(file);
                    //Check if the file exists already, if so delete it and then move the new file to the extract folder
                    if (File.Exists(Path.Combine(extractPath,f.Name)))
                    {
                        File.Delete(Path.Combine(extractPath, f.Name));
                        File.Move(f.FullName, Path.Combine(extractPath, f.Name));
                    }
                    else
                    {
                        File.Move(f.FullName, Path.Combine(extractPath, f.Name));
                    }
                }
                Directory.Delete(tempPath);
                return (0); // 0 all fine 
            }