473,626 Members | 3,031 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14657
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.tc c.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.co m>
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.Sha rpZipLib.Zip.Zi pInputStream(in File))
using (System.IO.File Stream outFile =
System.IO.File. Create(outPath) ) {
zip.GetNextEntr y();
int bytesRead;
while((bytesRea d = zip.Read(buffer , 0, BUFFER_LENGTH)) 0)
{
outFile.Write(b uffer, 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
9581
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
2216
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 same file stream. I was going to pass a ref to the file stream to the second thread, but the problem comes when I am trying to read from one section of the stream and write to another. I dont want one thread changing the position of the stream...
6
11154
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 calls? I need this because I don't want to create a regular file on disk (reasons for which are many , say directory permissions ), but being a file pointer, someone can use that pointer as a regular file descriptor for reading and writing. ...
9
2411
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 doe", with a space in it). For console input, cin.getline(var, howMuchIWant) or cin.get() has done the trick for me in the past. It doesn't seem to work for me nearly so well with a file stream. I wouldn't have thought cpp regarded
5
1989
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 method to read all the characters (including leading whitespaces) until the new line character (the default line delimiter) or end-of-file is reached. Then I close the stream, reopen it and repeat the procedure. I get the correct answer only the...
0
6982
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 game) and display it in a multiple editbox or richeditbox. Ive tryed somthing like this private FileStream file; private StreamReader stream;
2
3793
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(); } but it shows total file size...
2
6605
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 to create a buffer of a pre-defined length: (ConstBufferByteSize=10000000). How can I read a stream into a buffer or string without knowing the number of chars the stream will contain?
5
6861
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 again in to word object. If some one has some brilliant idea please share. In a nutshell this is what I'm currently doing ByteArray --File Stream --Word Object and this I want to do
0
1815
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 enumarate and getting the file handle However the fileStream Read/Write doesn't work I used the managed file stream method i.e first create a handle via native call in overlapped mode. Then use the handle to get a FileStream object and perform...
0
8262
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8196
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8364
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8502
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7192
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6122
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5571
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1507
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.