示例#1
0
        public void RootPathIsRespected()
        {
            using (var extractDirectory = new Utils.TempDir())
                using (var tarFileName = new Utils.TempFile())
                    using (var tempDirectory = new Utils.TempDir())
                    {
                        tempDirectory.CreateDummyFile();

                        using (var tarFile = File.Open(tarFileName.Filename, FileMode.Create))
                        {
                            using (var tarOutputStream = TarArchive.CreateOutputTarArchive(tarFile))
                            {
                                tarOutputStream.RootPath = tempDirectory.Fullpath;
                                var entry = TarEntry.CreateEntryFromFile(tempDirectory.Fullpath);
                                tarOutputStream.WriteEntry(entry, true);
                            }
                        }

                        using (var file = File.OpenRead(tarFileName.Filename))
                        {
                            using (var archive = TarArchive.CreateInputTarArchive(file, Encoding.UTF8))
                            {
                                archive.ExtractContents(extractDirectory.Fullpath);
                            }
                        }

                        var expectationDirectory = new DirectoryInfo(tempDirectory.Fullpath);
                        foreach (var checkFile in expectationDirectory.GetFiles("", SearchOption.AllDirectories))
                        {
                            var relativePath = checkFile.FullName.Substring(expectationDirectory.FullName.Length + 1);
                            FileAssert.Exists(Path.Combine(extractDirectory.Fullpath, relativePath));
                            FileAssert.AreEqual(checkFile.FullName, Path.Combine(extractDirectory.Fullpath, relativePath));
                        }
                    }
        }
示例#2
0
        private void TestFileNames(IEnumerable <string> names)
        {
            var zippy = new FastZip();

            using (var tempDir = new Utils.TempDir())
                using (var tempZip = new Utils.TempFile())
                {
                    int nameCount = 0;
                    foreach (var name in names)
                    {
                        tempDir.CreateDummyFile(name);
                        nameCount++;
                    }

                    zippy.CreateZip(tempZip.Filename, tempDir.Fullpath, true, null);

                    using (ZipFile z = new ZipFile(tempZip.Filename))
                    {
                        Assert.AreEqual(nameCount, z.Count);
                        foreach (var name in names)
                        {
                            var index = z.FindEntry(name, true);

                            Assert.AreNotEqual(index, -1, "Zip entry \"{0}\" not found", name);

                            var entry = z[index];

                            if (ZipStrings.UseUnicode)
                            {
                                Assert.IsTrue(entry.IsUnicodeText, "Zip entry #{0} not marked as unicode", index);
                            }
                            else
                            {
                                Assert.IsFalse(entry.IsUnicodeText, "Zip entry #{0} marked as unicode", index);
                            }

                            Assert.AreEqual(name, entry.Name);

                            var nameBytes = string.Join(" ", Encoding.BigEndianUnicode.GetBytes(entry.Name).Select(b => b.ToString("x2")));

                            Console.WriteLine($" - Zip entry: {entry.Name} ({nameBytes})");
                        }
                    }
                }
        }
示例#3
0
        public void ContentEqualAfterAfterArchived([Values(0, 1, 64)] int contentSize)
        {
            using (var sourceDir = new Utils.TempDir())
                using (var targetDir = new Utils.TempDir())
                    using (var zipFile = Utils.GetDummyFile(0))
                    {
                        var sourceFile    = sourceDir.CreateDummyFile(contentSize);
                        var sourceContent = File.ReadAllBytes(sourceFile);
                        new FastZip().CreateZip(zipFile.Filename, sourceDir.Fullpath, true, null);

                        Assert.DoesNotThrow(() =>
                        {
                            new FastZip().ExtractZip(zipFile.Filename, targetDir.Fullpath, null);
                        }, "Exception during extraction of test archive");

                        var targetFile    = Path.Combine(targetDir.Fullpath, Path.GetFileName(sourceFile));
                        var targetContent = File.ReadAllBytes(targetFile);

                        Assert.AreEqual(sourceContent.Length, targetContent.Length, "Extracted file size does not match source file size");
                        Assert.AreEqual(sourceContent, targetContent, "Extracted content does not match source content");
                    }
        }