C# 从字节数组中创建新的文件流

C# 从字节数组中创建新的文件流,c#,bytearray,compression,C#,Bytearray,Compression,我正在尝试从字节数组创建新的FileStream对象。我确信这毫无意义,所以我将在下面尝试进一步详细解释 我正在完成的任务: 1) 正在读取以前压缩的源文件 2) 使用GZipStream解压数据 3) 将解压缩的数据复制到字节数组中 我想更改的内容: 1) 我希望能够使用File.ReadAllBytes读取解压缩的数据。 2) 然后我想使用这个字节数组创建一个新的filestream对象 简而言之,我想使用字节数组完成整个操作。GZipStream的一个参数是某种类型的流,所以我想我一直在使

我正在尝试从字节数组创建新的FileStream对象。我确信这毫无意义,所以我将在下面尝试进一步详细解释

我正在完成的任务: 1) 正在读取以前压缩的源文件 2) 使用GZipStream解压数据 3) 将解压缩的数据复制到字节数组中

我想更改的内容:

1) 我希望能够使用File.ReadAllBytes读取解压缩的数据。 2) 然后我想使用这个字节数组创建一个新的filestream对象

简而言之,我想使用字节数组完成整个操作。GZipStream的一个参数是某种类型的流,所以我想我一直在使用文件流。但是,如果存在某种方法,我可以从字节数组中创建新的FileStream实例,那么我就可以了

以下是我到目前为止的情况:

FolderBrowserDialog fbd = new FolderBrowserDialog(); // Shows a browser dialog
 fbd.ShowDialog();

            // Path to directory of files to compress and decompress.
            string dirpath = fbd.SelectedPath;

            DirectoryInfo di = new DirectoryInfo(dirpath);


 foreach (FileInfo fi in di.GetFiles())
            {
                zip.Program.Decompress(fi);

            }

            // Get the stream of the source file.
            using (FileStream inFile = fi.OpenRead())
            {

                //Create the decompressed file.
                string outfile = @"C:\Decompressed.exe";
                {
                    using (GZipStream Decompress = new GZipStream(inFile,
                            CompressionMode.Decompress))
                    {
                        byte[] b = new byte[blen.Length];
                        Decompress.Read(b,0,b.Length);
                        File.WriteAllBytes(outfile, b);
                    }
                }
            }
谢谢你的帮助! 当做
Evan

听起来您需要使用一个。

因为您不知道要从
gzip流
读取多少字节,所以无法为它分配数组。您需要将其全部读入字节数组,然后使用MemoryStream进行解压缩

const int BufferSize = 65536;
byte[] compressedBytes = File.ReadAllBytes("compressedFilename");
// create memory stream
using (var mstrm = new MemoryStream(compressedBytes))
{
    using(var inStream = new GzipStream(mstrm, CompressionMode.Decompress))
    {
        using (var outStream = File.Create("outputfilename"))
        {
            var buffer = new byte[BufferSize];
            int bytesRead;
            while ((bytesRead = inStream.Read(buffer, 0, BufferSize)) != 0)
            {
                outStream.Write(buffer, 0, bytesRead);
            }  
        }
    }
}

这就是我最后要做的。我意识到我没有在我的问题中提供足够的信息,对此我深表歉意,但我知道我需要解压缩的文件的大小,因为我在我的程序中使用了它。此缓冲区称为“blen”


听起来可能对您有用。此代码不可计算(foreach中的括号不匹配)。当你编辑的时候,browsedialog的内容是完全不相关的。然而,对于我正在做的事情来说,browsedialog并不是不相关的。是的,奇怪的是,在发布这篇文章几秒钟后,我尝试了一下。我会确保我有它的工作,然后将上传一个完整的答案
string fi = @"C:\Path To Compressed File";
    // Get the stream of the source file.
           //     using (FileStream inFile = fi.OpenRead())
                using (MemoryStream infile1 = new MemoryStream(File.ReadAllBytes(fi)))
                {

                    //Create the decompressed file.
                    string outfile = @"C:\Decompressed.exe";
                    {
                        using (GZipStream Decompress = new GZipStream(infile1,
                                CompressionMode.Decompress))
                        {
                            byte[] b = new byte[blen.Length];
                            Decompress.Read(b,0,b.Length);
                            File.WriteAllBytes(outfile, b);
                        }
                    }
                }