473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

How to read a file into a stream

ad
Hi,
I have a file (c:\myFile.zip) in the Server.
How can I read the file into a Stream?
Aug 27 '06 #1
4 14644
Well, File.OpenRead(@"c:\myfile.zip") would give you a FileStream
looking at the file.

With the exception of MemoryStream, you don't really really read *into*
a stream; rather, the stream is a pipe through which you read the file
as you need it (i.e. a few thousand bytes at a time). Memory stream, on
the other hand, is a bucket.

Marc

Aug 27 '06 #2
ad <fl****@wfes.tcc.edu.twwrote:
I have a file (c:\myFile.zip) in the Server.
How can I read the file into a Stream?
Open it as a FileStream and that *is* a stream. Please give me details
on what you're really trying to do.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 27 '06 #3
Trying to predict your next question, can I offer the following example
(which also servers a general demonstration of stream usage)? This code
opens the file (at inPath), obtains the first file in the zip archive,
and writes it (to outPath).

Note that this uses #ZipLib to handle zip files (not handled natively
by the CLR); note that at any point in time we are only handling 2048
bytes of data, even for a 500Mb file - so we haven't read the entire
file into memory first.

We have three streams here; we have connected two "pipes" (streams)
together, with the nifty #ZipLib code in the middle, so when you
request e.g. 2048 bytes, it reads {some} bytes from the FileStream,
decompresses it, and gives you back {some} bytes (bytesRead). We then
write this amount from the buffer to out output stream.

Code maynot be 100% - I have ported it (in notepad) from a more complex
example.

int BUFFER_LENGTH = 2048;
byte[] buffer = new byte[BUFFER_LENGTH];
using(inFile = System.IO.File.OpenRead(inPath))
using(zip = new
ICSharpCode.SharpZipLib.Zip.ZipInputStream(inFile) )
using (System.IO.FileStream outFile =
System.IO.File.Create(outPath)) {
zip.GetNextEntry();
int bytesRead;
while((bytesRead = zip.Read(buffer, 0, BUFFER_LENGTH)) 0)
{
outFile.Write(buffer, 0, bytesRead);
}
outFile.Flush();
outFile.Close();
}

Marc

Aug 27 '06 #4
Marc Gravell wrote:
Note that this uses #ZipLib to handle zip files (not handled natively
by the CLR);
I use #ZipLib as well.

But as someone once pointed out to me then .NET supports
java.lang.zip in the vjslib assembly for J# !

Arne
Aug 28 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gigi | last post by:
Hello, I have a byte array and i must send(to blocks) to remote class In the remote class i write a file stream...... How? In my sample i send stream...
3
by: frustrated | last post by:
I am trying to share a file stream between two threads, but havent got a clue as to how to do it. The first thread will be reading the file, and the second thread will(/might) be writing to the...
6
by: Divick | last post by:
Is there a way in C++ to have a file stream which is directly mapped to memory(virtual memory) without actually having to create a file and then memory mapping that file using system dependent...
9
by: sherifffruitfly | last post by:
Hi, I've a got a little (exercise) program that reads data from a file and puts it into struct members. I run into trouble when one of the data pieces is comprised of several words (eg "john...
5
by: Generic Usenet Account | last post by:
I have been able to recreate a problem that I am having with the file stream using the simple sample code given below. I am trying to read the number of lines in a file, relying on the getline...
0
by: r0swell | last post by:
Hey all, I'm trying to make my app read the stream of a text file and begin at the end of the file. Then parse all the lines that get added in the text file by a other app (Its a log file from a...
2
by: somequestion | last post by:
During copying file , wanna read file Size like this string CheckFileSize(string fileName) { if( fileName == null ) return; FileInfo fi = new FileInfo(fileName); return fi.Length.ToString();...
2
by: Jack | last post by:
Hi, I want to read a string a chars from a stream, and put it into a string. At the moment, I'm creating a buffer of a fixed size, and reading the stream of text into it. It works, but I have...
5
by: Nitin Mahajan | last post by:
Guys Is there a way in C# to create a word object directly from a memory stream without passing that to hard disk (file stream). I think it doesn't makes sense to create a file just to read it...
0
by: Brian Pinto | last post by:
Im facing a problem with file stream read/write for a usb device. The code works fine on XP (32 bit). but fails to work on Windows 7 (64 bit). I'm successfully past problem of getting the device to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.