【问题标题】:Create new FileStream out of a byte array从字节数组中创建新的 FileStream
【发布时间】: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


【解决方案1】:

听起来您需要使用MemoryStream

【讨论】:

  • 是的,奇怪的是,在发布此消息几秒钟后,我试了一下。我会确保它正常工作,然后上传完整的答案
【解决方案2】:

由于您不知道将从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);
            }  
        }
    }
}

【讨论】:

    【解决方案3】:

    这就是我最终要做的事情。我意识到我没有在我的问题中提供足够的信息 - 对此我深表歉意 - 但我确实知道我需要解压缩的文件的大小,因为我在我的程序中早些时候使用它。这个缓冲区被称为“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);
                            }
                        }
                    }
    

    【讨论】:

      猜你喜欢
      • 2021-02-28
      • 2011-10-05
      • 2015-11-19
      • 2013-09-23
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多