iT邦幫忙

0

ASP.NET Core MVC 上傳壓縮檔,並解壓縮

我能夠做上傳檔案,但要解壓縮檔案,不知道該怎麼做,有大大知道嗎

public impExcelController(IWebHostEnvironment Env, testDbContext context)
{
_context = context;

_uploadFolder_zip = $@"{Env.WebRootPath}\zip";

}

private string[] Equi_Extension = { ".zip" };
private string sourcePath;
private string targetPath;
private string fileName;
[Route("Equi")]
[HttpPost]
public async Task Equipotential(List files)
{
var size = files.Sum(f => f.Length);

foreach (var file in files)
{

    var tufa = true;
    var ext = Path.GetExtension(file.FileName).ToLowerInvariant();

    if (string.IsNullOrEmpty(ext) || !Equi_Extension.Contains(ext))
    {
        // The extension is invalid ... discontinue processing the file
        tufa = false;
        return Ok(new { title = "上傳失敗,請檢查檔案格式是否正確" });
    }

    else if (tufa == true)
    {
        if (file.Length > 0)
        {
            fileName = "testzip.zip";
            sourcePath = $@"{_uploadFolder_zip}";
            targetPath = $@"{_uploadFolder_zip}\Backup";
            // 抓取現在 年月日時分秒
            string time = DateTime.Now.ToString("yyyyMMddHHmmss");

            // 判斷資料夾若不存在,則新增資料夾
            if (System.IO.Directory.Exists(targetPath) == false)
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }

            // 複製檔案,檔名加上當前日期,備份到 Backup資料夾
            System.IO.File.Copy(Path.Combine(sourcePath, fileName), Path.Combine(targetPath, time + fileName), true);

            // 覆蓋原有檔案
            var path = $@"{_uploadFolder_layer}\{"contour.geojson"}";
            using (var stream = System.IO.File.Create(path))
            {
                await file.CopyToAsync(stream);
            }
        }
    }
}
return Ok(new { title = "上傳成功!!!" });

}

1 個回答

我要發表回答

立即登入回答