Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read byte array into FileStream

Tags:

c#

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.

like image 829
Tri Nguyen Dung Avatar asked Mar 08 '13 10:03

Tri Nguyen Dung


3 Answers

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);
like image 60
G Ravinder Avatar answered Oct 06 '22 01:10

G Ravinder


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.

like image 29
usr Avatar answered Oct 06 '22 01:10

usr


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.

like image 38
Tri Nguyen Dung Avatar answered Oct 05 '22 23:10

Tri Nguyen Dung