By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,651 Members | 1,676 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,651 IT Pros & Developers. It's quick & easy.

How to compress stream in memory

P: n/a
ad
I used use SharpZipLib to compress files in disk.

But now I want to compress stream into another stream in memory(the stream
not associated with disk file)

My pseudo is:

Stream InputStream;
Stream OutPutStream;
DataSet1.WriteXml(InputStream);
OutPutStream=ZipStream(InputStream);

How can I do that?
Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
// Write compressed to network stream example - not compiled or tested.
using(NetworkStream ns = new NetworkStream(socket))
using ( GZipStream gz = new GZipStream(, CompressionMode.Compress, true) )
{
gz.Write(...); // Write bytes to compressed stream, which will
in-turn send the compressed bytes to the network stream.
// other work..
gz.Flush();
}

One issue with sending compressed streams over the network like this is you
don't know the stream size before hand so you can't prepend len bytes so the
other side know how many bytes to expect. So you either have to use some
kind of unique delimiter sequence (which could have some issues) or compress
the stream before hand and send that (which brings you back to same issue).
Small-to-Medium files could be compressed to a MemoryStream as long as RAM
is not an issue (but that is an unknown in most cases and probably not
something to count on).

--
William Stacey [MVP]

"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:e4*************@TK2MSFTNGP14.phx.gbl...
I used use SharpZipLib to compress files in disk.

But now I want to compress stream into another stream in memory(the stream
not associated with disk file)

My pseudo is:

Stream InputStream;
Stream OutPutStream;
DataSet1.WriteXml(InputStream);
OutPutStream=ZipStream(InputStream);

How can I do that?

Nov 17 '05 #2

P: n/a
If you are using .NET 2.0 or above, use the classes in the
System.IO.Compression namespace. You can use the GZipStream or the
DeflateStream class to wrap another stream, and write/read zipped contents
from it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:e4*************@TK2MSFTNGP14.phx.gbl...
I used use SharpZipLib to compress files in disk.

But now I want to compress stream into another stream in memory(the stream
not associated with disk file)

My pseudo is:

Stream InputStream;
Stream OutPutStream;
DataSet1.WriteXml(InputStream);
OutPutStream=ZipStream(InputStream);

How can I do that?

Nov 17 '05 #3

P: n/a
ad
Thanks,
When compile this line
NetworkStream ns = new NetworkStream(socket);
It fail because socket not declare.
How can I new socket?

"William Stacey [MVP]" <wi************@gmail.com> 撰寫於郵件新聞:uy*************@TK2MSFTNGP10.phx.gb l...
// Write compressed to network stream example - not compiled or tested.
using(NetworkStream ns = new NetworkStream(socket))
using ( GZipStream gz = new GZipStream(, CompressionMode.Compress, true) )
{
gz.Write(...); // Write bytes to compressed stream, which will
in-turn send the compressed bytes to the network stream.
// other work..
gz.Flush();
}

One issue with sending compressed streams over the network like this is
you don't know the stream size before hand so you can't prepend len bytes
so the other side know how many bytes to expect. So you either have to
use some kind of unique delimiter sequence (which could have some issues)
or compress the stream before hand and send that (which brings you back to
same issue). Small-to-Medium files could be compressed to a MemoryStream
as long as RAM is not an issue (but that is an unknown in most cases and
probably not something to count on).

--
William Stacey [MVP]

"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:e4*************@TK2MSFTNGP14.phx.gbl...
I used use SharpZipLib to compress files in disk.

But now I want to compress stream into another stream in memory(the
stream not associated with disk file)

My pseudo is:

Stream InputStream;
Stream OutPutStream;
DataSet1.WriteXml(InputStream);
OutPutStream=ZipStream(InputStream);

How can I do that?


Nov 17 '05 #4

P: n/a
ad
Thanks,
I have study GZipStream for a while , but I can not figure out how to do.
Please give me an example!
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> 撰寫於郵件新聞:Ok**************@tk2msftngp13.phx.g bl...
If you are using .NET 2.0 or above, use the classes in the
System.IO.Compression namespace. You can use the GZipStream or the
DeflateStream class to wrap another stream, and write/read zipped contents
from it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:e4*************@TK2MSFTNGP14.phx.gbl...
I used use SharpZipLib to compress files in disk.

But now I want to compress stream into another stream in memory(the
stream not associated with disk file)

My pseudo is:

Stream InputStream;
Stream OutPutStream;
DataSet1.WriteXml(InputStream);
OutPutStream=ZipStream(InputStream);

How can I do that?


Nov 17 '05 #5

P: n/a
If you use a socket in your app, you can use that. It could be any Stream
however.

--
William Stacey [MVP]

"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:eX**************@tk2msftngp13.phx.gbl...
Thanks,
When compile this line
NetworkStream ns = new NetworkStream(socket);
It fail because socket not declare.
How can I new socket?

"William Stacey [MVP]" <wi************@gmail.com>
撰寫於郵件新聞:uy*************@TK2MSFTNGP10.phx.gb l...
// Write compressed to network stream example - not compiled or tested.
using(NetworkStream ns = new NetworkStream(socket))
using ( GZipStream gz = new GZipStream(, CompressionMode.Compress,
true) )
{
gz.Write(...); // Write bytes to compressed stream, which will
in-turn send the compressed bytes to the network stream.
// other work..
gz.Flush();
}

One issue with sending compressed streams over the network like this is
you don't know the stream size before hand so you can't prepend len bytes
so the other side know how many bytes to expect. So you either have to
use some kind of unique delimiter sequence (which could have some issues)
or compress the stream before hand and send that (which brings you back
to same issue). Small-to-Medium files could be compressed to a
MemoryStream as long as RAM is not an issue (but that is an unknown in
most cases and probably not something to count on).

--
William Stacey [MVP]

"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:e4*************@TK2MSFTNGP14.phx.gbl...
I used use SharpZipLib to compress files in disk.

But now I want to compress stream into another stream in memory(the
stream not associated with disk file)

My pseudo is:

Stream InputStream;
Stream OutPutStream;
DataSet1.WriteXml(InputStream);
OutPutStream=ZipStream(InputStream);

How can I do that?



Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.