Routine to download and unzip zip file on the server itself

2

I need to develop a routine on my site (ASP.NET MVC) that will download a weekly zip file from another server and unzip it on my own hosting service.

This routine can be triggered via a button that checks the file's availability and executes the procedure.

The zip file is available from the

I have the following url link

I want to download this zip through the routine and unzip it.

I'm trying to use the ZipFile library, but I did not succeed.

response.ContentType = "application/zip";
response.AddHeader("content-disposition", "attachment; filename=" + outputFileName);
using (ZipFile zipfile = new ZipFile()) {
  zipfile.AddSelectedFiles("*.*", folderName, includeSubFolders);
  zipfile.Save(response.OutputStream);
}

Update : I've inserted your code into a new Controller to execute this command when I access it, and so on.

public async Task<ActionResult> Index()
{
   HttpClient web = new HttpClient();
   string url = "http://www.caixa.gov.br/Downloads/";
   url += "licitacoes-e-fornecedores-consultas-publicas/TR_Nova_Instantanea_v2.zip";
   byte[] data = await web.GetByteArrayAsync(url);
   System.IO.File.WriteAllBytes("p.zip", data);
   ZipFile.ExtractToDirectory("p.zip", "./g");

    return View();
 }

But when it comes to this line it looks like the file has not been downloaded and gives an exception:

System.IO.File.WriteAllBytes("p.zip", data);

Access to path 'C: \ Program Files (x86) \ IIS Express \ p.zip' has been denied.

I'm running test on localhost, I have not tested on the server since I have to first develop the solution by complete.

Questions What does "./g" mean?

Can I test this application on localhost ?

    
asked by anonymous 30.10.2017 / 21:58

1 answer

1

Using HttpClient and the System.IO.Compression.ZipFile installation via nuget , with a minimal example:

HttpClient web = new HttpClient();
string url = "http://www.caixa.gov.br/Downloads/";
url += "licitacoes-e-fornecedores-consultas-publicas/TR_Nova_Instantanea_v2.zip";
byte[] data = await web.GetByteArrayAsync(url);
System.IO.File.WriteAllBytes(Server.MapPath("zip/p.zip"), data);
ZipFile.ExtractToDirectory(Server.MapPath("zip/p.zip"), Server.MapPath("descompacta/"));

Note zip/ and descompacta/ are valid directories in your Web application.

    
30.10.2017 / 23:33