【发布时间】:2011-05-20 18:31:28
【问题描述】:
我正在尝试从字节数组创建一个新的 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 可能对你有用。
-
此代码不强制(foreach 中的大括号不匹配)。在您进行编辑时,浏览对话框的内容完全无关紧要。
-
然而,对于我正在做的事情,浏览对话框并非无关紧要。
标签: c# bytearray compression