private Commands Parse()
 {
     using (var reader = _commandFile.OpenText())
     {
         var commands = Deserializer.Consume <Commands>(reader);
         return(commands);
     }
 }
        public void MockFileInfo_OpenText_ShouldReturnStringContentOfFile()
        {
            // Arrange
            MockFileSystem fileSystem = new MockFileSystem();

            fileSystem.AddFile(XFS.Path(@"c:\temp\file.txt"), new MockFileData(@"line 1\r\nline 2"));
            FileInfoBase fileInfo = fileSystem.FileInfo.FromFileName(XFS.Path(@"c:\temp\file.txt"));

            // Act
            string result;

            using (StreamReader streamReader = fileInfo.OpenText())
            {
                result = streamReader.ReadToEnd();
            }

            Assert.Equal(@"line 1\r\nline 2", result);
        }
Esempio n. 3
0
        public static IDictionary <string, string> Extract(this ZipArchive archive, string directoryName, bool preserveSymlinks = false)
        {
            IDictionary <string, string> symLinks = new Dictionary <string, string>();
            bool isSymLink = false;

            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                string path = Path.Combine(directoryName, entry.FullName);
                if (entry.Length == 0 && (path.EndsWith("/", StringComparison.Ordinal) || path.EndsWith("\\", StringComparison.Ordinal)))
                {
                    // Extract directory
                    FileSystemHelpers.CreateDirectory(path);
                }
                else
                {
                    FileInfoBase fileInfo = FileSystemHelpers.FileInfoFromFileName(path);

                    if (!fileInfo.Directory.Exists)
                    {
                        fileInfo.Directory.Create();
                    }
                    using (Stream zipStream = entry.Open(),
                           fileStream = fileInfo.Open(FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
                    {
                        zipStream.CopyTo(fileStream);
                    }

                    isSymLink = false;
                    string originalFileName = string.Empty;

                    if (!OSDetector.IsOnWindows())
                    {
                        try
                        {
                            using (Stream fs = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                            {
                                byte[] buffer = new byte[4];
                                fs.Read(buffer, 0, buffer.Length);
                                fs.Close();

                                var str = System.Text.Encoding.Default.GetString(buffer);
                                if (preserveSymlinks && str.StartsWith("../"))
                                {
                                    using (StreamReader reader = fileInfo.OpenText())
                                    {
                                        symLinks[entry.FullName] = reader.ReadToEnd();
                                    }
                                    isSymLink = true;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("Could not identify symlinks in zip file : " + ex.ToString());
                        }
                    }

                    try
                    {
                        fileInfo.LastWriteTimeUtc = entry.LastWriteTime.ToUniversalTime().DateTime;
                    }
                    catch (Exception)
                    {
                        //best effort
                    }

                    if (isSymLink)
                    {
                        fileInfo.Delete();
                    }
                }
            }
            return(symLinks);
        }