473,395 Members | 1,554 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,395 software developers and data experts.

How to compress stream in memory

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
5 12966
// 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
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
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> ¼¶¼g©ó¶l¥ó·s»D: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
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> ¼¶¼g©ó¶l¥ó·s»D: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
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>
¼¶¼g©ó¶l¥ó·s»D: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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tony | last post by:
Hello, Any assistance would be appreciated: As the subject suggests I am trying to gunzip files using the Compress::Zlib module. The following is basically the code that I am working with: ...
2
by: Jean-Marc Blaise | last post by:
Hi, I get a SQL2009C when using the 'compress' option for backup, but the util_heap_sz does not blow if I do not use the 'compress' option. What is the overhead induced by the compress option in...
1
by: Tim Bücker | last post by:
Hello. I am working on a program that generates about 1000 pictures that should reside in an avi later. Of course this amount of pictures creates a very huge avi-file but my main problem is...
4
by: TT (Tom Tempelaere) | last post by:
Hey there, I need a string stream, but I can't find one in .NET. I thought StringWriter would derive from Stream, alas it doesn't do so. Which leads me to my next question: What is the purpose...
8
by: Jose L. Velazquez | last post by:
Hi all, I have made a webservice that returns an XML, but sometimes the connection is so slow and there is a lot of data to be returned. I would like to know if it is possible to send this data...
4
by: Scott F. Brown | last post by:
Greetings all... I was playing around with compressing streams and came across a behavior that I do not understand. I create a stream (input) from the contents of a textbox. That stream is...
6
by: Champika Nirosh | last post by:
Hi, I have two machine where I needed to have a extended TCP/IP protocol to make the link between the two machines Mean,I need to write a application that compress every data the machine send...
7
by: iporter | last post by:
I use the code below to authorise the download of certain files. Thus, instead of linking to the file in a wwwroot directory, I link to this code with the filename as a parameter, and the script...
7
by: mathieu | last post by:
Hi there, I am trying to rewrite this very slow function (due to AFAIK the extra copy): void DoAction(std::istream &is, std::ostream &os) { uint16_t c; while( is.read((char*)&c,2) )
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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...

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.