public void CryptDecryptFileWithTrueKey() { // arrange MyFile file = Factory.CreateFile(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Path.GetRandomFileName())); file.FileAppendAllText("secretInfo123123"); List <Entry> entries = new List <Entry>(); entries.Add(file); string key = "key123"; // act FileSystemContainer container = new FileSystemContainer(entries, false, true); container.Accept(new DESCryptoVisitor(true, key)); MyFile cryptFile; if (Factory.TryGetFile(file.FullPath + "_crypted", out cryptFile)) { entries.Clear(); entries.Add(cryptFile); container = new FileSystemContainer(entries, false, true); container.Accept(new DESCryptoVisitor(false, key)); } else { Assert.Fail("File " + file.FullPath + " is crypted, but " + file.FullPath + "_crypted " + "could not be found!"); } string decryptResult = file.FileReadAllLines()[0]; file.Delete(); // assert Assert.AreEqual("secretInfo123123", decryptResult); }
private string OnAskModelGetTXTStats(MyFile file) { if (Path.GetExtension(file.FullPath) == ".txt") { try { string[] lines = file.FileReadAllLines(); int countOfLines = lines.Length; char[] separators = new char[] { ' ', ',', '!', ':', ';', '"', '–', '.' }; List <string> words = new List <string>(); for (int i = 0; i < countOfLines; i++) { words.AddRange(lines[i].Split(separators)); } int countOfWords = words.Count(); var topTen = (from word in words where word.Length > 3 group word by word into grp orderby grp.Count() descending select(grp.Key) + ": " + grp.Count() + ".").Take(10).ToArray(); StringBuilder sb = new StringBuilder(); sb.AppendLine("Count of lines:"); sb.AppendLine(countOfLines.ToString()); sb.AppendLine("Count of words:"); sb.AppendLine(countOfWords.ToString()); sb.AppendLine("Top 10:"); foreach (var str in topTen) { sb.AppendLine(str); } string result = sb.ToString(); return(result); } catch (Exception exc) { return(exc.Message); } } return(string.Empty); }
public void AddFileToZipArchiveAndUnzip() { // arrange MyZipArchive zipArchive = Factory.CreateZipArchive(Path.Combine (Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".zip")); MyFile file = Factory.CreateFile(Path.Combine (Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Path.GetRandomFileName())); MyFolder tempFolder = Factory.GetSpecialFolder(Environment.SpecialFolder.ApplicationData); // act file.FileAppendAllText("content1"); file.MoveToDirectory(zipArchive); //перемещаем файл в архив if (file.Exists) { Assert.Fail("File " + file.FullPath + "is moved, but Exists is true!"); } var files = zipArchive.DirectoryGetFiles; if (files.Count != 1) { Assert.Fail("Archive contains " + files.Count + " files, expected 1!"); } files[0].CopyToDirectory(tempFolder); //копируем файл из архива на прежнее место if (!file.Exists) { Assert.Fail("File " + file.FullPath + "is unzipped, but Exists is false!"); } string fileContent = file.FileReadAllLines()[0]; // удаляем все файлы zipArchive.Delete(); file.Delete(); // assert Assert.AreEqual("content1", fileContent); }