Pergunta

Estou precisando criar um arquivo zip contendo documentos que existem no servidor. Estou usando a classe .NET Package para fazê -lo e para criar um novo pacote (que é o arquivo zip), preciso ter um caminho para um arquivo físico ou um fluxo. Estou tentando não criar um arquivo real que seria o arquivo zip, apenas criar um fluxo que existiria na memória ou algo assim.

Minha pergunta é como você instancia um novo fluxo (ou seja, fileStream, MemoryStream, etc.) sem ter um arquivo físico para instanciar.

Foi útil?

Solução

MemoryStream tem vários Sobrecargas do construtor, nenhum dos quais exige um arquivo.

Outras dicas

Há um exemplo de como fazer isso no Página msdn para memória:

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);
        }
    }
}

É isso que você está procurando?

Você pode criar um novo fluxo e escrever para ele. Você não precisa de um arquivo para construir o objeto.

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

Método de gravação:

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

Construtores para fluxo de memória:

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top