Example #1
0
        public void Test_GetFileCount_Return_3_When_Adding_3_Files([Values(ArchiveType.Zip, ArchiveType.GZip)] ArchiveType archiveType)
        {
            // Arrange
            var testFiles = new List <DotNetHelper_IO.FileObject>()
            {
                RandomTestFileNoExtension, RandomTestFileNoExtension, RandomTestFileNoExtension
            };

            if (archiveType == ArchiveType.GZip)
            {
                testFiles.RemoveAt(2);
                testFiles.RemoveAt(1);
            }
            var zipFile = new ZipFileObject(Path.Combine(TestFolder.FullName, "Test" + CompressExtensionHelper.ZipExtensionLookup[archiveType]), archiveType);

            // Act
            foreach (var file in testFiles)
            {
                file.Write(file.Name);                 // write file name to file
                zipFile.Add(file);
            }

            // Assert
            Assert.That(zipFile.GetEntriesCount(), Is.EqualTo(testFiles.Count));
            //	Assert.That(zipFile.GetArchiveEntries().Count(), Is.EqualTo(3));
        }
Example #2
0
        public void ZipFileObjectRetrievesObjects()
        {
            IIoRepository repo        = new IoRepository();
            FileInfo      zipFileInfo = new FileInfo(path.Combine(TestDir.FullName, "test.zip"));

            using (var zipObject = new ZipFileObject(zipFileInfo, repo))
            {
                Assert.AreEqual(0, zipObject.GetContents().Count(), "should be no root objects");
            }
        }
        /// <summary>
        /// I couldn't find a way to modify the existing archive using ZipArchive.Open so were just going to create a new instance and copy the existing content to it
        /// </summary>
        /// <param name="zipFileObject"></param>
        /// <returns></returns>
        internal static IWritableArchive CopyContentToNewWritableArchive(this ZipFileObject zipFileObject)
        {
            var newArchive = zipFileObject.GetWritableArchive();

            using (var currentArchive = zipFileObject.GetReadableArchive())
            {
                using (currentArchive)
                {
                    foreach (var entry in currentArchive.Entries)
                    {
                        var memoryStream = new MemoryStream();
                        entry.WriteTo(memoryStream);
                        newArchive.AddEntry(entry.Key, memoryStream, true, entry.Size, entry.LastModifiedTime);
                    }
                }
            }
            return(newArchive);
        }
Example #4
0
        public void Test_GetArchiveEntries_Return_Correct_Content([Values(ArchiveType.Zip, ArchiveType.GZip, ArchiveType.Tar)] ArchiveType archiveType)
        {
            if (archiveType == ArchiveType.Rar || archiveType == ArchiveType.SevenZip)
            {
                return;
            }
            // Arrange
            var testFiles = new List <DotNetHelper_IO.FileObject>()
            {
                RandomTestFileNoExtension, RandomTestFileNoExtension, RandomTestFileNoExtension
            };

            if (archiveType == ArchiveType.GZip)
            {
                testFiles.RemoveAt(2);
                testFiles.RemoveAt(1);
            }
            var zipFile = new ZipFileObject(Path.Combine(TestFolder.FullName, $"Test{CompressExtensionHelper.ZipExtensionLookup[archiveType]}"), archiveType);

            // Act
            foreach (var file in testFiles)
            {
                file.Write(file.Name);                 // write file name to file
                zipFile.Add(file);
            }


            using (var filesInZip = zipFile.GetReadableArchive())
            {
                foreach (var file in filesInZip.Entries)
                {
                    using (var archiveFileStream = file.OpenEntryStream())
                    {
                        var content = archiveFileStream.ReadToString();
                        Assert.That(content, Is.EqualTo(file.Key));
                    }
                }
            }

            // Assert
        }
Example #5
0
        public void ZipFileObjectDirectories()
        {
            IIoRepository repo        = new IoRepository();
            FileInfo      zipFileInfo = new FileInfo(path.Combine(TestDir.FullName, "test.zip"));

            System.IO.File.CreateText(path.Combine(TestDir.FullName, "test2.txt")).Close();
            TestZip.AddFile(path.Combine(TestDir.FullName, "test2.txt"), "projects");
            TestZip.Save();
            foreach (var entry in TestZip.Entries)
            {
                Debug.WriteLine("Zip entry name: " + entry.FileName);
            }
            Debug.WriteLine("");
            using (var zipObject = new ZipFileObject(zipFileInfo, repo))
            {
                foreach (var item in zipObject.GetContents())
                {
                    Debug.WriteLine(item.FullName);
                }
                Assert.AreEqual(2, zipObject.GetContents().Count(), "should be 2 root objects");
                Assert.AreEqual(1, zipObject.GetDirectories().Count(), "should be 1 root dir");
                Assert.AreEqual(1, zipObject.GetFiles().Count(), "should be 1 root file");
            }
        }
Example #6
0
 public void CanCreateZipFileObject()
 {
     IIoRepository repo        = new IoRepository();
     FileInfo      zipFileInfo = new FileInfo(path.Combine(TestDir.FullName, "test.zip"));
     var           zipObject   = new ZipFileObject(zipFileInfo, repo);
 }