private XDocument ReadResultsFile(FileInfoBase testResultsFile)
        {
            XDocument document;
            using (var stream = testResultsFile.OpenRead())
            {
                using (var streamReader = new System.IO.StreamReader(stream))
                {
                    string content = streamReader.ReadToEnd();

                    int begin = content.IndexOf("<!-- Pickles Begin", StringComparison.Ordinal);

                    content = content.Substring(begin);

                    content = content.Replace("<!-- Pickles Begin", string.Empty);

                    int end = content.IndexOf("Pickles End -->", System.StringComparison.Ordinal);

                    content = content.Substring(0, end);

                    content = content.Replace("&lt;", "<").Replace("&gt;", ">");

                    var xmlReader = XmlReader.Create(new System.IO.StringReader(content));
                    document = XDocument.Load(xmlReader);
                }
            }

            return document;
        }
Пример #2
0
 public static void AddFile(this ZipArchive zipArchive, FileInfoBase file, string directoryNameInArchive)
 {
     string fileName = Path.Combine(directoryNameInArchive, file.Name);
     ZipArchiveEntry entry = zipArchive.CreateEntry(fileName, CompressionLevel.Fastest);
     using (Stream zipStream = entry.Open(),
                   fileStream = file.OpenRead())
     {
         fileStream.CopyTo(zipStream);
     }
 }
Пример #3
0
        public XDocument Load(FileInfoBase fileInfo)
        {
            XDocument document;
            using (var stream = fileInfo.OpenRead())
            {
                document = this.Load(stream);
            }

            return document;
        }
Пример #4
0
 public XDocument Load(FileInfoBase fileInfo)
 {
   XDocument document;
   using (var stream = fileInfo.OpenRead())
   {
     XmlReader xmlReader = XmlReader.Create(stream);
     document = XDocument.Load(xmlReader);
     stream.Close();
   }
   return document;
 }
 private List<Feature> ReadResultsFile(FileInfoBase testResultsFile)
 {
     List<Feature> result;
       using (var stream = testResultsFile.OpenRead())
       {
     using (var reader = new StreamReader(stream))
     {
       result = JsonConvert.DeserializeObject<List<Feature>>(reader.ReadToEnd());
     }
       }
       return result;
 }
Пример #6
0
        /// <summary>
        /// Loads a file from the file name.
        /// </summary>
        /// <param name="fileInfo">The file info.</param>
        /// <returns>The file's content.</returns>
        protected string LoadFile(FileInfoBase fileInfo)
        {
            // Data validation
            if (null == fileInfo)
                throw new ArgumentNullException("fileInfo");

            // Load the file content
            MemoryStream memoryStream = new MemoryStream();
            using (StreamReader streamReader = new StreamReader(fileInfo.OpenRead()))
            using (var fileWriter = new StreamWriter(memoryStream))
            {
                fileWriter.Write(streamReader.ReadToEnd());
            }

            // Read the code snippet from the file
            return Encoding.UTF8.GetString(memoryStream.ToArray());
        }
        public static IFileReader CreateFromFileName(FileInfoBase fileInfoBase)
        {
            if (IsCsvFile(fileInfoBase.FullName))
            {
                var reader = new CsvHelper.CsvReader(new StreamReader(fileInfoBase.OpenRead()), new CsvConfiguration
                {
                    HasHeaderRecord = true,
                    Delimiter = ";"
                });

                return new CsvReader(reader);
            }

            if (IsExcelFile(fileInfoBase.FullName))
            {
                var excelReader = ExcelReaderFactory.CreateOpenXmlReader(fileInfoBase.Open(FileMode.Open, FileAccess.Read));
                excelReader.IsFirstRowAsColumnNames = true;
                return new ExcelReader(excelReader);
            }

            throw new InvalidOperationException();
        }
Пример #8
0
 private static string HashDelivery(FileInfoBase file)
 {
     using (var fs = file.OpenRead())
     using (var bs = new BufferedStream(fs))
     {
         using (var sha1 = new SHA1Managed())
         {
             var hash = sha1.ComputeHash(bs);
             var formatted = new StringBuilder(2 * hash.Length);
             foreach (var b in hash)
             {
                 formatted.AppendFormat("{0:X2}", b);
             }
             return formatted.ToString();
         }
     }
 }
Пример #9
0
 private XDocument ReadResultsFile(FileInfoBase testResultsFile)
 {
     XDocument document;
       using (var stream = testResultsFile.OpenRead())
       {
     var xmlReader = XmlReader.Create(stream);
     document = XDocument.Load(xmlReader);
     stream.Close();
       }
       return document;
 }
 public static double GetHeightToWidthRatio(FileInfoBase relativePath)
 {
     using (var stream = relativePath.OpenRead())
     {
         var image = Image.FromStream(stream);
         return ((double)image.Height) / ((double)image.Width);
     }
 }
        /// <summary>
        /// Retrieves the build script contained in the given file.
        /// </summary>
        /// <param name="file">The file containing a build script</param>
        /// <returns>A build script.</returns>
        protected async Task<ScriptFile> GetScriptAsync(FileInfoBase file)
        {
            // Parse file name and path
            var typeName = file.Directory.Name;
            var schemaName = file.Directory.Parent.Name;
            var fileName = Path.GetFileNameWithoutExtension(file.FullName);
            if(!_objectTypes.ContainsKey(typeName))
            {
                return new ScriptFile(new DatabaseObject(ServerName, DatabaseName, schemaName.TrimObjectName(), fileName.TrimObjectName()), new UnexpectedObjectTypeError(typeName));
            }
            var objectType = _objectTypes[typeName];
            var dbObject = new TypedDatabaseObject(ServerName, DatabaseName, schemaName.TrimObjectName(), fileName.TrimObjectName(), objectType);

            // Read file contents
            string content;
            try
            {
                using (var fileInputStream = file.OpenRead())
                {
                    using (var fileReader = new StreamReader(fileInputStream))
                    {
                        content = await fileReader.ReadToEndAsync();
                    }
                }
            }
            catch (DirectoryNotFoundException)
            {
                return new ScriptFile(dbObject, GetDropScript(dbObject), _sqlParser);
            }
            catch (FileNotFoundException)
            {
                return new ScriptFile(dbObject, GetDropScript(dbObject), _sqlParser);
            }

            // Parse script file
            var script = new ScriptFile(dbObject, content, _sqlParser);

            return script;
        }
Пример #12
0
        private static bool BytesEqual(FileInfoBase file1, FileInfoBase file2)
        {
            if (file1.Length != file2.Length)
            {
                return false;
            }

            int bufferCount = (int)Math.Ceiling((double)file1.Length / BufferSize);
            using (Stream sourceStream = file1.OpenRead())
            using (Stream targetStream = file2.OpenRead())
            {
                byte[] sourceBuffer = new byte[BufferSize];
                byte[] targetBuffer = new byte[BufferSize];
                for (int i = 0; i < bufferCount; i++)
                {
                    sourceStream.Read(sourceBuffer, 0, BufferSize);
                    targetStream.Read(targetBuffer, 0, BufferSize);
                    if (BitConverter.ToInt64(sourceBuffer, 0) != BitConverter.ToInt64(targetBuffer, 0))
                    {
                        return false;
                    }
                }
            }
            return true;
        }
Пример #13
-1
        public static void AddFile(this ZipArchive zipArchive, FileInfoBase file, ITracer tracer, string directoryNameInArchive)
        {
            Stream fileStream = null;
            try
            {
                fileStream = file.OpenRead();
            }
            catch (Exception ex)
            {
                // tolerate if file in use.
                // for simplicity, any exception.
                tracer.TraceError(String.Format("{0}, {1}", file.FullName, ex));
                return;
            }

            try
            {
                string fileName = ForwardSlashCombine(directoryNameInArchive, file.Name);
                ZipArchiveEntry entry = zipArchive.CreateEntry(fileName, CompressionLevel.Fastest);
                entry.LastWriteTime = file.LastWriteTime;

                using (Stream zipStream = entry.Open())
                {
                    fileStream.CopyTo(zipStream);
                }
            }
            finally
            {
                fileStream.Dispose();
            }
        }