Create a Stream without having a physical file to create from
Asked Answered
G

3

23

I'm needing to create a zip file containing documents that exist on the server. I am using the .Net Package class to do so, and to create a new Package (which is the zip file) I have to have either a path to a physical file or a stream. I am trying to not create an actual file that would be the zip file, instead just create a stream that would exist in memory or something.

My question is how do you instantiate a new Stream (i.e. FileStream, MemoryStream, etc) without having a physical file to instantiate from.

Gerdes answered 12/4, 2010 at 15:5 Comment(0)
B
32

MemoryStream has several constructor overloads, none of which require a file.

Brezin answered 12/4, 2010 at 15:7 Comment(0)
H
11

There is an example of how to do this on the MSDN page for MemoryStream:

using System;
using System.IO;
using System.Text;

class MemStream
{
    static void Main()
    {
        int count;
        byte[] byteArray;
        char[] charArray;
        UnicodeEncoding uniEncoding = new UnicodeEncoding();

        // Create the data to write to the stream.
        byte[] firstString = uniEncoding.GetBytes(
            "Invalid file path characters are: ");
        byte[] secondString = uniEncoding.GetBytes(
            Path.GetInvalidPathChars());

        using(MemoryStream memStream = new MemoryStream(100))
        {
            // Write the first string to the stream.
            memStream.Write(firstString, 0 , firstString.Length);

            // Write the second string to the stream, byte by byte.
            count = 0;
            while(count < secondString.Length)
            {
                memStream.WriteByte(secondString[count++]);
            }

            // Write the stream properties to the console.
            Console.WriteLine(
                "Capacity = {0}, Length = {1}, Position = {2}\n",
                memStream.Capacity.ToString(),
                memStream.Length.ToString(),
                memStream.Position.ToString());

            // Set the position to the beginning of the stream.
            memStream.Seek(0, SeekOrigin.Begin);

            // Read the first 20 bytes from the stream.
            byteArray = new byte[memStream.Length];
            count = memStream.Read(byteArray, 0, 20);

            // Read the remaining bytes, byte by byte.
            while(count < memStream.Length)
            {
                byteArray[count++] =
                    Convert.ToByte(memStream.ReadByte());
            }

            // Decode the byte array into a char array
            // and write it to the console.
            charArray = new char[uniEncoding.GetCharCount(
                byteArray, 0, count)];
            uniEncoding.GetDecoder().GetChars(
                byteArray, 0, count, charArray, 0);
            Console.WriteLine(charArray);
        }
    }
}

Is this what you are looking for?

Hobo answered 12/4, 2010 at 15:13 Comment(0)
B
3

You can create a new stream and write to it. You don't need a file to construct the object.

http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx

Write Method:

http://msdn.microsoft.com/en-us/library/system.io.memorystream.write.aspx

Constructors for Memory Stream:

http://msdn.microsoft.com/en-us/library/system.io.memorystream.memorystream.aspx

Bern answered 12/4, 2010 at 15:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.