first you can add system.io.compression.filedirectory
then create a file and move it a folder then compress it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.Compression;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string fileNamewithExtension = DateTime.Now.Ticks.ToString() + ".txt";
string filePath = @"C:\AbhishekData\" ;
string fileName = filePath + fileNamewithExtension;
using (FileStream fs = new FileStream(fileName, FileMode.Append, FileAccess.Write))
{
string s = "My Name is abhishek";
byte[] byteArray = Encoding.ASCII.GetBytes(s);
fs.Write(byteArray, 0, byteArray.Length);
fs.Flush();
fs.Close();
string zipPath = @"C:\AbhishekData\"+ DateTime.Now.Ticks.ToString()+".zip";
if (!Directory.Exists(@"C:\AbhishekData\myabc"))
{
Directory.CreateDirectory(@"C:\AbhishekData\myabc");
File.Move(fileName, @"C:\AbhishekData\myabc\"+ fileNamewithExtension);
ZipFile.CreateFromDirectory(@"C:\AbhishekData\myabc", zipPath,CompressionLevel.Fastest,true);
}
else
{
File.Move(fileName, @"C:\AbhishekData\myabc\"+ fileNamewithExtension);
ZipFile.CreateFromDirectory(@"C:\AbhishekData\myabc", zipPath, CompressionLevel.Fastest, true);
}
}
}
}
}
No comments:
Post a Comment