【问题标题】:Create a Stream without having a physical file to create from在没有要创建的物理文件的情况下创建流
【发布时间】:2011-02-07 01:25:37
【问题描述】:

我需要创建一个 zip 文件,其中包含服务器上存在的文档。我正在使用 .Net Package 类来执行此操作,并且要创建一个新包(即 zip 文件),我必须具有物理文件或流的路径。我试图不创建一个将是 zip 文件的实际文件,而只是创建一个存在于内存或其他东西中的流。

我的问题是如何在没有要实例化的物理文件的情况下实例化一个新的 Stream(即 FileStream、MemoryStream 等)。

【问题讨论】:

    标签: c# stream package system.io.packaging


    【解决方案1】:

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

    这是你要找的吗?

    【讨论】:

      【解决方案2】:
      【解决方案3】:

      MemoryStream 有多个constructor overloads,它们都不需要文件。

      【讨论】:

        猜你喜欢
        • 2021-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-28
        • 2012-09-11
        • 2014-12-30
        • 1970-01-01
        相关资源
        最近更新 更多