23

I have an byte array and I want to read the byte array into a FileStream. Below is my sample of code:

string fileName = "test.txt"; byte[] file = File.ReadAllBytes(Server.MapPath("~/Files/" + fileName)); FileStream fs = new FileStream(); fs.ReadByte(file); object obj = LoadFile<object>(fs); public static T LoadFile<T>(FileStream fs) { using (GZipStream gzip = new GZipStream(fs, CompressionMode.Decompress)) { BinaryFormatter bf = new BinaryFormatter(); return (T)bf.Deserialize(gzip); } } 

In the method above, I have use FileStream to read byte array, but unlucky fs.ReadByte cannot read byte array. Any help please focus on how to Read byte array into a FileStream for using as a parameter in method "LoadFile". Please do not read directly the file into FileStream because the file here is loaded from somewhere else like from database or other source.

3
  • Why don't you use a MemoryStream instead of the FileStream? Commented Mar 8, 2013 at 11:02
  • MemoryStream can use as FileStream on the method LoadFile? Commented Mar 8, 2013 at 11:05
  • How is your code compiling FileStream does not take 0 arguments in the constuctor Commented Mar 8, 2013 at 11:12

4 Answers 4

23
string fileName = "test.txt"; byte[] file = File.ReadAllBytes(Server.MapPath("~/Files/" + fileName)); MemoryStream memStream = new MemoryStream(); BinaryFormatter binForm = new BinaryFormatter(); memStream.Write(file, 0, file.Length); memStream.Seek(0, SeekOrigin.Begin); Object obj = (Object)binForm.Deserialize(memStream); 
Sign up to request clarification or add additional context in comments.

4 Comments

Your method is using memStream but I got an error from your code. It said this: "The input stream is not a valid binary format. The starting contents (in bytes) are: 1F-8B-08-00-00-00-00-00-04-00-ED-BD-07-60-1C-49-96" when run at line Object obj = (Object)binForm.Deserialize(memStream);
Currently, I am using this code to get the file: byte[] file; using (var stream = new FileStream(Server.MapPath("~/Files/" + fileName), FileMode.Open, FileAccess.Read)) { using (var reader = new BinaryReader(stream)) { file = reader.ReadBytes((int)stream.Length); } }
If your file is zipped, which I assume it is because of the use of a GZipStream, then that is likely why this isn't working.
I have analyze my code and come to a conclusion as your thought. So I have post my new code above to require more help. If it is in your case, please help me with that.
9

I'm not sure where the misunderstanding is. FileStream represents a file on disk. You cannot "read bytes into it" without writing them to disk and you cannot read from it without reading from disk.

Maybe what you want is a MemoryStream which can contain arbitrary contents.

Both derive from Stream.

6 Comments

Yes. I have said that FileStream cannot read byte directly. If MemoryStream can contain Byte Array, then how can I use it for my method "LoadFile"?
Either change LoadFile to take a Stream or write your byte array to a file and create a FileStream from that.
Use the fact that both derive from Stream so you can pass any stream. You are about to experience the usefulness of inheritance :)
If I write byte array to a file, and then read the file back to FileStream, then how can I Read a file if it is exist only on database but not on any drive on server?
If you don't want to write the byte array to a file to read it, and really that was the worse of my two options. Then all you have to do is change LoadFile to take a Stream instead of a FileStream then load the byte array in a MemoryStream and pass that.
|
2

Yeah! Now I got a good solution after doing some more research. As the topic I have posted "How to read byte array into FileStream". We cannot read byte array into FileStream, it just use to read a file on driver to byte array. So I have change a little bit on my code, and now I have a file to read it using FileStream. How I made a file?

In this context I have an object. The object is anything as you want!

I use a collection as a samble object.

Collection<object> list = new Collection<object>(); //Now I will write this list to a file. fileName is what you want and be sure that folder Files is exist on server or at the root folder of your project WriteFile(list, Server.MapPath("~/Files/" + fileName)); //The method to write object to file is here public static void WriteFile<T>(T obj, string path) { FileStream serializeStream = new FileStream(path, FileMode.Create); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(serializeStream, obj); serializeStream.Flush(); serializeStream.Close(); } 

After I have wrote my object to a file, I need a method to read it back to object. So I do write this method:

public static Collection<object> ReatFile(string fileName){ //I have to read the file which I have wrote to an byte array byte[] file; using (var stream = new FileStream(Server.MapPath("~/Files/" + fileName), FileMode.Open, FileAccess.Read)) { using (var reader = new BinaryReader(stream)) { file = reader.ReadBytes((int)stream.Length); } } //And now is what I have to do with the byte array of file is to convert it back to object which I have wrote it into a file //I am using MemoryStream to convert byte array back to the original object. MemoryStream memStream = new MemoryStream(); BinaryFormatter binForm = new BinaryFormatter(); memStream.Write(file, 0, file.Length); memStream.Seek(0, SeekOrigin.Begin); Object obj = (Object)binForm.Deserialize(memStream); Collection<object> list = (Collection<object>)obj; return list; } 

After doing some steps above, I am now can write any type object to file and then read it back to original object. Thank too much for any help I have got there.

Comments

0

Why do you run File.ReadAllBytes prior to the usage of your FileStream?

string fileName = "test.txt"; using(FileStream fs = new FileStream(Server.MapPath("~/Files/" + fileName), FileMode.Open, FileAccess.Read)) { object obj = LoadFile<object>(fs); fs.Close(); } 

3 Comments

In my question, I have mentioned that the byte array is not reading from any file on server. It is just an byte array I got from database or other source. So please do not read file directly into FileStream.
Why So please do not read file directly into FileStream? You're using Server.MapPath("~/Files/" + fileName) to read the file on disk. Don't use FileStream on that if you don't intend to read any files on disk.
You right. I only have byte array and it isn't loaded from any file. So can you help me with this? Because the method "LoadFile" require a FileStream, and I just have a byte array from database. So how can I use it for my method "LoadFile"?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.