Search code examples
c#webclientdotnetzip

Zip file from URL not valid


I followed another post to be able to zip the content of an URL..

When I click my button Download, I "zip" the content of the URL and I save it in the default download folder...

So far this is my code:

WebClient wc = new WebClient(); 

ZipFile zipFile = new ZipFile();

string filename = "myfile.zip";

zipFile.Password = item.Password;

Stream s = wc.OpenRead(myUrl); 

zipFile.AddeEntry(filename, s);

return File(s, "application/zip", filename);

it´s similar to this one (which zips the content of a folder... ) (It works correctly)

ZipFile zipFile = new ZipFile();

zipFile.Password = item.Password;

zipFile.AddDirectory(sourcePath, "");

MemoryStream stream = new MemoryStream();
zipFile.Save(stream);
zipFile.Dispose();
stream.Seek(0, SeekOrigin.Begin);

return File(stream, "application/zip", fileName);

So, I want to do exactly the same with an URL..

THanks!


Solution

  • at the end I use this code and It works like I wanted...

    Thanks to all again!

     string fileName = "filename" + ".zip";
    
     MemoryStream stream = new MemoryStream();
    
     ZipFile zipFile = new ZipFile();     
    
     WebRequest webRequest = WebRequest.Create(myUrl);
    
     webRequest.Timeout = 1000;
    
     WebResponse webResponse = webRequest.GetResponse();
    
     using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
     {
         string content = reader.ReadToEnd();
         zipFile.AddEntry("myfile.txt", content);
     }
    
     zipFile.Save(stream);
     zipFile.Dispose();
     stream.Seek(0, SeekOrigin.Begin);
    
     return File(stream, "application/zip", fileName);