Textfile filestream to ZipStorer

Mar 11, 2012 at 4:44 AM

Hi I am trying to convert a method from writing a regular textfile to doing it in a zip file

Here's what I've tried:

void savepfc(string file)
{

 

MemoryStream fsOutput = new MemoryStream();

           // FileStream fsOutput = new FileStream(file, FileMode.Create, FileAccess.Write);           

StreamWriter srOutput = new StreamWriter(fsOutput);           

srOutput.WriteLine(Product_Name.Text);           

srOutput.WriteLine(Product_ID.Text);           

srOutput.WriteLine(price.Text);
*snips about 20 srOutput.WriteLines...
            srOutput.Flush();           

ZipStorer zipfile = ZipStorer.Create(file, "This is a product file");           

zipfile.AddStream(ZipStorer.Compression.Deflate, "product.txt", fsOutput, DateTime.Now, "This is your product."); 

          srOutput.Close();           

zipfile.Close();

}

But when I check the txt file inside the zip its got nothing in it.

I've not used memorystreams before. Are they different from filestreams?

Mar 11, 2012 at 5:47 AM

Got this figured out

I put the following in after srOutput.Flush(); and it works

 
            fsOutput.Flush();
            fsOutput.Seek(0, SeekOrigin.Begin);