繁体   English   中英

从字节数组中创建新的FileStream

[英]Create new FileStream out of a byte array

我试图从字节数组创建一个新的FileStream对象。 我确信根本没有任何意义,所以我将在下面进一步详细解释。

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

我想改变什么:

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

简而言之,我想使用字节数组完成整个操作。 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);
                    }
                }
            }

谢谢你的帮助! 此致,埃文

听起来你需要使用MemoryStream

由于您不知道从GZipStream读取多少字节,因此无法为其分配数组。 您需要将其全部读入字节数组,然后使用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”。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM