18204

Zip file from URL not valid

Question:

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!

Answer1:

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

Answer2:

The example you provide will create an entry inside your zip file with the contents from the stream but you are doing nothing to save and return the actual zip file. You need to use the creation code from your second example.

// name of zip file string filename = "myfile.zip"; // filename of content string contentName = "mypage.html"; WebClient wc = new WebClient(); ZipFile zipFile = new ZipFile(); zipFile.Password = item.Password; Stream s = wc.OpenRead(myUrl); zipFile.AddeEntry(contentName, s); MemoryStream stream = new MemoryStream(); zipFile.Save(stream); zipFile.Dispose(); // could use using instead s.Dispose(); // could use using instead.... stream.Seek(0, SeekOrigin.Begin); return File(stream, "application/zip", fileName);

this will return a zip file with one file in it called content.html containing the contents of the url stream

Recommend

  • Powershell v2.0 Using multiple threads
  • Unzipping a file error
  • Zipping Files and Downloading them Using SaveAs Dialog
  • How to unit test ASP.NET bundles
  • Scope in JavaScript / jQuery [duplicate]
  • Filling web form via PowerShell does not recognize the values entered
  • Smart URL with optional parameters?
  • Where does the file get saved using “File file = new file(filename)” in Android
  • How to trick Node.js to load .js files as ES6 modules?
  • Want to understand iframe breakout code
  • Streaming screenshots over WebRTC as a video stream from iOS
  • Extract zip entries to another Zip file
  • Play WS (2.2.1): post/put large request
  • Bad request using file_get_contents for PUT request in PHP
  • How to create a file in java without a extension
  • Django rest serializer Breaks when data exists
  • Ajax Loaded meta Tags
  • Exchange data b/w iOS devices using Bluetooth 4.0
  • Master page gives error
  • Uncaught Error: Could not find module `ember-load-initializers`
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • Can Jackson SerializationFeature be overridden per field or class?
  • How to set/get protobuf's extension field in Go?
  • Sending data from AppleScript to FileMaker records
  • Running a C# exe file
  • Symfony2: How to get request parameter
  • Google cloud sdk not working when python points python3
  • Why winpcap requires both .lib and .dll to run?
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j
  • Authorize attributes not working in MVC 4
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • How to push additional view controllers onto NavigationController but keep the TabBar?