/// <summary> /// Method to save a single entry as file /// </summary> /// <param name="item">ExtractorItem to save</param> /// <param name="filename">file name for the target file</param> /// <param name="writeStatus">If true, the status of the operation will be stated</param> /// <returns>True if the file could be saved</returns> private bool Save(ExtractorItem item, string filename, bool writeStatus) { try { FileInfo fi = new FileInfo(filename); if (Directory.Exists(fi.DirectoryName) == false) { Directory.CreateDirectory(fi.DirectoryName); } FileStream fs = new FileStream(filename, FileMode.Create); item.Stream.Position = 0; fs.Write(item.Stream.GetBuffer(), 0, (int)item.Stream.Length); fs.Flush(); fs.Close(); if (writeStatus == true) { CurrentModel.StatusText = "The file was saved as: " + filename; } } catch (Exception e) { if (writeStatus == true) { CurrentModel.StatusText = "Could not save the file: " + e.Message; MessageBox.Show("The file could not be saved", "Error", MessageBoxButton.OK, MessageBoxImage.Information); return(false); } } return(true); }
/// <summary> /// Method to get all embedded files as list according to the target image / file (format) type /// </summary> /// <param name="archive">Reference to the opened file (handled as archive)</param> /// <returns>A list of items</returns> private List <ExtractorItem> GetEntries(ref ArchiveFile archive) { List <ExtractorItem> list = new List <ExtractorItem>(); MemoryStream ms; string[] split; char[] delimiter = new char[] { '\\', '/' }; string file, path; ExtractorItem item; for (int i = 0; i < archive.Entries.Count; i++) { if (archive.Entries[i].IsFolder) { continue; // Skip folders as entries } ms = new MemoryStream(); archive.Entries[i].Extract(ms); ms.Flush(); ms.Position = 0; split = archive.Entries[i].FileName.Split(delimiter); file = split[split.Length - 1]; path = archive.Entries[i].FileName.Substring(0, archive.Entries[i].FileName.Length - file.Length); item = new ExtractorItem(file, ms, path, currentModel.GenericTextPreview); item.Crc32 = archive.Entries[i].CRC; item.FileSize = (long)archive.Entries[i].Size; item.LastChange = archive.Entries[i].LastWriteTime; list.Add(item); } return(list); }
/// <summary> /// Restores the user settings /// </summary> public void RestoreSettings() { CurrentModel.ShowEmbeddedImages = Properties.Settings.Default.DocumentShowImages; CurrentModel.ShowEmbeddedOther = Properties.Settings.Default.DocumentShowOther; CurrentModel.GenericTextPreview = Properties.Settings.Default.DocumentGenericTextPreview; CurrentModel.LargeFilePreviewWarning = Properties.Settings.Default.DocumentShowLargeFileWarning; CurrentModel.KeepFolderStructure = Properties.Settings.Default.DocumentPreserveStructure; CurrentModel.ShowInExplorer = Properties.Settings.Default.DocumentShowExplorer; CurrentModel.UseDarkMode = Properties.Settings.Default.AppearanceDarkMode; if (Properties.Settings.Default.ExtractSaveAll) { CurrentModel.SaveAllIsDefault = true; } else if (Properties.Settings.Default.ExtractSaveSelected) { CurrentModel.SaveSelectedIsDefault = true; } LoadRecentFiles(Properties.Settings.Default.RecentFiles); HandleDarkMode(); string imageExts = Properties.Settings.Default.ImageExtensions; string textExts = Properties.Settings.Default.TextExtensions; string xmlExts = Properties.Settings.Default.XmlExtensions; if (!ExtractorItem.GetExtensions(textExts, imageExts, xmlExts)) { Properties.Settings.Default.ImageExtensions = ExtractorItem.FALLBACK_IMAGE_EXTENTIONS; Properties.Settings.Default.TextExtensions = ExtractorItem.FALLBACK_TEXT_EXTENTIONS; Properties.Settings.Default.XmlExtensions = ExtractorItem.FALLBACK_XML_EXTENTIONS; MessageBox.Show(I18n.T(I18n.Key.DialogInvalidExtensions), I18n.T(I18n.Key.DialogErrorTitle), MessageBoxButton.OK, MessageBoxImage.Error); } }
/// <summary> /// Method to save a single entry as file /// </summary> /// <param name="item">ExtractorItem to save</param> /// <param name="filename">file name for the target file</param> /// <param name="writeStatus">If true, the status of the operation will be stated</param> /// <returns>True if the file could be saved</returns> private bool Save(ExtractorItem item, string filename, bool writeStatus) { try { FileInfo fi = new FileInfo(filename); if (!Directory.Exists(fi.DirectoryName)) { Directory.CreateDirectory(fi.DirectoryName); } FileStream fs = new FileStream(filename, FileMode.Create); item.Stream.Position = 0; fs.Write(item.Stream.GetBuffer(), 0, (int)item.Stream.Length); fs.Flush(); fs.Close(); if (writeStatus) { CurrentModel.StatusText = I18n.R(I18n.Key.StatusSaveSuccess, filename); } } catch (Exception e) { if (writeStatus) { CurrentModel.StatusText = I18n.R(I18n.Key.StatusSaveFailure, e.Message); MessageBox.Show(I18n.T(I18n.Key.DialogSaveFailure), I18n.T(I18n.Key.DialogErrorTitle), MessageBoxButton.OK, MessageBoxImage.Information); return(false); } } return(true); }
/// <summary> /// Method to get all embedded files as list according to the target image / file (format) type /// </summary> /// <param name="format">Target format</param> /// <param name="archive">Reference to the opened file (handled as archive)</param> /// <returns>A list of items</returns> private List <ExtractorItem> GetEntries(EmbeddedFormat format, ref ArchiveFile archive) { string extension = ""; bool allFiles = false; switch (format) { case EmbeddedFormat.Emf: extension = ".emf"; break; case EmbeddedFormat.Wmf: extension = ".wmf"; break; case EmbeddedFormat.Png: extension = ".png"; break; case EmbeddedFormat.Jpg: extension = ".jpg"; break; case EmbeddedFormat.All: allFiles = true; break; } List <ExtractorItem> list = new List <ExtractorItem>(); MemoryStream ms; string[] split; char[] delimiter = new char[] { '\\', '/' }; string file, path; ExtractorItem item; for (int i = 0; i < archive.Entries.Count; i++) { if ((archive.Entries[i].IsFolder == false && archive.Entries[i].FileName.ToLower().EndsWith(extension)) || allFiles == true) { ms = new MemoryStream(); archive.Entries[i].Extract(ms); ms.Flush(); ms.Position = 0; split = archive.Entries[i].FileName.Split(delimiter); file = split[split.Length - 1]; path = archive.Entries[i].FileName.Substring(0, archive.Entries[i].FileName.Length - file.Length); item = new ExtractorItem(file, ms, false, path); item.Crc32 = archive.Entries[i].CRC; item.FileSize = (long)archive.Entries[i].Size; item.LastChange = archive.Entries[i].LastWriteTime; list.Add(item); } } return(list); }
/// <summary> /// Method to check whether the specified input file exists /// </summary> /// <param name="folder">Folder</param> /// <param name="item">Extractor Item</param> /// <param name="fileName">Determined name and path of the existing file as output parameter</param> /// <param name="keepFolderStructure">If true, the relative folder of the item will be added to the root folder</param> /// <returns>True if the file exists</returns> private bool CheckFileExists(string folder, ExtractorItem item, bool keepFolderStructure, out string fileName) { string separator = Path.DirectorySeparatorChar.ToString(); char[] chars = new char[] { '/', '\\' }; if (keepFolderStructure == true) { folder = folder.TrimEnd(chars) + separator + item.Path.Trim(chars); } else { folder = folder.TrimEnd(chars); } fileName = folder + separator + item.FileName; return(File.Exists(fileName)); }