public override List<FB2File> LoadFile(string fileName, IFB2ImportSettings settings) { var result = new List<FB2File>(); try { RarArchive rarFile = RarArchive.Open(fileName); int n = rarFile.Entries.Count; Logger.Log.DebugFormat("Detected {0} entries in RAR file", n); foreach (var entry in rarFile.Entries) { if (entry.IsDirectory) { Logger.Log.DebugFormat("{0} is not file but folder", fileName); continue; } var extension = Path.GetExtension(entry.FilePath); if (extension != null && extension.ToUpper() == ".FB2") { try { string tempPath = Path.GetTempPath(); entry.WriteToDirectory(tempPath); if (FileTypeDetector.DetectFileType(tempPath) != FileTypeDetector.FileTypesEnum.FileTypeFB2) { Logger.Log.ErrorFormat("{0} is not FB2 file", entry.FilePath); continue; } string fileNameOnly = Path.GetFileName(entry.FilePath); Logger.Log.InfoFormat("Processing {0} ...", fileNameOnly); var fb2Loader = new FB2FileLoader(); try { result.AddRange(fb2Loader.LoadFile(string.Format(@"{0}\{1}", tempPath, fileNameOnly),settings)); } catch (Exception) { Logger.Log.ErrorFormat("Unable to load {0}", fileNameOnly); } } catch (Exception ex) { Logger.Log.ErrorFormat("Unable to unrar file entry {0} : {1}", entry.FilePath, ex); } } else { Logger.Log.InfoFormat("{0} is not FB2 file", entry.FilePath); } } } catch (Exception ex) { Logger.Log.ErrorFormat("Error loading RAR file : {0}", ex); } return result; }
public override List <FB2File> LoadFile(string fileName, IFB2ImportSettings settings) { var result = new List <FB2File>(); try { RarArchive rarFile = RarArchive.Open(fileName); int n = rarFile.Entries.Count; Logger.Log.DebugFormat("Detected {0} entries in RAR file", n); foreach (var entry in rarFile.Entries) { if (entry.IsDirectory) { Logger.Log.DebugFormat("{0} is not file but folder", fileName); continue; } var extension = Path.GetExtension(entry.FilePath); if (extension != null && extension.ToUpper() == ".FB2") { try { string tempPath = Path.GetTempPath(); entry.WriteToDirectory(tempPath); if (FileTypeDetector.DetectFileType(tempPath) != FileTypeDetector.FileTypesEnum.FileTypeFB2) { Logger.Log.ErrorFormat("{0} is not FB2 file", entry.FilePath); continue; } string fileNameOnly = Path.GetFileName(entry.FilePath); Logger.Log.InfoFormat("Processing {0} ...", fileNameOnly); var fb2Loader = new FB2FileLoader(); try { result.AddRange(fb2Loader.LoadFile(string.Format(@"{0}\{1}", tempPath, fileNameOnly), settings)); } catch (Exception) { Logger.Log.ErrorFormat("Unable to load {0}", fileNameOnly); } } catch (Exception ex) { Logger.Log.ErrorFormat("Unable to unrar file entry {0} : {1}", entry.FilePath, ex); } } else { Logger.Log.InfoFormat("{0} is not FB2 file", entry.FilePath); } } } catch (Exception ex) { Logger.Log.ErrorFormat("Error loading RAR file : {0}", ex); } return(result); }
public override List <FB2File> LoadFile(string fileName, IFB2ImportSettings settings) { var result = new List <FB2File>(); try { using (Stream s = File.OpenRead(fileName)) { if (settings.FixMode == FixOptions.Fb2FixAlways) { result.Add(LoadFB2StreamWithFix(s, ReadFb2FileStream)); } else { try { try { result.Add(ReadFb2FileStream(s)); } catch (XmlException) { if (settings.FixMode == FixOptions.DoNotFix) { Logger.Log.ErrorFormat("Error in file, not fixing "); return(null); } Logger.Log.Info("Error loading file - invalid XML content - attempting to repair..."); // try to read broken XML s.Seek(0, SeekOrigin.Begin); result.Add(ReadBrokenXmlFb2FileStream(s)); } } catch (XmlException) { if (settings.FixMode == FixOptions.MinimalFix) { Logger.Log.ErrorFormat("Error in file, not fixing "); return(result); } Logger.Log.Info("Repair attempt failed - attempting to repair using Fb2Fix..."); // try to read broken XML s.Seek(0, SeekOrigin.Begin); result.Add(LoadFB2StreamWithFix(s, ReadFb2FileStream)); } } } } catch (Exception ex) { Logger.Log.ErrorFormat("Error loading FB2 file : {0}", ex); } return(result); }
public override List<FB2File> LoadFile(string fileName, IFB2ImportSettings settings) { var result = new List<FB2File>(); try { using (Stream s = File.OpenRead(fileName)) { if (settings.FixMode == FixOptions.Fb2FixAlways) { result.Add(LoadFB2StreamWithFix(s, ReadFb2FileStream)); } else { try { try { result.Add(ReadFb2FileStream(s)); } catch (XmlException) { if (settings.FixMode == FixOptions.DoNotFix) { Logger.Log.ErrorFormat("Error in file, not fixing "); return null; } Logger.Log.Info("Error loading file - invalid XML content - attempting to repair..."); // try to read broken XML s.Seek(0, SeekOrigin.Begin); result.Add(ReadBrokenXmlFb2FileStream(s)); } } catch (XmlException) { if (settings.FixMode == FixOptions.MinimalFix) { Logger.Log.ErrorFormat("Error in file, not fixing "); return result; } Logger.Log.Info("Repair attempt failed - attempting to repair using Fb2Fix..."); // try to read broken XML s.Seek(0, SeekOrigin.Begin); result.Add(LoadFB2StreamWithFix(s, ReadFb2FileStream)); } } } } catch (Exception ex) { Logger.Log.ErrorFormat("Error loading FB2 file : {0}", ex); } return result; }
public void CopyFrom(IFB2ImportSettings temp) { if (temp == null) { throw new ArgumentNullException("temp"); } if (temp == this) { return; } _fixMode = temp.FixMode; _convertAlphaPng = temp.ConvertAlphaPng; }
public abstract List <FB2File> LoadFile(string fileName, IFB2ImportSettings settings);
public override List<FB2File> LoadFile(string fileName, IFB2ImportSettings settings) { Logger.Log.DebugFormat("Starting to load ZIP file {0}", fileName); var result = new List<FB2File>(); try { Exception returnEx = null; using (var s = new ZipInputStream(File.OpenRead(fileName))) { try { ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { if (!theEntry.IsFile || !theEntry.CanDecompress) { Logger.Log.InfoFormat("{0} is not file or not decompressable", fileName); continue; } Logger.Log.InfoFormat("Processing {0} ...", theEntry.Name); var extension = Path.GetExtension(theEntry.Name); if (extension != null && extension.ToUpper() == ".FB2") { if (settings.FixMode == FixOptions.Fb2FixAlways) { using (var s1 = new ZipInputStream(File.OpenRead(fileName))) { // reach the same position in ZIP while (theEntry.ToString() != s1.GetNextEntry().ToString()) { } result.Add(LoadFB2StreamWithFix(s1, ReadFb2FileStream)); } } try { result.Add(ReadFb2FileStream(s)); } catch (XmlException) // broken/malformed Xml detected { if (settings.FixMode == FixOptions.DoNotFix) { Logger.Log.ErrorFormat("Error in file, not fixing "); continue; } // try to run work around case Logger.Log.Info("Error loading file - invalid XML content - attempting to repair..."); using (var s1 = new ZipInputStream(File.OpenRead(fileName))) { // reach the same position in ZIP while (theEntry.ToString() != s1.GetNextEntry().ToString()) { } // try to read broken XML try { result.Add(ReadBrokenXmlFb2FileStream(s1)); } catch (XmlException) { if (settings.FixMode == FixOptions.MinimalFix) { Logger.Log.ErrorFormat("Error in file, not fixing "); continue; } s1.Close(); using (var s2 = new ZipInputStream(File.OpenRead(fileName))) { // reach the same position in ZIP while (theEntry.ToString() != s2.GetNextEntry().ToString()) { } Logger.Log.Info( "Repair attempt failed - attempting to repair using Fb2Fix..."); // try to read broken XML try { try { result.Add(LoadFB2StreamWithFix(s2, ReadFb2FileStream)); } catch (XmlException) { Logger.Log.ErrorFormat("Error in file - unable to fix"); //continue; } } catch (Exception ex) { Logger.Log.ErrorFormat("Error in file - Fb2Fix crashes - unable to fix. \nError {0}", ex.Message); //continue; } } } } } } } } catch (ZipException ze) { Logger.Log.ErrorFormat("{0} - problem decompressing the file, UnZip error: {1}", fileName, ze.Message); s.Close(); returnEx = ze; } catch (Exception ex) { Logger.Log.ErrorFormat("{0} - problem decompressing the file, error: {1}", fileName, ex); s.Close(); returnEx = ex; } s.Close(); } Logger.Log.DebugFormat("ZIP file {0} loaded successfully", fileName); if (returnEx != null) { throw returnEx; } } catch (Exception ex) { Logger.Log.ErrorFormat("Error loading ZIP file {0} : {1}", fileName, ex); } return result; }
public abstract List<FB2File> LoadFile(string fileName, IFB2ImportSettings settings);
public override List <FB2File> LoadFile(string fileName, IFB2ImportSettings settings) { Logger.Log.DebugFormat("Starting to load ZIP file {0}", fileName); var result = new List <FB2File>(); try { Exception returnEx = null; using (var s = new ZipInputStream(File.OpenRead(fileName))) { try { ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { if (!theEntry.IsFile || !theEntry.CanDecompress) { Logger.Log.InfoFormat("{0} is not file or not decompressable", fileName); continue; } Logger.Log.InfoFormat("Processing {0} ...", theEntry.Name); var extension = Path.GetExtension(theEntry.Name); if (extension != null && extension.ToUpper() == ".FB2") { if (settings.FixMode == FixOptions.Fb2FixAlways) { using (var s1 = new ZipInputStream(File.OpenRead(fileName))) { // reach the same position in ZIP while (theEntry.ToString() != s1.GetNextEntry().ToString()) { } result.Add(LoadFB2StreamWithFix(s1, ReadFb2FileStream)); } } try { result.Add(ReadFb2FileStream(s)); } catch (XmlException) // broken/malformed Xml detected { if (settings.FixMode == FixOptions.DoNotFix) { Logger.Log.ErrorFormat("Error in file, not fixing "); continue; } // try to run work around case Logger.Log.Info("Error loading file - invalid XML content - attempting to repair..."); using (var s1 = new ZipInputStream(File.OpenRead(fileName))) { // reach the same position in ZIP while (theEntry.ToString() != s1.GetNextEntry().ToString()) { } // try to read broken XML try { result.Add(ReadBrokenXmlFb2FileStream(s1)); } catch (XmlException) { if (settings.FixMode == FixOptions.MinimalFix) { Logger.Log.ErrorFormat("Error in file, not fixing "); continue; } s1.Close(); using (var s2 = new ZipInputStream(File.OpenRead(fileName))) { // reach the same position in ZIP while (theEntry.ToString() != s2.GetNextEntry().ToString()) { } Logger.Log.Info( "Repair attempt failed - attempting to repair using Fb2Fix..."); // try to read broken XML try { try { result.Add(LoadFB2StreamWithFix(s2, ReadFb2FileStream)); } catch (XmlException) { Logger.Log.ErrorFormat("Error in file - unable to fix"); //continue; } } catch (Exception ex) { Logger.Log.ErrorFormat("Error in file - Fb2Fix crashes - unable to fix. \nError {0}", ex.Message); //continue; } } } } } } } } catch (ZipException ze) { Logger.Log.ErrorFormat("{0} - problem decompressing the file, UnZip error: {1}", fileName, ze.Message); s.Close(); returnEx = ze; } catch (Exception ex) { Logger.Log.ErrorFormat("{0} - problem decompressing the file, error: {1}", fileName, ex); s.Close(); returnEx = ex; } s.Close(); } Logger.Log.DebugFormat("ZIP file {0} loaded successfully", fileName); if (returnEx != null) { throw returnEx; } } catch (Exception ex) { Logger.Log.ErrorFormat("Error loading ZIP file {0} : {1}", fileName, ex); } return(result); }