2020-06-17 13:08:22 +00:00
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.IO.Compression ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
2021-06-15 23:25:48 +00:00
namespace TBMM
2020-06-17 13:08:22 +00:00
{
public static class ZipArchiveExtensions
{ //https://stackoverflow.com/a/14795752/2703239
public static void ExtractToDirectory ( this ZipArchive archive , string destinationDirectoryName , bool overwrite )
{
if ( ! overwrite )
{
archive . ExtractToDirectory ( destinationDirectoryName ) ;
return ;
}
DirectoryInfo di = Directory . CreateDirectory ( destinationDirectoryName ) ;
string destinationDirectoryFullPath = di . FullName ;
foreach ( ZipArchiveEntry file in archive . Entries )
{
string completeFileName = Path . GetFullPath ( Path . Combine ( destinationDirectoryFullPath , file . FullName ) ) ;
if ( ! completeFileName . StartsWith ( destinationDirectoryFullPath , StringComparison . OrdinalIgnoreCase ) )
{
throw new IOException ( "Trying to extract file outside of destination directory. See this link for more info: https://snyk.io/research/zip-slip-vulnerability" ) ;
}
if ( file . Name = = "" )
{ // Assuming Empty for Directory
Directory . CreateDirectory ( Path . GetDirectoryName ( completeFileName ) ) ;
continue ;
}
file . ExtractToFile ( completeFileName , true ) ;
}
}
}
}