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