Exemplo n.º 1
0
        string Repackage(string stagingDirectory, string fileName)
        {
            var supportedZipExtensions      = new[] { ".nupkg", ".zip" };
            var supportedJavaExtensions     = new[] { ".jar", ".war", ".ear", ".rar" };
            var supportedTarExtensions      = new[] { ".tar" };
            var supportedTarGZipExtensions  = new[] { ".tgz", ".tar.gz", ".tar.Z" };
            var supportedTarBZip2Extensions = new[] { "tar.bz", ".tar.bz2", ".tbz" };

            var lowercasedFileName = fileName.ToLower();

            using (var targetArchive = fileSystem.CreateTemporaryFile(string.Empty, out var targetArchivePath))
            {
                if (supportedZipExtensions.Any(lowercasedFileName.EndsWith) || supportedJavaExtensions.Any(lowercasedFileName.EndsWith))
                {
                    using (var archive = ZipArchive.Create())
                    {
                        archive.AddAllFromDirectory(stagingDirectory);
                        archive.SaveTo(targetArchive, CompressionType.Deflate);
                    }
                }
                else if (supportedTarExtensions.Any(lowercasedFileName.EndsWith))
                {
                    using (var archive = TarArchive.Create())
                    {
                        archive.AddAllFromDirectory(stagingDirectory);
                        archive.SaveTo(targetArchive, CompressionType.None);
                    }
                }
                else if (supportedTarGZipExtensions.Any(lowercasedFileName.EndsWith))
                {
                    using (var archive = TarArchive.Create())
                    {
                        archive.AddAllFromDirectory(stagingDirectory);
                        archive.SaveTo(targetArchive, CompressionType.GZip);
                    }
                }
                else if (supportedTarBZip2Extensions.Any(lowercasedFileName.EndsWith))
                {
                    using (var archive = TarArchive.Create())
                    {
                        archive.AddAllFromDirectory(stagingDirectory);
                        archive.SaveTo(targetArchive, CompressionType.BZip2);
                    }
                }
                else
                {
                    throw new ArgumentException($"Unable to compress file {fileName.ToLower()}, the extension is unsupported.");
                }

                return(targetArchivePath);
            }
        }