Extracts a ZIP archive.
Inheritance: ArchiveExtractor
Esempio n. 1
0
        public static ArchiveExtractor Create([NotNull] Stream stream, [NotNull] string target, [CanBeNull] string mimeType)
        {
            #region Sanity checks
            if (stream == null) throw new ArgumentNullException(nameof(stream));
            if (string.IsNullOrEmpty(target)) throw new ArgumentNullException(nameof(target));
            #endregion

            ArchiveExtractor extractor;
            switch (mimeType)
            {
                case Archive.MimeTypeZip:
                    extractor = new ZipExtractor(stream, target);
                    break;
                case Archive.MimeTypeTar:
                    extractor = new TarExtractor(stream, target);
                    break;
                case Archive.MimeTypeTarGzip:
                    extractor = new TarGzExtractor(stream, target);
                    break;
                case Archive.MimeTypeTarBzip:
                    extractor = new TarBz2Extractor(stream, target);
                    break;
                case Archive.MimeTypeTarLzma:
                    extractor = new TarLzmaExtractor(stream, target);
                    break;
                case Archive.MimeTypeTarXz:
                    extractor = new TarXzExtractor(stream, target);
                    break;
                case Archive.MimeTypeRubyGem:
                    extractor = new RubyGemExtractor(stream, target);
                    break;
                case Archive.MimeType7Z:
                    extractor = new SevenZipExtractor(stream, target);
                    break;
                case Archive.MimeTypeCab:
                    extractor = new CabExtractor(stream, target);
                    break;
                case Archive.MimeTypeMsi:
                    throw new NotSupportedException("MSIs can only be accessed as local files, not as streams!");
                default:
                    throw new NotSupportedException(string.Format(Resources.UnsupportedArchiveMimeType, mimeType));
            }

            return extractor;
        }
Esempio n. 2
0
        public void TestRejectParentDirectoryEntry()
        {
            var builder = new PackageBuilder();
            builder.AddFolder("..");

            using (var archiveStream = File.Create(Path.Combine(_sandbox, "ar.zip")))
            {
                builder.GeneratePackageArchive(archiveStream);
                archiveStream.Seek(0, SeekOrigin.Begin);
                using (var extractor = new ZipExtractor(archiveStream, _sandbox))
                    extractor.Invoking(x => x.Run()).ShouldThrow<IOException>(because: "ZipExtractor must not accept archives with '..' as entry");
            }
        }
Esempio n. 3
0
        public void TestExtractToCustomDestination()
        {
            var builder = new PackageBuilder()
                .AddFile("emptyFile", new byte[] {});

            using (var archiveStream = File.Create(Path.Combine(_sandbox, "ar.zip")))
            {
                builder.GeneratePackageArchive(archiveStream);
                archiveStream.Seek(0, SeekOrigin.Begin);

                const string message = "ZipExtractor should correctly extract empty files in an archive to custom destination";
                using (var extractor = new ZipExtractor(archiveStream, _sandbox) {Destination = "custom"})
                    extractor.Invoking(x => x.Run()).ShouldNotThrow();
                File.Exists(Path.Combine(_sandbox, "custom", "emptyFile")).Should().BeTrue(because: message);
                File.ReadAllBytes(Path.Combine(_sandbox, "custom", "emptyFile")).Should().BeEmpty(because: message);
            }
        }
Esempio n. 4
0
        public void TestExtractUnixArchiveWithSymlink()
        {
            using (var extractor = new ZipExtractor(typeof(ZipExtractorTest).GetEmbeddedStream("testArchive.zip"), _sandbox))
                extractor.Run();

            string target;
            string source = Path.Combine(_sandbox, "symlink");
            if (UnixUtils.IsUnix) FileUtils.IsSymlink(source, out target).Should().BeTrue();
            else CygwinUtils.IsSymlink(source, out target).Should().BeTrue();

            target.Should().Be("subdir1/regular", because: "Symlink should point to 'regular'");
        }
Esempio n. 5
0
        public void TestExtractUnixArchiveWithExecutable()
        {
            using (var extractor = new ZipExtractor(typeof(ZipExtractorTest).GetEmbeddedStream("testArchive.zip"), _sandbox))
                extractor.Run();

            if (UnixUtils.IsUnix)
                FileUtils.IsExecutable(Path.Combine(_sandbox, "subdir2/executable")).Should().BeTrue(because: "File 'executable' should be marked as executable");
            else
            {
                string xbitFileContent = File.ReadAllText(Path.Combine(_sandbox, FlagUtils.XbitFile)).Trim();
                xbitFileContent.Should().Be("/subdir2/executable");
            }
        }
        public void TestExtractUnixArchiveWithSymlink()
        {
            using (var extractor = new ZipExtractor(this.GetEmbedded("testArchive.zip"), _sandbox))
                extractor.Run();

            string target;
            if (UnixUtils.IsUnix)
                Assert.IsTrue(FileUtils.IsSymlink(Path.Combine(_sandbox, "symlink"), out target));
            else
            {
                string symlinkFileContent = File.ReadAllText(Path.Combine(_sandbox, FlagUtils.SymlinkFile)).Trim();
                Assert.AreEqual("/symlink", symlinkFileContent);
                target = File.ReadAllText(Path.Combine(_sandbox, "symlink"));
            }
            Assert.AreEqual("subdir1/regular", target, "Symlink should point to 'regular'");
        }
        public void TestExtractUnixArchiveWithExecutable()
        {
            using (var extractor = new ZipExtractor(this.GetEmbedded("testArchive.zip"), _sandbox))
                extractor.Run();

            if (UnixUtils.IsUnix)
                Assert.IsTrue(FileUtils.IsExecutable(Path.Combine(_sandbox, "subdir2/executable")), "File 'executable' should be marked as executable");
            else
            {
                string xbitFileContent = File.ReadAllText(Path.Combine(_sandbox, FlagUtils.XbitFile)).Trim();
                Assert.AreEqual("/subdir2/executable", xbitFileContent);
            }
        }
        public void TestExtractToCustomDestination()
        {
            var builder = new PackageBuilder()
                .AddFile("emptyFile", new byte[] {});

            using (var archiveStream = File.Create(Path.Combine(_sandbox, "ar.zip")))
            {
                builder.GeneratePackageArchive(archiveStream);
                archiveStream.Seek(0, SeekOrigin.Begin);

                const string message = "ZipExtractor should correctly extract empty files in an archive to custom destination";
                using (var extractor = new ZipExtractor(archiveStream, _sandbox) {Destination = "custom"})
                    Assert.DoesNotThrow(() => extractor.Run());
                Assert.IsTrue(File.Exists(new[] {_sandbox, "custom", "emptyFile"}.Aggregate(Path.Combine)), message);
                Assert.AreEqual(new byte[] {}, File.ReadAllBytes(new[] {_sandbox, "custom", "emptyFile"}.Aggregate(Path.Combine)), message);
            }
        }