Пример #1
0
        /// <summary>
        /// 调用外部函数
        /// </summary>
        /// <param name="filepath">待匹配文件</param>
        /// <param name="key">待搜索键</param>
        /// <returns></returns>
        private void DocumentContainsKey(string filepath, string key, Dictionary <string, string> zipDic)
        {
            using (var process = new Process())
            {
                process.StartInfo.FileName               = @"java";
                process.StartInfo.UseShellExecute        = false;
                process.StartInfo.CreateNoWindow         = true;
                process.StartInfo.RedirectStandardInput  = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.Arguments              = @"-jar C:\Users\andys\source\repos\searchdocs\Csapp_ReadFile.jar";
                process.OutputDataReceived              += new DataReceivedEventHandler((sender, e) =>
                {
                    if (!string.IsNullOrEmpty(e.Data))
                    {
                        if (int.TryParse(e.Data, out int val))
                        {
                            if (val > 0)
                            {
                                //转为.zip路径
                                var isContained = false;
                                if (zipDic.ContainsKey(filepath))
                                {
                                    filepath = zipDic[filepath];
                                    if (listSet.Contains(filepath))
                                    {
                                        isContained = true;
                                    }
                                    else
                                    {
                                        //更新list中的.zip路径
                                        listSet.Add(filepath);
                                    }
                                }

                                var fileInfo = new FileInfo(filepath);
                                // 存入redis缓存
                                RedisHelper.ListPush(GetCombinedKey(key), filepath);
                                // 存入文件MD5摘要
                                RedisHelper.Set(GetMD5Key(filepath), MD5Checker.GetFileMD5(filepath));

                                // 如果.zip目录不在list中,加入UI
                                if (!isContained)
                                {
                                    FilesListView.Dispatcher.BeginInvoke(new Action(() =>
                                    {
                                        FilesListView.Items.Add(new FileItemInfo(fileInfo.Name, "file", filepath));
                                    }));
                                }
                            }
                            process.Kill();
                        }
                    }
                });

                process.Start();//启动程序
                process.BeginOutputReadLine();
                process.StandardInput.AutoFlush = true;
                process.StandardInput.WriteLine(filepath);
                process.StandardInput.WriteLine(key);
                process.WaitForExit();
            }
        }
Пример #2
0
        /// <summary>
        /// 根据key搜索当前目录下匹配的文件
        /// </summary>
        /// <param name="dir">当前文件夹</param>
        /// <param name="key">搜索关键字</param>
        public void SearchInSelectedDir(string dir, string key)
        {
            var swTotal = Stopwatch.StartNew();

            stopwatch.Start();
            FilesListView.Items.Clear();

            string combinedKey = GetCombinedKey(key);

            if (RedisHelper.Exists(combinedKey))
            {
                MessageBox.Show("从缓存获取成功");
                matchedFilenameList = RedisHelper.ListGet <string>(combinedKey);
                foreach (var matchedPath in matchedFilenameList)
                {
                    // 判断文件更新
                    string oriMD5 = RedisHelper.Get <string>(GetMD5Key(matchedPath));
                    string curMD5 = MD5Checker.GetFileMD5(matchedPath);
                    if (oriMD5 != curMD5)
                    {
                        // 摘要发生变化
                        // todo 移除缓存
                        RenewFile(matchedPath, key);
                    }
                }
                DispMatchedFiles();
            }
            else
            {
                //提前解压缩
                var sw    = Stopwatch.StartNew();
                var ziper = new Ziper();
                TR.ZipSearched.AddRange(ziper.extract(dir));
                sw.Stop();
                TR.Zip = sw.Elapsed.TotalMilliseconds * 1e6;

                TR.Doc = 0;
                TR.Img = 0;
                var files = Directory.GetFiles(curDirPath, "*.*", SearchOption.AllDirectories);
                foreach (var filename in files)
                {
                    if (IsImageFile(filename))
                    {
                        TR.ImgSearched.Add(filename);
                        Console.WriteLine(filename);
                        sw = Stopwatch.StartNew();
                        ImageContainsKey(filename, key);
                        sw.Stop();
                        TR.Img += sw.Elapsed.TotalMilliseconds * 1e6;
                    }
                    else if (IsDocFile(filename))
                    {
                        TR.DocSearched.Add(filename);
                        Console.WriteLine(filename);
                        sw = Stopwatch.StartNew();
                        DocumentContainsKey(filename, key, ziper.Table);
                        sw.Stop();
                        TR.Doc += sw.Elapsed.TotalMilliseconds * 1e6;
                    }
                }
                //清除被解压的文件夹
                ziper.ClearFile();
            }

            stopwatch.Stop();
            DispElapsedTime();

            swTotal.Stop();
            TR.Total = swTotal.Elapsed.TotalMilliseconds * 1e6;
        }