Routine to download and unzip zip file on the server itself

Asked

Viewed 260 times

2

I need to develop on my website (ASP.NET MVC) a routine that downloads from another server a zip file made available weekly and unzip in my own hosting service.

This routine can be triggered by a button that checks the availability of the file and runs the procedure.

The zip file is made available by Box

I have the following url http://www.caixa.gov.br/Downloads/licitacoes-e-fornecedores-consultas-publicas/TR_Nova_Instantanea_v2.zip

I want to download this zip through routine and unpack it.

I am 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);
}

Updating: I entered your code into a new Controller to execute this command when I access, so.

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 arrives on this line it seems that the file has not been downloaded and gives an exception:

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

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

I am doing test on localhost, I have not tested on the server, because I have to develop the solution first by full.

Questions What it represents "./g" ?

I can perform this application test on localhost ?

  • What’s the mistake? What’s happening that you’re not getting?

  • I did the editing in my reply, because, really spoke to say in which location you want to save and then unzip the zip.

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/"));

Observing: zip/ and descompacta/ sane directories valid in their application Web.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.