Wednesday, February 17, 2010

ziping folder using ICSharpCode dll

using ICSharpCode.SharpZipLib.Zip;

private static void ListZipContent(string sFile)
{
ZipFile zip = new ZipFile(File.OpenRead(sFile));
foreach (ZipEntry entry in zip)
{
//Console.WriteLine(entry.Name);
}
}

private static void CompressZip(string sPath, string zipSavingPath)
{
try
{
string zipName = DateTime.Now.ToString("m") + ".zip";
ZipOutputStream zipOut = new ZipOutputStream(File.Create(zipSavingPath + zipName));
//ZipOutputStream zipOut = new ZipOutputStream(File.Create(sPath));
foreach (string fName in Directory.GetFiles(sPath))
{
FileInfo fi = new FileInfo(fName);
ZipEntry entry = new ZipEntry(fi.Name);
FileStream sReader = File.OpenRead(fName);
byte[] buff = new byte[Convert.ToInt32(sReader.Length)];
sReader.Read(buff, 0, (int)sReader.Length);
entry.DateTime = fi.LastWriteTime;
entry.Size = sReader.Length;
sReader.Close();
zipOut.PutNextEntry(entry);
zipOut.Write(buff, 0, buff.Length);
}
zipOut.Finish();
zipOut.Close();
}
catch (Exception ex)
{

}
}

Simple copy functionality in C#

public class SimpleFileCopy
{
public void CopyFile(string sourcePath, string targetPath, string folderPath)
{
try
{
if (!System.IO.Directory.Exists(folderPath))
{
System.IO.Directory.CreateDirectory(folderPath);
}
System.IO.File.Copy(sourcePath, targetPath, true);
}
catch (Exception ex)
{
}
}
}
#endregion