Esempio n. 1
0
        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;
         
        }
Esempio n. 2
0
        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);
        }
 /// <summary>
 /// Loads and parses input file or archive to ePub in memory representation
 /// </summary>
 /// <param name="fileName">file to load</param>
 /// <returns></returns>
 public bool LoadAndCheckFB2Files(string fileName)
 {
     Logger.Log.InfoFormat("Starting to convert {0}", fileName);
     _fb2Files.Clear();
     if (!File.Exists(fileName))
     {
         Logger.Log.ErrorFormat("Unable to locate file {0} on disk.", fileName);
         return false;
     }
     switch (FileTypeDetector.DetectFileType(fileName))
     {
         case FileTypeDetector.FileTypesEnum.FileTypeZIP:
             Logger.Log.InfoFormat("Loading ZIP : {0}",fileName);
             var fb2ZipLoader = new FB2ZipFileLoader();
             try
             {
                 _fb2Files.AddRange(fb2ZipLoader.LoadFile(fileName, Settings.FB2ImportSettings));
             }
             catch (Exception)
             {
                 Logger.Log.ErrorFormat("Error loading ZIP {0} :",fileName);
                 return false;
             }
             break;
         case FileTypeDetector.FileTypesEnum.FileTypeFB2:
             Logger.Log.InfoFormat("Processing {0} ...", fileName);
             var fb2FileLoader = new FB2FileLoader();
             try
             {
                 _fb2Files.AddRange(fb2FileLoader.LoadFile(fileName,Settings.FB2ImportSettings));
             }
             catch (Exception)
             {
                 Logger.Log.ErrorFormat("Error loading FB2 {0} :", fileName);
                 return false;
             }
             break;
         case FileTypeDetector.FileTypesEnum.FileTypeRAR:
             Logger.Log.InfoFormat("Loading RAR : {0}", fileName);
             var fb2RarLoader = new FB2RarLoader();
             try
             {
                 _fb2Files.AddRange(fb2RarLoader.LoadFile(fileName,Settings.FB2ImportSettings));
             }
             catch (Exception)
             {
                 Logger.Log.ErrorFormat("Error loading RAR {0} :", fileName);
                 return false;
             }
             break;
         default:
             Logger.Log.InfoFormat("File {0} is of unsupported type",fileName);
             return false;
     }
     return true;
 }
Esempio n. 4
0
        /// <summary>
        /// Detects file type of the input file
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        internal static FileTypesEnum DetectFileType(string fileName)
        {
            Logger.Log.DebugFormat("Detecting file type for {0}", fileName);
            var extension = Path.GetExtension(fileName);

            if (extension != null)
            {
                switch (extension.ToUpper())
                {
                case ".FB2":
                    Logger.Log.Debug("The file is FB2 file");
                    return(FileTypesEnum.FileTypeFB2);

                case ".ZIP":
                    Logger.Log.Debug("The file is ZIP file");
                    return(FileTypesEnum.FileTypeZIP);

                case ".RAR":
                    Logger.Log.Debug("The file is RAR file");
                    return(FileTypesEnum.FileTypeRAR);

                default:
                    Logger.Log.Debug("Can't use extension - attempting to detect");
                    if (FB2ZipFileLoader.IsZipFile(fileName))
                    {
                        Logger.Log.Debug("The file is ZIP file");
                        return(FileTypesEnum.FileTypeZIP);
                    }
                    if (FB2RarLoader.IsRarFile(fileName))
                    {
                        Logger.Log.Debug("The file is RAR file");
                        return(FileTypesEnum.FileTypeRAR);
                    }
                    if (FB2FileLoader.IsFB2File(fileName))
                    {
                        Logger.Log.Debug("The file is FB2 file");
                        return(FileTypesEnum.FileTypeFB2);
                    }
                    break;
                }
            }
            Logger.Log.Debug("The file is unknown file type");
            return(FileTypesEnum.FileTypeUnknown);
        }