[英]Create new FileStream out of a byte array
I am attempting to create a new FileStream object from a byte array. 我试图从字节数组创建一个新的FileStream对象。 I'm sure that made no sense at all so I will try to explain in further detail below.
我确信根本没有任何意义,所以我将在下面进一步详细解释。
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)读取先前压缩的源文件2)使用GZipStream解压缩数据3)将解压缩的数据复制到字节数组中。
What I would like to change: 我想改变什么:
1) I would like to be able to use File.ReadAllBytes to read the decompressed data. 1)我希望能够使用File.ReadAllBytes来读取解压缩的数据。 2) I would then like to create a new filestream object usingg this byte array.
2)然后我想使用这个字节数组创建一个新的文件流对象。
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.
GZipStream的一个参数是某种类型的流,所以我认为我使用了一个文件流。 But, if some method exists where I can create a new instance of a FileStream from a byte array - then I should be fine.
但是,如果存在某些方法,我可以从字节数组创建一个新的FileStream实例 - 那么我应该没问题。
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
此致,埃文
听起来你需要使用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. 由于您不知道从
GZipStream
读取多少字节,因此无法为其分配数组。 You need to read it all into a byte array and then use a MemoryStream to decompress. 您需要将其全部读入字节数组,然后使用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);
}
}
}
}
Here is what I ended up doing. 这就是我最终做的事情。 I realize that I did not give sufficient information in my question - and I apologize for that - but I do know the size of the file I need to decompress as I am using it earlier in my program.
我意识到我没有在我的问题中提供足够的信息 - 我为此道歉 - 但我确实知道我需要解压缩的文件的大小,因为我在我的程序中使用它。 This buffer is referred to as "blen".
该缓冲区称为“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.