public void ZipArchiveAddFiles() { // arrange MyZipArchive zipArchive = Factory.CreateZipArchive(Path.Combine (Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".zip")); MyFolder folder = Factory.CreateFolder(Path.Combine (Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()))); string content = "S);PFE_NWaf9fAk"; MyFile file1 = Factory.CreateFile(folder.FullPath + "\\document.doc"); file1.FileAppendAllText(content); MyFile file2 = Factory.CreateFile(folder.FullPath + "\\documentx.docx"); file2.FileAppendAllText(content); MyFile file3 = Factory.CreateFile(folder.FullPath + "\\document.html"); file3.FileAppendAllText(content); MyFile file4 = Factory.CreateFile(folder.FullPath + "\\document.dot"); file4.FileAppendAllText(content); MyFile file5 = Factory.CreateFile(folder.FullPath + "\\page.doc"); file5.FileAppendAllText(content); MyFile file6 = Factory.CreateFile(folder.FullPath + "\\image.jpg"); file6.FileAppendAllText(content); HashSet <string> paths = new HashSet <string>(); paths.Add(file1.Name); paths.Add(file2.Name); paths.Add(file3.Name); paths.Add(file4.Name); paths.Add(file5.Name); paths.Add(file6.Name); // act folder.CopyToDirectory(zipArchive); var dirs = zipArchive.DirectoryGetFolders; if (dirs.Count != 1) { Assert.Fail("There is " + dirs.Count + " folders. Expected 1."); } var files = dirs[0].DirectoryGetFiles; if (files.Count != 6) { Assert.Fail("There is " + files.Count + " files in " + dirs[0].FullPath + ". Expected 6."); } foreach (var file in files) { if (!paths.Contains(file.Name)) { Assert.Fail(dirs[0].FullPath + "does not contain " + file.Name + "."); } else { paths.Remove(file.Name); } } // удаляем используемые entry folder.Delete(); zipArchive.Delete(); // assert Assert.IsTrue(paths.Count == 0); }
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); }