Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi i have windows form application where i generate an XML file and what i looking for is to save automatically the XML inside zip file

You find below export button code:

What I have tried:

SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
            saveFileDialog1.Filter = "Extension|*.xml";
            saveFileDialog1.FilterIndex = 1;
            saveFileDialog1.FileName = String.Format("{0}", txtBox.Text + "_" + comBo1.Text + "_" + comBo2.Text);
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {

                doc.Save(saveFileDialog1.FileName);
            }
Posted
Updated 21-May-20 17:28pm

1 solution

Add a reference & 'using' statement for System.IO.Compression.FileSystem

ref : ZipArchive Constructor (System.IO.Compression) | Microsoft Docs[^] - using the 'stream constructor' gets-around having to add an entire directory

using (FileStream fs = new FileStream(@"drive:\path\file.zip",FileMode.Create))
using (ZipArchive zArchive = new ZipArchive(fs, ZipArchiveMode.Create))
{
    zArchive.CreateEntryFromFile(@"drive:\path\xml-file.xml", "xml-file.xml");
}
 
Share this answer
 
Comments
Maciej Los 22-May-20 2:15am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900