Beispiel #1
0
 private void AddListItem(FiasFileInfo aFileInfo)
 {
     if (Dispatcher == null)
     {
         FileInfoList.Add(aFileInfo);
     }
     else
     {
         Dispatcher.BeginInvoke(new ThreadStart(() => FileInfoList.Add(aFileInfo)));
     }
 }
Beispiel #2
0
        private void CheckFile(FiasFileInfo aFileInfo)
        {
            var folder = Path.GetDirectoryName(aFileInfo.FileName) + "\\Extracted";

            if (!Directory.Exists(folder))
            {
                aFileInfo.Extracted = false;
                return;
            }
            var files = Directory.GetFiles(folder).Select(Path.GetFileName).ToList();

            using (var unrar = new Unrar())
            {
                unrar.ArchivePathName = aFileInfo.FileName;
                unrar.Open(Unrar.OpenMode.List);
                while (unrar.ReadHeader())
                {
                    if (stopEvent.WaitOne(0))
                    {
                        throw new StopException();
                    }

                    if (OnCheckProgress != null)
                    {
                        OnCheckProgress(0, 0, string.Format("Проверка файла {0}", folder + "\\" + unrar.CurrentFile.FileName));
                    }

                    if (!files.Contains(unrar.CurrentFile.FileName))
                    {
                        aFileInfo.Extracted = false;
                        return;
                    }

                    var  crc32 = new Crc32();
                    uint hash;

                    using (var fs = File.Open(folder + "\\" + unrar.CurrentFile.FileName, FileMode.Open))
                    {
                        var byteHash = crc32.ComputeHash(fs);
                        hash = BitConverter.ToUInt32(byteHash.Reverse().ToArray(), 0);
                    }
                    if (unrar.CurrentFile.FileCRC != hash)
                    {
                        aFileInfo.Extracted = false;
                        return;
                    }
                    unrar.Skip();
                }
            }
            aFileInfo.Extracted = true;
        }
Beispiel #3
0
        private void DownloadFile(FiasFileInfo aFileInfo)
        {
            using (var client = new WebClient())
            {
                using (var remoteStream = client.OpenRead(aFileInfo.Url))
                {
                    if (!Directory.Exists(Path.GetDirectoryName(aFileInfo.FileName)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(aFileInfo.FileName));
                    }
                    var tmpFileName = Path.GetDirectoryName(aFileInfo.FileName) + "\\data.tmp";
                    using (var localStream = new FileStream(tmpFileName, FileMode.Create, FileAccess.Write))
                    {
                        long      totalBytesReaded = 0;
                        const int bufferLength     = 1024 * 1024;

                        while (totalBytesReaded < aFileInfo.Length)
                        {
                            if (stopEvent.WaitOne(0))
                            {
                                throw new StopException();
                            }

                            byte[] buffer = null;
                            buffer = aFileInfo.Length - totalBytesReaded < bufferLength ? new byte[Convert.ToInt32(aFileInfo.Length - totalBytesReaded)] : new byte[bufferLength];
                            if (remoteStream != null)
                            {
                                var bytesReaded = remoteStream.Read(buffer, 0, buffer.Length);
                                localStream.Write(buffer, 0, bytesReaded);
                                totalBytesReaded += bytesReaded;
                            }

                            if (OnDownloadProgress != null)
                            {
                                OnDownloadProgress(totalBytesReaded, aFileInfo.Length, string.Format("Загрузка {0}", aFileInfo.FileName));
                            }
                        }
                    }

                    if (File.Exists(aFileInfo.FileName))
                    {
                        File.Delete(aFileInfo.FileName);
                    }
                    File.Move(tmpFileName, aFileInfo.FileName);
                    aFileInfo.IsExists = true;
                    aFileInfo.Checked  = false;
                }
            }
        }
Beispiel #4
0
        private void ProcessXmlFile(FiasFileInfo aFileInfo)
        {
            var databaseMap = FiasDatabaseMap.LoadFromFile(RootFolder + "\\Fias.Mapping.xml");

            var itemWorker = new FiasDbUpdateWorkerItemWorker
            {
                Connection  = connection,
                DbCache     = DbCache,
                FileInfo    = aFileInfo,
                DatabaseMap = databaseMap,
                StopEvent   = stopEvent
            };

            itemWorker.Execute();
        }
Beispiel #5
0
        private void CheckFile(FiasFileInfo aFileInfo)
        {
            var folder = Path.GetDirectoryName(aFileInfo.FileName) + "\\Extracted";
            if (!Directory.Exists(folder))
            {
                aFileInfo.Extracted = false;
                return;
            }
            var files = Directory.GetFiles(folder).Select(Path.GetFileName).ToList();

            using (var unrar = new Unrar())
            {
                unrar.ArchivePathName = aFileInfo.FileName;
                unrar.Open(Unrar.OpenMode.List);
                while (unrar.ReadHeader())
                {
                    if (stopEvent.WaitOne(0))
                        throw new StopException();

                    if (OnCheckProgress != null)
                        OnCheckProgress(0, 0, string.Format("Проверка файла {0}", folder + "\\" + unrar.CurrentFile.FileName));

                    if (!files.Contains(unrar.CurrentFile.FileName))
                    {
                        aFileInfo.Extracted = false;
                        return;
                    }

                    var crc32 = new Crc32();
                    uint hash;

                    using (var fs = File.Open(folder + "\\" + unrar.CurrentFile.FileName, FileMode.Open))
                    {
                        var byteHash = crc32.ComputeHash(fs);
                        hash = BitConverter.ToUInt32(byteHash.Reverse().ToArray(), 0);
                    }
                    if (unrar.CurrentFile.FileCRC != hash)
                    {
                        aFileInfo.Extracted = false;
                        return;
                    }
                    unrar.Skip();
                }
            }
            aFileInfo.Extracted = true;
        }
Beispiel #6
0
 private void UnRarFile(FiasFileInfo aFileInfo)
 {
     using (var unrar = new Unrar())
     {
         unrar.Open(aFileInfo.FileName, Unrar.OpenMode.Extract);
         unrar.DestinationPath     = Path.GetDirectoryName(aFileInfo.FileName) + "\\Extracted.tmp";
         unrar.ExtractionProgress += unrar_ExtractionProgress;
         while (unrar.ReadHeader())
         {
             if (stopEvent.WaitOne(0))
             {
                 throw new StopException();
             }
             unrar.Extract();
         }
     }
     Directory.Move(Path.GetDirectoryName(aFileInfo.FileName) + "\\Extracted.tmp", Path.GetDirectoryName(aFileInfo.FileName) + "\\Extracted");
     aFileInfo.Checked   = false;
     aFileInfo.Extracted = true;
 }
Beispiel #7
0
        private void ProcessXmlFile(FiasFileInfo aFileInfo)
        {
            var databaseMap = FiasDatabaseMap.LoadFromFile(RootFolder + "\\Fias.Mapping.xml");

            var itemWorker = new FiasDbUpdateWorkerItemWorker
            {
                Connection = connection,
                DbCache = DbCache,
                FileInfo = aFileInfo,
                DatabaseMap = databaseMap,
                StopEvent = stopEvent
            };
            itemWorker.Execute();
        }
Beispiel #8
0
        private void LoadData()
        {
            if (OnLoadDataStart != null)
                OnLoadDataStart();

            try
            {
                ClearList();
                var databaseMap = FiasDatabaseMap.LoadFromFile(RootFolder + "\\Fias.Mapping.xml");
                using (var transaction = connection.BeginTransaction())
                {
                    var dataFolder = RootFolder + "\\Data\\";
                    foreach (var dateFolder in Directory.GetDirectories(dataFolder).OrderBy(a => a))
                    {
                        var extractedFolder = dateFolder + "\\Extracted\\";
                        if (!Directory.Exists(extractedFolder)) continue;
                        foreach (var xmlFileName in Directory.GetFiles(extractedFolder))
                        {
                            var fInfo = new FiasFileInfo
                            {
                                Description = Path.GetFileName(dateFolder),
                                FileName = xmlFileName
                            };
                            var fileName = Path.GetFileName(xmlFileName);
                            var dtImport = DbData.GetFieldValue(connection, transaction, string.Format("select DT_IMPORT from FIAS_IMPORT_HISTORY where name = '{0}';", fileName));
                            if (dtImport == null || dtImport is DBNull)
                            {
                                var c = 0;
                                var updated = DbData.GetFieldValue(connection, transaction, string.Format("select LAST_COMMIT_COUNT from FIAS_IMPORT_HISTORY where name = '{0}';", fileName));
                                if (!(updated == null || updated is DBNull))
                                    c = Convert.ToInt32(updated);
                                fInfo.StatusPos = c;
                                fInfo.Status = c == 0 ? string.Format("Пусто") : string.Format("Загружено записей {0}", c);
                                fInfo.IsExists = false;
                            }
                            else
                            {
                                fInfo.Url = dtImport.ToString();
                                fInfo.Status = "Полностью загружен";
                                fInfo.IsExists = true;
                            }

                            var tableMap = databaseMap.Tables.FirstOrDefault(a =>
                            {
                                var name = Path.GetFileName(fInfo.FileName);
                                return name != null && name.StartsWith(a.XmlDescription);
                            });
                            fInfo.Length = tableMap == null ? 0 : tableMap.OrderNum;
                            fInfo.Checked = tableMap != null;
                            AddListItem(fInfo);
                        }
                    }
                }
            }
            finally
            {
                if (OnLoadDataComplete != null)
                    OnLoadDataComplete();
            }
        }
Beispiel #9
0
 private void AddListItem(FiasFileInfo aFileInfo)
 {
     if (Dispatcher == null)
         FileInfoList.Add(aFileInfo);
     else
     {
         Dispatcher.BeginInvoke(new ThreadStart(() => FileInfoList.Add(aFileInfo)));
     }
 }
Beispiel #10
0
        private void LoadData()
        {
            if (OnLoadDataStart != null)
            {
                OnLoadDataStart();
            }

            try
            {
                ClearList();
                var databaseMap = FiasDatabaseMap.LoadFromFile(RootFolder + "\\Fias.Mapping.xml");
                using (var transaction = connection.BeginTransaction())
                {
                    var dataFolder = RootFolder + "\\Data\\";
                    foreach (var dateFolder in Directory.GetDirectories(dataFolder).OrderBy(a => a))
                    {
                        var extractedFolder = dateFolder + "\\Extracted\\";
                        if (!Directory.Exists(extractedFolder))
                        {
                            continue;
                        }
                        foreach (var xmlFileName in Directory.GetFiles(extractedFolder))
                        {
                            var fInfo = new FiasFileInfo
                            {
                                Description = Path.GetFileName(dateFolder),
                                FileName    = xmlFileName
                            };
                            var fileName = Path.GetFileName(xmlFileName);
                            var dtImport = DbData.GetFieldValue(connection, transaction, string.Format("select DT_IMPORT from FIAS_IMPORT_HISTORY where name = '{0}';", fileName));
                            if (dtImport == null || dtImport is DBNull)
                            {
                                var c       = 0;
                                var updated = DbData.GetFieldValue(connection, transaction, string.Format("select LAST_COMMIT_COUNT from FIAS_IMPORT_HISTORY where name = '{0}';", fileName));
                                if (!(updated == null || updated is DBNull))
                                {
                                    c = Convert.ToInt32(updated);
                                }
                                fInfo.StatusPos = c;
                                fInfo.Status    = c == 0 ? string.Format("Пусто") : string.Format("Загружено записей {0}", c);
                                fInfo.IsExists  = false;
                            }
                            else
                            {
                                fInfo.Url      = dtImport.ToString();
                                fInfo.Status   = "Полностью загружен";
                                fInfo.IsExists = true;
                            }

                            var tableMap = databaseMap.Tables.FirstOrDefault(a =>
                            {
                                var name = Path.GetFileName(fInfo.FileName);
                                return(name != null && name.StartsWith(a.XmlDescription));
                            });
                            fInfo.Length  = tableMap == null ? 0 : tableMap.OrderNum;
                            fInfo.Checked = tableMap != null;
                            AddListItem(fInfo);
                        }
                    }
                }
            }
            finally
            {
                if (OnLoadDataComplete != null)
                {
                    OnLoadDataComplete();
                }
            }
        }
Beispiel #11
0
        private void DoGetFileInfoList()
        {
            if (OnGetFileInfoListStart != null)
            {
                OnGetFileInfoListStart();
            }

            try
            {
                ClearList();

                var uri     = new Uri("http://fias.nalog.ru/WebServices/Public/DownloadService.asmx");
                var ea      = new EndpointAddress(uri);
                var binding = new BasicHttpBinding
                {
                    MaxReceivedMessageSize = 1024 * 1024
                };
                using (var client = new DownloadServiceSoapClient(binding, ea))
                {
                    var position             = 0;
                    var downloadFileInfoList = client.GetAllDownloadFileInfo();
                    foreach (var dfi in downloadFileInfoList)
                    {
                        //DownloadFileInfo dfi = DownloadFileInfoList[8];
                        var fi = new FiasFileInfo();
                        AddListItem(fi);

                        fi.Checked     = false;
                        fi.Description = dfi.TextVersion;
                        fi.Url         = dfi.FiasDeltaXmlUrl ?? dfi.FiasCompleteXmlUrl;
                        fi.Version     = dfi.VersionId;

                        fi.FileName = RootFolder + "\\Data\\" + Path.GetFileName(Path.GetDirectoryName(fi.Url)) + "\\" + Path.GetFileName(fi.Url);
                        fi.IsExists = File.Exists(fi.FileName);

                        var req = (HttpWebRequest)WebRequest.Create(fi.Url);
                        req.Method = "HEAD"; using (var resp = (HttpWebResponse)req.GetResponse())
                        {
                            fi.Length = resp.ContentLength;
                        }
                        if (OnGetFileInfoProgress != null)
                        {
                            OnGetFileInfoProgress(position++, downloadFileInfoList.Length, string.Empty);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LastError = ex.InnerException == null ? ex.Message : ex.InnerException.Message;

                /*if (OnGetFileInfoListError != null)
                 *  OnGetFileInfoListError(0, 0, ex.InnerException.Message);*/
            }
            finally
            {
                if (OnGetFileInfoListComplete != null)
                {
                    OnGetFileInfoListComplete();
                }
            }
        }
Beispiel #12
0
 private void UnRarFile(FiasFileInfo aFileInfo)
 {
     using (var unrar = new Unrar())
     {
         unrar.Open(aFileInfo.FileName, Unrar.OpenMode.Extract);
         unrar.DestinationPath = Path.GetDirectoryName(aFileInfo.FileName) + "\\Extracted.tmp";
         unrar.ExtractionProgress += unrar_ExtractionProgress;
         while (unrar.ReadHeader())
         {
             if (stopEvent.WaitOne(0))
                 throw new StopException();
             unrar.Extract();
         }
     }
     Directory.Move(Path.GetDirectoryName(aFileInfo.FileName) + "\\Extracted.tmp", Path.GetDirectoryName(aFileInfo.FileName) + "\\Extracted");
     aFileInfo.Checked = false;
     aFileInfo.Extracted = true;
 }