Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 9 years, 9 months ago. Active 5 months ago. Viewed k times. Improve this question. Keyur Potdar 6, 6 6 gold badges 23 23 silver badges 37 37 bronze badges. I tried section Downloading a binary file and writing it to disk of this page which worked as a chram.
Add a comment. Active Oldest Votes. ZipFile StringIO. StringIO r. ZipFile io. Necessary cookies are absolutely essential for the website to function properly.
This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information. Non-necessary Non-necessary. What each of those various attributes mean isn't important to figure out now, it's just enough to know that they exist as part of every request for a web resource, whether it's a webpage, image file, data file, etc. Returning to our previous code snippet, let's assign the result of the requests.
I like using resp for the variable name — short for "response". If you want to get the text of a successful requests. Otherwise zipfileMemoryStream will be disposed when ZipArchive is disposed which prevents you from interacting with the stream. For every path in botFilePaths : Extract the file name from the bot file path and store it in botFileName. Create a ZipArchiveEntry by calling archive. CreateEntry passing in the file name.
This entry is where the file data will be stored. Get a writable Stream to write to the ZipArchiveEntry by calling entry. Open a readable FileStream to read the bot file by calling System.
OpenRead passing in the bot file path. At this point, you don't need to use the full namespace, but it will be required later to avoid collision between System.
Copy the data from the file stream to the zip entry stream. If you now want to read from the memory stream, you have to set the position of the stream back to the beginning. You can do this using. Lastly, do whatever you need to do with your in-memory ZIP file. If you eventually end up saving or downloading this ZIP archive to disk, it will look like this: Now that you know how to generate a ZIP file into a stream using ZipArchive and FileStreams , let's take a look at how you can send the stream to the browser in ASP.
The code below shows how you can send the zipFileMemoryStream from the previous code sample to the browser using the File method inherited from the Controller class: using System.
IO; using System. Compression; using System. Tasks; using Microsoft. Hosting; using Microsoft. Mvc; namespace WebZipIt. There is no Recycle Bin at the programming level. The Python interpreter just assumes you know what you're doing, and won't even throw an error or warning. It will just wipe out the existing file before writing to it. So consider this your warning to be incredibly mindful whenever you want to write a file to disk.
What is that newfile variable pointing to? Use the type function to find out:. For simplicity's sake, I'm just going to refer to it as a "file object" or file stream object. Let's use the Tab autocomplete to get its list of methods:. You can guess that the write function is what we want. But this object also has a read function…That's because it's a file object, and file objects can be written to or read from. It doesn't matter how we called the open function.
There is how Python reminds us that the file is not meant to be read from, since we called open with the "w" argument. Now that we have that cleared up, let's just write to the file. You can pass in a string object as the argument, and call write as many times as you want to:.
0コメント