Uploading files: FileUpload.SaveAs or manually writing FileUpload.FileBytes - c#

Using the FileUpload control, please explain the difference between the following two methods of uploading file:
1. Using the FileUpload.SaveAs() method:
fileUploadControl.SaveAs(path)
2. Writing a byte array to disk from FileUpload.FileBytes using File.WriteAllBytes():
File.WriteAllBytes(path, fileUploadControl.FileBytes);
How would these compare when uploading large files?

These both have different purposes.
SaveAs lets you save as a file directly while WriteAllBytes gives you a byte array of contents.
Your file upload control will receive the bytes only after the file has been uploaded by the client, so there will be no difference in upload speeds.
A byte array is a value-type, so if you are passing copies of that around, note that it will create copies in memory whenever you pass it to functions.
I would use FileUpload.FileBytes when I want to access the bytes directly in memory and fileUploadControl.SaveAs whenever all I want to do is write the file to disk.

Related

Unpacking tar/BZ2 files using C#

I have a tar.bz2 file and I want to extract it to a directory. In the examples I only see option of compress or decompress however I want actually to extract or unpack.
Also tried ICSharpCode.SharpZipLib.BZip2 but I didn't find an option to unpack.
While you use a ZipInputStream for .zip files, you should use a BZip2InputStream for .bz2 files (and GZipInputStream for .gz files etc.).
Taken from:
How to decompress .bz2 file in C#?
Decompressing and unpacking are two different operations. A foo.tar.bz2 file is actually a foo.tar file which was then compressed using bz2.
So to get single files you have to do this in the opposite direction. I.e. first decompress it (which you managed to do with sharpziplib). The result of this decompression has then to be untared (which can also be done with sharpziplib) see the docs for details.

Folder structure in memory(compression and uncompression)

I need compress and uncompress strings only in memory, without saving it inside file.
It's easy to do that with one string but the problem appears when I need do this with many files. I don't want do compress every string creating array of bytes array. Is it possible to compress many strings into one object (zip with many files in memory)and uncompress this dynamically ? (yield)
Also I have to deal with big strings(xml files) and I copy this data from procedure into MemotySream. I'd like to just compress data from this streams

Find and replace data in file (without loading the entire thing)?

I want to replace some data in a file, however I do not know exactly where this 200MB file would contain it. Is it possible to find (and replace them with something else) these values without loading a 200mb+ file into the memory?
Searching the file is not a problem. What you need is to work with the FileStream which is available via File.Open method. You can read through the file up to the bytes you need to replace.
Problem arises when you need to insert something. The FileStream allows you to overwrite some or all of the file contents from a particular byte forth and to append new content to its end but it does not allow you to insert data in the middle of the file. In order to overcome this problem you are going to need a temporary file. If you agree to that you could do the following:
Open the FileStream on the original file.
Create a temporary file that will hold the draft version.
Search through the original file and copy all "good" data into temporary file up to the point where modifications are to be made.
Insert modified and new data into the temporary file.
Finish up the temporary file with the remaining "good" content from the original file.
Replace the original file with the temporary one.
Delete the temporary file.
You could use the Path.GetTempFileName method for convenient way of utilizing a temporary file.
P.S. If you modify an exe then you probably make replacements on text constants and you neither need to insert new bytes nor to remove any. In such a case you do not need to bother with the temporary file and the FileStream is all you need.
P.P.S. Working with the FileStream you decide on size of a buffer you read from file and write back. Keep in mind that this size is the tradeoff between memory consumption, I/O performance and complexity of your code. Choose wisely. I would make it per-byte for the first time and try to optimize increasing the buffer to say 64k when it works. You can count on the FileStream to buffer data; it is not performing disk I/O each time you request another byte from it. If you dive into buffering yourself then try not to fragment the Large Object Heap. The threshold for .NET 4.5 is 85000 bytes.
Just a thought, how about reading your file line by line or may be in chunk of bytes and see in each chunk if u have the data that needs to be replaced. Also while reading make sure get the file pointer till where you have read the file so that when u find the match then u can go back to that location and over write those exact bytes which u have targetted.

Client.DownloadFile() C# only write to file when download successful

Here is my code to download file using C#
Client.DownloadFile("link","file");
I want to modify it in such a way so that it will create file, only if download is successful. Currently, if I already have a file in the folder where I am trying to download, the above code deletes current file, if download is not successful.
Any suggestions?
Regards,
I see two ways:
Download the file to a temporary name, and when the download is complete you move it to the right place.
Use the DownloadData method to get the data as a byte array instead, and File.WriteAllBytes to save it to the file when you have all the data.
The first option works better for large files, and the second for small files.

HttpPostedFileBase SaveAs vs. InputStream

Assuming I'm just saving files to a web server.
What is the difference of saving an uploaded file using the SaveAs method versus processing the file via InputStream?
Is there a performance difference?
Can both accomplish large file size uploading?
What is the difference of saving an uploaded file using the SaveAs method versus processing the file via InputStream?
Using SaveAs will just push the file to the file system. Processing using the input stream will allow you to perform any number of tasks - save to the file system, write to another stream, etc.
Is there a performance difference?
Depends on what you do. If you're comparing SaveAs to manually saving the file using the stream, then the difference is negligible.
Can both accomplish large file size uploading?
Yes.

Categories