Пример #1
0
        //Added by daRedLoCo
        /// <summary>
        /// Extracts a ZipArchive to a destination (with overwrite functionality and ZipSettings inside the archive)
        /// </summary>
        /// <param name="archive">The archive to unpack</param>
        /// <param name="destinationDirectoryName">The destination directory</param>
        /// <param name="overwrite">If true, it will overwrite the content inside the destination directory</param>
        public static bool ExtractWithSettings(this ZipArchive archive, string destinationDirectoryName, bool overwrite)
        {
            ZipSettings settings             = null;
            bool        extractionSuccessful = true;

            if (archive.GetEntry("zipsettings.json") != null)
            {
                settings = ZipSettings.FromStream(archive.GetEntry("zipsettings.json").Open());
            }
            if (settings == null || settings.Subfolders.Count < 1)
            {
                return(ExtractToDirectory(archive, destinationDirectoryName, overwrite));
            }
            Uninstaller uninstaller = new Uninstaller(destinationDirectoryName);

            foreach (ZipArchiveEntry file in archive.Entries)
            {
                string completeFileName = Path.Combine(destinationDirectoryName, settings.GetSubfolder(file.FullName), file.FullName);
                string directory        = Path.GetDirectoryName(completeFileName);

                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                if (!string.IsNullOrEmpty(file.Name) && file.Name != "zipsettings.json")
                {
                    while (!Utils.CanBeWrittenTo(completeFileName))
                    {
                        if (DialogResult.Cancel == MessageBox.Show($"The file '{completeFileName}' is locked or in use and can't be overwritten! Please close the process thats blocking it and press OK or press CANCEL to stop the installing process.", "Warning!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning))
                        {
                            MessageBox.Show("Can't progress with the installation!", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            extractionSuccessful = false;
                            break;
                        }
                    }
                    if (!extractionSuccessful)
                    {
                        break;
                    }
                    file.ExtractToFile(completeFileName, overwrite);
                    uninstaller.files.Add(completeFileName);
                }
            }
            if (Settings.Uninstall)
            {
                uninstaller.GenerateFile();
            }


            if (!extractionSuccessful)
            {
                return(false);
            }
            return(true);
        }
Пример #2
0
 /// <summary>
 /// Returns the ZipSettings from a Stream
 /// </summary>
 /// <param name="stream">The stream with the ZipSettings</param>
 /// <returns>A ZipSettings object</returns>
 public static ZipSettings FromStream(Stream stream)
 {
     using (StreamReader reader = new StreamReader(stream))
     {
         try
         {
             string      jsonstr      = reader.ReadToEnd();
             ZipSettings zs           = new ZipSettings();
             JObject     obj          = JsonConvert.DeserializeObject <JObject>(jsonstr);
             JArray      foldersarray = obj.Value <JArray>("Subfolders");
             foreach (JObject subdir in foldersarray.Children <JObject>())
             {
                 SubFolder sub = new SubFolder();
                 foreach (var dict in subdir)
                 {
                     sub.Name = dict.Key;
                     foreach (JToken token in dict.Value.Children <JToken>().ToList())
                     {
                         Utils.ZipType ztype = Utils.FileOrDir(token.Value <string>());
                         if (ztype == Utils.ZipType.File)
                         {
                             sub.Files.Add(token.Value <string>());
                         }
                         else if (ztype == Utils.ZipType.Directory)
                         {
                             sub.Directories.Add(token.Value <string>());
                         }
                     }
                 }
                 zs.Subfolders.Add(sub);
             }
             return(zs);
         }
         catch (Exception ex)
         {
             MainWindow.Instance.WriteLog("There was an error while reading the ZipSettings! => " + ex.Message);
             return(null);
         }
     }
 }