Esempio n. 1
0
        private int ConvertFile(string sourcefile)
        {
            if (!File.Exists(sourcefile))
            {
                return(1);
            }
            try
            {
                TextReader reader = new FilterReader(sourcefile);
                using (reader)
                {
                    //                 char[] buffer = new char[0x5000];
                    //                 reader.Read(buffer, 0, 0x5000);
                    //                 string context = new string(buffer);
                    string context = reader.ReadToEnd();

                    calcWords(context);

                    context = Regex.Replace(context, "\n\r", " ", RegexOptions.IgnoreCase);

                    try
                    {
                        string name = GetFileName(sourcefile);
                        using (StreamWriter writer = new StreamWriter((outpath + @"\" + name + ".txt").Replace(@"\\", @"\"), false, Encoding.Default))
                        {
                            writer.Write(context);
                            writer.Close();
                        }
                        reader.Close();

                        return(0);
                    }
                    catch (Exception exception)
                    {
                        reader.Close();
                        return(3);
                    }
                }
            }
            catch (Exception e)
            {
                return(2);
            }
        }
Esempio n. 2
0
        private void btnGetText_Click(object sender, EventArgs e)
        {
            string         dbt_wfile = "";
            OpenFileDialog dlg       = new OpenFileDialog();

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                dbt_wfile = dlg.FileName;
                TextReader reader = new FilterReader(dbt_wfile);
                using (reader)
                {
                    MessageBox.Show(reader.ReadToEnd());
                }
                reader.Close();
            }
        }
Esempio n. 3
0
        public static int ConvertFile()
        {
            if (!File.Exists(sourcefile))
            {
                return((int)OutStatus.FileLoss);
            }
            try
            {
                TextReader reader = new FilterReader(sourcefile);
                using (reader)
                {
                    //                 char[] buffer = new char[0x5000];
                    //                 reader.Read(buffer, 0, 0x5000);
                    //                 string context = new string(buffer);
                    string context = reader.ReadToEnd();
                    context = Regex.Replace(context, "\n\r", " ", RegexOptions.IgnoreCase);

                    try
                    {
                        string txtfile = (outtxtpath + @"\" + fileid + ".txt").Replace(@"\\", @"\");
                        using (StreamWriter writer = new StreamWriter(txtfile, false, Encoding.Default))
                        {
                            writer.Write(context);
                            writer.Close();
                        }
                        reader.Close();
                        ExecuteRegexTxt(txtfile);
                        return((int)OutStatus.ConvertSuccess);
                    }
                    catch (Exception exception)
                    {
                        Console.WriteLine("保存txt文件发生异常" + exception);
                        return((int)OutStatus.TotxtFailed);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("打开文件失败" + e);
                return((int)OutStatus.TotxtFailed);
            }
        }
Esempio n. 4
0
        private static void TryReadFile(FileInfo file)
        {
            var          stream = file.OpenRead();
            FilterReader reader = null;

            try
            {
                FilterReaderOptions filterReaderOptions = new FilterReaderOptions();
                reader = new FilterReader(stream, file.Extension, filterReaderOptions);
                var result = reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                reader?.Close();
                stream?.Close();
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Get the textual representation of the Binary Data of the document using IFILTER
        /// </summary>
        /// <returns>The text of the document or null if we could not parse the document into text</returns>
        public virtual string GetTextFromDocumentBinary()
        {
            /*
             * The default is to save the binary data to a temporary location and
             * use IFilter to extract the text.  This should be a good catch-all for
             * all files that don't have a specific mechanism for extracting the
             * text of the file.
             */

            // If we have no bytes then we can't do anything.
            if (Bytes == null || Bytes.Length == 0)
            {
                // Log the problem.
                log.Error("Tried to extract text from empty bytes for file " + Name);
                return(null);
            }

            // Get the original file name without the extension
            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(Name);

            bool   success     = false;
            string newFileName = "";

            try
            {
                // Now try to generate a new temporary file name that we don't have in the temporary directory
                for (int i = 0; i < 50; i++)
                {
                    Random rand = new Random();
                    newFileName = "~/TemporaryFilesDirectory/" + fileNameWithoutExtension +
                                  Convert.ToString(rand.Next(100000)) + Extension;
                    newFileName = HttpContext.Current.Server.MapPath(newFileName);

                    // Try to see if this file exists
                    if (!File.Exists(newFileName))
                    {
                        success = true;
                        break;
                    }
                }

                if (!success)
                {
                    // We failed.  Log the problem.
                    log.Error("Failed to create a unique file to extract data. Last file tried is " + newFileName);
                    return(null);
                }
            }
            catch (Exception e)
            {
                // We failed.  Log the problem.
                log.Error("Failed to create a unique file to extract data for file " + Name, e);
                return(null);
            }

            FileStream theFileStream = null;

            try
            {
                // Now try to write the bytes to the newly created file

                theFileStream = File.Create(newFileName);
                theFileStream.Write(Bytes, 0, Bytes.Length);
                theFileStream.Close();
            }
            catch (Exception e)
            {
                // We failed to write the file.  Log the problem
                log.Error("Failed to write bytes to new file " + newFileName, e);

                // Try to close the stream, in case it is still open and delete the file
                try
                {
                    if (theFileStream != null)
                    {
                        theFileStream.Close();
                    }

                    if (File.Exists(newFileName))
                    {
                        File.Delete(newFileName);
                    }
                }
                catch
                {
                    // We don't do anything.  This is a best effort close and delete
                }

                return(null);
            }

            string       text           = null;
            FilterReader myFilterReader = null;

            // Now try to extract the text for the file
            try
            {
                myFilterReader = new FilterReader(newFileName);
                text           = myFilterReader.ReadToEnd();
                myFilterReader.Close();
            }
            catch (Exception e)
            {
                log.Error("Failed to parse text for file " + Name + " using IFilter", e);

                // Try to close the IFilter, in case it is still open
                try
                {
                    if (myFilterReader != null)
                    {
                        myFilterReader.Close();
                    }
                }
                catch
                {
                    // We don't do anything.  This is a best effort close.
                }
            }

            try
            {
                // Try to delete the temporary file
                if (File.Exists(newFileName))
                {
                    File.Delete(newFileName);
                }
            }
            catch (Exception e)
            {
                log.Error("Failed to delete temporary file " + newFileName, e);
            }

            return(text);
        }