Possible Duplicate:
.Net Zip Up files
Hi,
I am having a problem in creating a text file to zip file ,
I am creating a project which reads data file and convert it into a text file .
My code creates a text file .I want to zip the text file which is created ,
Here Is My Code,
private void button1_Click(object sender, EventArgs e)
{
string path = DayFuturesDestination + "\\" + txtSelectedDate.Text + ".txt";
StreamWriter Strwriter = new StreamWriter(path);
}
Can Anyone Pls Say me how to create the zip file after creating it into a text file.
Thanks In Advance.
First, you'll need to do more with your IO stream to finish writing the file you plan on zipping. The example below shows one way you could do this.
If you want to stick with only the .net framework and just want to compress and decompress, you could use System.IO.Compression.GZipStream, which adheres to the GNU Zip format. Note that this is not the same as the ZIP archive format. For that, you'll need a third party library, such as DotNetZip. The latter, is easier to implement, and likely a better option than GZipStream.
For example, adapting DotNetZip's example to your code:
private void button1_Click(object sender, EventArgs e)
{
string path = DayFuturesDestination + "\\" + txtSelectedDate.Text + ".txt";
StreamWriter Strwriter = new StreamWriter(path);
Strwriter.Write(textToAdd);
Strwriter.Flush();
Strwriter.Close();
using (ZipFile zip = new ZipFile())
{
zip.AddFile(path);
zip.Save(path + ".zip");
}
}
You'll need a zip library to compress the files. Try this: http://dotnetzip.codeplex.com/
Or this one: http://www.icsharpcode.net/OpenSource/SharpZipLib/ (which is used in The Orchard Project)
They both look really easy to use.
精彩评论