///////////Download This Dll From Link
///////////http://sourceforge.net/projects/sharpdevelop/files/SharpZipLib/0.86/SharpZipLib_0860_Bin.zip/dow/nload?use_mirror=kaz
///////////and Add This Dll in project
using ICSharpCode.SharpZipLib.Zip;
//Zip a Folder
public static void ZipToFilder(string ZipFolderPath, string SavePath)
{
StringBuilder newPath = new StringBuilder(SavePath);
ZipInputStream zipIn = new ZipInputStream(File.OpenRead(ZipFolderPath));
ZipEntry entry;
if (Directory.Exists(SavePath) != true)
{
Directory.CreateDirectory(SavePath);
}
while ((entry = zipIn.GetNextEntry()) != null)
{
if (entry.Name.EndsWith("/"))
{
Directory.CreateDirectory(String.Format("{0}{1}", newPath, entry.Name.Replace(@"/", @"\")));
}
else
{
FileStream streamWriter = File.Create(String.Format("{0}{1}", newPath, entry.Name.Replace(@"/", @"\")));
long size = entry.Size;
byte[] data = new byte[size];
while (true)
{
size = zipIn.Read(data, 0, data.Length);
if (size > 0) streamWriter.Write(data, 0, (int)size);
else break;
}
streamWriter.Close();
streamWriter.Dispose();
}
}
zipIn.Close();
}
//UnZip a Folder
public static void FolderToZip(string sTargetFolderPath, string ZipFileName)
{
string sZipFileName = ZipFileName;
string[] filenames = Directory.GetFiles(sTargetFolderPath);
//string ZipPath = Directory.GetParent(sTargetFolderPath).ToString();
string ZipPath = sTargetFolderPath;
using (ZipOutputStream s = new ZipOutputStream(File.Create(ZipPath + "\\" + sZipFileName + "")))
{
s.SetLevel(9); // 0-9, 9 being the highest level of compression
byte[] buffer = new byte[4096];
foreach (string file in filenames)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
s.Finish();
s.Close();
}
//Directory.Delete(sTargetFolderPath + "\\TempZipFile\\", true);
}
No comments:
Post a Comment