serialize list in a zip?

F

frazan

Hi,
I would like to solve a problem.
Normally in my code I use this:
----------------------------------------
public void SaveFileCat(string path)
{
Stream myfile;
IFormatter formatter = new BinaryFormatter();
if(File.Exists(path))
{
myfile = new FileStream(path, FileMode.Truncate, FileAccess.Write);
}
else
myfile = new FileStream(path, FileMode.CreateNew, FileAccess.Write);

formatter.Serialize(myfile, listCat);
myfile.Close();
}
-------------------------------------------
The problem:
serialized files could to be up to 1 MB long, it depend from archive lenght.

In #develop there's #ziplib, but I don't know how to make it,
or better, I tried to make it, but only with normal file is simple.
There are other ways to make it?

Thanks you
frazan
 
S

Samuel R. Neff

Since this is a single file that you have control over reading/writing
you can use GZipOutputStream to compress the data. Just change

formatter.Serialize(myfile, listCat);
myfile.Close();

to

Stream zs = new GZipOutputStream(myfile);
formatter.Serialize(zs, listCat);
zs.Close()

The change to the load routine is pretty much the same, just use
GZipInputStream wrapped around the file stream before passing to
Deserialize.

The docs that come with #ziplib may be a little light but they do have
examples of this.

HTH,

Sam


Hi,
I would like to solve a problem.
Normally in my code I use this:
----------------------------------------
public void SaveFileCat(string path)
{
Stream myfile;
IFormatter formatter = new BinaryFormatter();
if(File.Exists(path))
{
myfile = new FileStream(path, FileMode.Truncate, FileAccess.Write);
}
else
myfile = new FileStream(path, FileMode.CreateNew, FileAccess.Write);

formatter.Serialize(myfile, listCat);
myfile.Close();
}
-------------------------------------------
The problem:
serialized files could to be up to 1 MB long, it depend from archive lenght.

In #develop there's #ziplib, but I don't know how to make it,
or better, I tried to make it, but only with normal file is simple.
There are other ways to make it?

Thanks you
frazan

B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
 
F

frazan

It's ok!!! :) Is two days that I put my head on the wall, and it's so
simple!!
I was testing MemoryStream but this bring me on wrong way, I obtain this
error:
"End of Stream encountered before parsing was completed".

Very very Thanks!!! If you come in Italy I will offer you a beer! :))

frazan
 

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