10

I wanted to use the FileStream class to avoid saving any files on the client's computer. But every constructor of FileStream requires a path to a file.

How do I create a FileStream from a stream and not have to save it on the harddrive?

EDIT: I am already using memorystream to store my information. But at some point I need to zip the files into ANOTHER STREAM. The problem is that the zip commands (I've looked in GZipStream - It zips FILES to a STREAM) require me a source filePath(FileStream).

If I can't surpass by creating a FileStream from a stream, is there another way to zip streams?

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
user779444
  • 1,365
  • 4
  • 21
  • 38
  • FileStream IS to write to file, use MemoryStream instead – Renatas M. Mar 31 '12 at 07:30
  • It is entirely incorrect to say that GZipStream processes *files*. It does not. It processes a byte-stream and doesn't care about files whatsoever. You need to explain your scenario more, perhaps showing exactly the code that wants a file. You can use GZipStream to work with arbitrary sources/destinations. You really need to explain what you are getting stuck on. If you "looked in GZipStream" and reached that conclusion, then you looked wrong. – Marc Gravell Mar 31 '12 at 07:55

3 Answers3

15

In this scenario you should just use a MemoryStream, and ensure your API just demands Stream rather than FileStream specifically. For example:

using(var ms = new MemoryStream()) {
     YourCode(ms);
}

Alternatively you could keep using FileStream by using a file in the temp area, via Path.GetTempFileName().

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

You can either use other Stream types (like MemoryStream) or derive your own class from FileStream that will do nothing (or write wherever you need).

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

FileStream works with files. You can't change this behaviour.

You may check other *Stream classes inherited from System.IO.Stream like MemoryStream.

asktomsk
  • 2,289
  • 16
  • 23