创建新的FileStream出一个字节数组 [英] Create new FileStream out of a byte array

查看:142
本文介绍了创建新的FileStream出一个字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个字节数组新的FileStream对象。我敢肯定,没有任何意义可言,所以我会尽量在下文进一步详细解释

I am attempting to create a new FileStream object from a byte array. I'm sure that made no sense at all so I will try to explain in further detail below.

任务我完成:
1)阅读源文件这是以前的压缩
2)使用GZipStream
3)解压缩后的数据复制到一个字节数组解压缩数据。

Tasks I am completing: 1) Reading the source file which was previously compressed 2) Decompressing the data using GZipStream 3) copying the decompressed data into a byte array.

我想改变:

1)我想能够使用File.ReadAllBytes读解压缩数据。
2),那么我想创建一个新的文件流对象usingg此字节数组。

1) I would like to be able to use File.ReadAllBytes to read the decompressed data. 2) I would then like to create a new filestream object usingg this byte array.

在短,我想用字节数组做到这一点整个操作。一个用于GZipStream的参数之一是某种类型的数据流,所以我想我使用的是FILESTREAM卡住。但是,如果一些方法存在在那里我可以从一个字节数组一个FileStream的新实例 - 那么我应该没事。

In short, I want to do this entire operating using byte arrays. One of the parameters for GZipStream is a stream of some sort, so I figured I was stuck using a filestream. But, if some method exists where I can create a new instance of a FileStream from a byte array - then I should be fine.

下面是我到目前为止有:

Here is what I have so far:

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



任何帮助的感谢!
的问候,
埃文

Thanks for any help! Regards, Evan

推荐答案

既然你不知道你有多少字节是阅读从 GZipStream ,你真的不能分配数组它。你需要给它的所有读入一个字节数组,然后使用一个MemoryStream解压缩

Since you don't know how many bytes you'll be reading from the GZipStream, you can't really allocate an array for it. You need to read it all into a byte array and then use a MemoryStream to decompress.

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

这篇关于创建新的FileStream出一个字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆