Zipping page output

D

Dylan Parry

Hi folks,

I've got a page that outputs XML, but in some cases the size of the page
is several MB. So I figured the best way to deal with it is to ZIP the
contents of the page and send that to the browser along with the
appropriate headers.

So my question... Is there a way that I can create a ZIP file containing
an XML document based on the value of a string/XmlDocument object? If
not, is there a way of temporarily writing out an actual XML file,
zipping it up and sending that to the browser, then deleting the files?

Thanks,
 
J

Jon Skeet [C# MVP]

Dylan said:
I've got a page that outputs XML, but in some cases the size of the page
is several MB. So I figured the best way to deal with it is to ZIP the
contents of the page and send that to the browser along with the
appropriate headers.

So my question... Is there a way that I can create a ZIP file containing
an XML document based on the value of a string/XmlDocument object? If
not, is there a way of temporarily writing out an actual XML file,
zipping it up and sending that to the browser, then deleting the files?

..NET 2.0 has compression classes built in, and if they don't do what
you want you should look at SharpZipLib (a Google search will find it
for you). You'd compress by writing the XmlDocument out to a stream
which does the compression on the fly.

Now, how you go about telling ASP.NET to serve up the compressed
version for the browser to decompress, I'm not sure... I know there are
compression headers available, but I don't know how widely implemented
they are.

Jon
 
D

Dylan Parry

Jon said:
.NET 2.0 has compression classes built in, and if they don't do what
you want you should look at SharpZipLib (a Google search will find it
for you). You'd compress by writing the XmlDocument out to a stream
which does the compression on the fly.

Thanks. I'll take a look at that.
Now, how you go about telling ASP.NET to serve up the compressed
version for the browser to decompress, I'm not sure... I know there are
compression headers available, but I don't know how widely implemented
they are.

Oh that bit doesn' matter. It will be fine serving the file as a .zip
file for the user to download. The reason being that they are inevitably
going to zip it themselves anyway as they will be required to send the
data to a third party.
 
J

Jon Skeet [C# MVP]

Dylan said:
Oh that bit doesn' matter. It will be fine serving the file as a .zip
file for the user to download. The reason being that they are inevitably
going to zip it themselves anyway as they will be required to send the
data to a third party.

Right. To save you a bit of time: if you want to produce an actual zip
file, you should use SharpZipLib. The .NET 2.0 compression stuff is
fine for the compression of streams, but it doesn't build a file with a
table of contents etc.

Jon
 
D

Dylan Parry

Jon said:
Right. To save you a bit of time: if you want to produce an actual zip
file, you should use SharpZipLib. The .NET 2.0 compression stuff is
fine for the compression of streams, but it doesn't build a file with a
table of contents etc.

Thanks very much. I'm looking in to this now - got as far as downloading
it and installing it to the GAC, now reading the docs ;)
 
D

Dylan Parry

Dylan Parry wrote:

[SharpZipLib]
Thanks very much. I'm looking in to this now - got as far as downloading
it and installing it to the GAC, now reading the docs ;)

Ok, well I've downloaded and installed it, read through the docs and I
can't figure out how on Earth to use the damn thing. It looks pretty
complicated! Any ideas how I would perform this simple action using the
library:

1) Create a ZIP file in memory
2) Add a new file to the ZIP containing the contents of a string, and
with a given name
3) Output the ZIP file as binary data

Ie. I have already got a string that contains "Hello World", and I want
to create a file in memory called "hello.txt" that contains this string,
then I want to add it to a ZIP file and send it to the browser.

Sorry if I seem thick, but I really couldn't understand the docs that
came with SharpZipLib :(
 
V

Vincent

Doesn't IIS automatically compress the pages that it serves? If it
doesn't that would be silly, use apache instead.
 
J

Jon Skeet [C# MVP]

Dylan Parry said:
[SharpZipLib]
Thanks very much. I'm looking in to this now - got as far as downloading
it and installing it to the GAC, now reading the docs ;)

Ok, well I've downloaded and installed it, read through the docs and I
can't figure out how on Earth to use the damn thing. It looks pretty
complicated! Any ideas how I would perform this simple action using the
library:

It's not too hard really. If you download the source and samples,
there's a fairly easy-to-understand example called CreateZipFile.
1) Create a ZIP file in memory
2) Add a new file to the ZIP containing the contents of a string, and
with a given name
3) Output the ZIP file as binary data

You don't really need to create the zip file in memory - you can stream
it straight out, assuming you've got access to the output stream in
ASP.NET. Where the sample code uses File.Create to make a FileStream,
just use the stream you want to write to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top