Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建一个流,而无需从中创建物理文件_C#_Stream_Package_System.io.packaging - Fatal编程技术网

C# 创建一个流,而无需从中创建物理文件

C# 创建一个流,而无需从中创建物理文件,c#,stream,package,system.io.packaging,C#,Stream,Package,System.io.packaging,我需要创建一个包含服务器上存在的文档的zip文件。我使用.Net Package类来实现这一点,要创建一个新的包(即zip文件),我必须有一个指向物理文件或流的路径。我试图不创建一个实际的zip文件,而是创建一个存在于内存中的流 我的问题是,如何实例化一个新的流(即FileStream、MemoryStream等),而不需要从物理文件进行实例化 有几个流,其中没有一个需要文件。您可以创建一个新流并写入它。构建对象不需要文件 写入方法: 内存流的构造函数: 下面是一个如何在上执行此操作的示例:

我需要创建一个包含服务器上存在的文档的zip文件。我使用.Net Package类来实现这一点,要创建一个新的包(即zip文件),我必须有一个指向物理文件或流的路径。我试图不创建一个实际的zip文件,而是创建一个存在于内存中的流


我的问题是,如何实例化一个新的流(即FileStream、MemoryStream等),而不需要从物理文件进行实例化

有几个流,其中没有一个需要文件。

您可以创建一个新流并写入它。构建对象不需要文件

写入方法:

内存流的构造函数:


下面是一个如何在上执行此操作的示例:

使用系统;
使用System.IO;
使用系统文本;
类MemStream
{
静态void Main()
{
整数计数;
字节[]字节数组;
char[]charArray;
Unicode编码Unicode=新的Unicode编码();
//创建要写入流的数据。
byte[]firstString=uniEncoding.GetBytes(
无效的文件路径字符为:);
byte[]secondString=uniEncoding.GetBytes(
GetInvalidPathChars());
使用(MemoryStream memStream=新的MemoryStream(100))
{
//将第一个字符串写入流。
memStream.Write(firstString,0,firstString.Length);
//将第二个字符串逐字节写入流。
计数=0;
while(计数
这就是你要找的吗

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