Ejemplo n.º 1
0
        static void UnZipCommand(string Zip)
        {
            int           EntryCount       = 0;
            bool          OkToKeepGoing    = false;
            StringBuilder TargetTempFolder = new StringBuilder(GetTempFileLocation());
            StringBuilder FinalFolder      = new StringBuilder();

            if (!Zip.Contains(Path.VolumeSeparatorChar))
            {
                FinalFolder.Append(Directory.GetCurrentDirectory());
            }
            else
            {
                FinalFolder.Append(Path.GetDirectoryName(Zip));
            }

            if (FinalFolder.Length != 0)
            {
                if (FinalFolder[FinalFolder.Length - 1] != Path.DirectorySeparatorChar)
                {
                    FinalFolder.Append(Path.DirectorySeparatorChar);
                }
            }

            if (File.Exists(TargetTempFolder.ToString()))
            {
                File.Delete(TargetTempFolder.ToString());
                if (TargetTempFolder[TargetTempFolder.Length - 1] != Path.DirectorySeparatorChar)
                {
                    TargetTempFolder.Append(Path.DirectorySeparatorChar);
                }
                TargetTempFolder.Append(Path.GetFileNameWithoutExtension(Zip));
                Directory.CreateDirectory(TargetTempFolder.ToString());

                try
                {
                    using (FileStream Input = File.OpenRead(Zip))
                    {
                        using (ZipArchive Arch = new ZipArchive(Input, ZipArchiveMode.Read))
                        {
                            Arch.ExtractToDirectory(TargetTempFolder.ToString());
                            OkToKeepGoing = true;
                            EntryCount    = Arch.Entries.Count;
                        }
                    }
                }
                catch (UnauthorizedAccessException e)
                {
                    Console.WriteLine(e.Message);
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    // if it's all good. Move the resulst to the target.
                    if (OkToKeepGoing)
                    {
                        //try

                        {
                            if (EntryCount > 0)
                            {
                                string[]      Info      = Directory.GetFiles(TargetTempFolder.ToString());
                                StringBuilder RootBased = new StringBuilder();
                                foreach (string SingleFile in Info)
                                {
                                    RootBased.Clear();
                                    RootBased.Append(FinalFolder.ToString());
                                    if (RootBased[RootBased.Length - 1] != Path.DirectorySeparatorChar)
                                    {
                                        RootBased.Append(Path.DirectorySeparatorChar);
                                    }
                                    if (Directory.Exists(RootBased.ToString()) == false)
                                    {
                                        try
                                        {
                                            Directory.CreateDirectory(RootBased.ToString());
                                        }
                                        catch (UnauthorizedAccessException)
                                        {
                                            Console.WriteLine("Could not completely extract zip file " + Zip);
                                            Console.WriteLine("Access Denied at " + RootBased.ToString());
                                            break;
                                        }
                                    }
                                    RootBased.Append(SingleFile.Substring(TargetTempFolder.Length));
                                    try
                                    {
                                        if ((SafeMode == true) && (File.Exists(RootBased.ToString())))
                                        {
                                            Console.WriteLine(string.Format("Error: Unable to extra {0} to {1}. Reason is target already exists and SafeMode is on", SingleFile, RootBased.ToString()));
                                        }
                                        else
                                        {
                                            bool OkMove = false;
                                            if (File.Exists(RootBased.ToString()))
                                            {
                                                try
                                                {
                                                    File.Delete(RootBased.ToString());
                                                    OkMove = true;
                                                }
                                                catch (IOException)
                                                {
                                                    // can't delete.
                                                    OkMove = false;
                                                }
                                            }
                                            else
                                            {
                                                OkMove = true;
                                            }
                                            if (OkMove == true)
                                            {
                                                File.Move(SingleFile, RootBased.ToString());
                                                Console.WriteLine("Extracted to " + RootBased.ToString());
                                            }
                                            else
                                            {
                                                Console.WriteLine("Unable to move " + SingleFile + " to final location of " + RootBased.ToString());
                                            }
                                        }
                                    }
                                    catch (UnauthorizedAccessException)
                                    {
                                        Console.WriteLine("Access Denied when moving to " + RootBased.ToString());
                                        continue;
                                    }
                                    catch (IOException e)
                                    {
                                        Console.WriteLine(e.Message);
                                    }
                                }

                                if (Directory.Exists(FinalFolder.ToString()))
                                {
                                    if (ShowResults)
                                    {
                                        Explore(FinalFolder.ToString());
                                    }
                                }
                            }
                        }
                    }

                    // now clean up temp folder
                    Directory.Delete(TargetTempFolder.ToString(), true);
                }
            }
            else
            {
                Console.WriteLine("Error: Could not extract zip. Reason being failed to setup tempoary storage");
            }
        }