首先在网上下载开源的sharpziplib.dll动态链接库,将其装入你的项目,这样你就可以开发自己的压缩与解压缩工具了,以下是示例源代码,敬请参考。
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Tar;
public partial class Form1 : Form
... {
public Form1()
...{
InitializeComponent();
}
方法#region 方法
private void CopyFiles(string varFromDirectory, string varToDirectory) //备份
...{
Directory.CreateDirectory(varToDirectory);
if (!Directory.Exists(varFromDirectory)) return;
string[] directories = Directory.GetDirectories(varFromDirectory);
if (directories.Length > 0)
...{
foreach (string d in directories)
...{
CopyFiles(d, varToDirectory + d.Substring(d.LastIndexOf("/")));
}
}
string[] files = Directory.GetFiles(varFromDirectory);
if (files.Length > 0)
...{
foreach (string s in files)
...{
File.Copy(s, varToDirectory + s.Substring(s.LastIndexOf("/")), true);
}
}
}
public static void UnZip(string FileToUpZip, string ZipedFolder)//
...{
if (!File.Exists(FileToUpZip))
...{
return;
}
if (!Directory.Exists(ZipedFolder))
...{
Directory.CreateDirectory(ZipedFolder);
}
ZipInputStream s = null;
ZipEntry theEntry = null;
string fileName;
FileStream streamWriter = null;
try
...{
s = new ZipInputStream(File.OpenRead(FileToUpZip));
while ((theEntry = s.GetNextEntry()) != null)
...{
if (theEntry.Name != String.Empty)
...{
fileName = Path.Combine(ZipedFolder, theEntry.Name);
/**////判断文件路径是否是文件夹
if (fileName.EndsWith("/") || fileName.EndsWith("/"))
...{
Directory.CreateDirectory(fileName);
continue;
}
streamWriter = File.Create(fileName);
int size = 2048;
byte[] data = new byte[2048];
while (true)
...{
size = s.Read(data, 0, data.Length);
if (size > 0)
...{
streamWriter.Write(data, 0, size);
}
else
...{
break;
}
}
}
}
}
finally
...{
if (streamWriter != null)
...{
streamWriter.Close();
streamWriter = null;
}
if (theEntry != null)
...{
theEntry = null;
}
if (s != null)
...{
s.Close();
s = null;
}
GC.Collect();
GC.Collect(1);
}
}
public void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)//压缩
...{
//如果文件没有找到,则报错
if (!System.IO.File.Exists(FileToZip))
...{
throw new System.IO.FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd");
}
System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
ZipEntry ZipEntry = new ZipEntry("ZippedFile");
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(CompressionLevel);
byte[] buffer = new byte[BlockSize];
System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
ZipStream.Write(buffer, 0, size);
try
...{
while (size < StreamToZip.Length)
...{
int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
ZipStream.Write(buffer, 0, sizeRead);
size += sizeRead;
}
}
catch (System.Exception ex)
...{
throw ex;
}
ZipStream.Finish();
ZipStream.Close();
StreamToZip.Close();
}
public void ZipFileMain(string[] args)
...{
string[] filenames = Directory.GetFiles(args[0]);
Crc32 crc = new Crc32();
ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));
s.SetLevel(6); // 0 - store only to 9 - means best compression
foreach (string file in filenames)
...{
//打开压缩文件
FileStream fs = File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(file);
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
s.Finish();
s.Close();
}
public void UnZip(string[] args)//解压
...{
ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
...{
string directoryName = Path.GetDirectoryName(args[1]);
string fileName = Path.GetFileName(theEntry.Name);
//生成解压目录
Directory.CreateDirectory(directoryName);
if (fileName != String.Empty)
...{
//解压文件到指定的目录
FileStream streamWriter = File.Create( args[1] + fileName );
int size = 2048;
byte[] data = new byte[2048];
while (true)
...{
size = s.Read(data, 0, data.Length);
if (size > 0)
...{
streamWriter.Write(data, 0, size);
}
else
...{
break;
}
}
streamWriter.Close();
}
}
s.Close();
}
#endregion
private void button1_Click(object sender, EventArgs e)
...{
CopyFiles(@"D:图标TecSmart", @"C:photo");
}
private void btnzip_Click(object sender, EventArgs e)
...{
string[] FileProperties = new string[2];
FileProperties[0] = @"D:图标TecSmart";//待压缩文件目录
FileProperties[1] = @"E:DBd.zip"; //压缩后的目标文件 ,E盘中须有DB文件夹
ZipFileMain(FileProperties);
}
private void btnunzip_Click(object sender, EventArgs e)
...{
string[] FileProperties = new string[2];
FileProperties[0] = @"E:DBd.zip";//待解压的文件
FileProperties[1] = "E:/DB/DataSet/";//解压后放置的目标目录
UnZip(FileProperties);
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Tar;
public partial class Form1 : Form
... {
public Form1()
...{
InitializeComponent();
}
方法#region 方法
private void CopyFiles(string varFromDirectory, string varToDirectory) //备份
...{
Directory.CreateDirectory(varToDirectory);
if (!Directory.Exists(varFromDirectory)) return;
string[] directories = Directory.GetDirectories(varFromDirectory);
if (directories.Length > 0)
...{
foreach (string d in directories)
...{
CopyFiles(d, varToDirectory + d.Substring(d.LastIndexOf("/")));
}
}
string[] files = Directory.GetFiles(varFromDirectory);
if (files.Length > 0)
...{
foreach (string s in files)
...{
File.Copy(s, varToDirectory + s.Substring(s.LastIndexOf("/")), true);
}
}
}
public static void UnZip(string FileToUpZip, string ZipedFolder)//
...{
if (!File.Exists(FileToUpZip))
...{
return;
}
if (!Directory.Exists(ZipedFolder))
...{
Directory.CreateDirectory(ZipedFolder);
}
ZipInputStream s = null;
ZipEntry theEntry = null;
string fileName;
FileStream streamWriter = null;
try
...{
s = new ZipInputStream(File.OpenRead(FileToUpZip));
while ((theEntry = s.GetNextEntry()) != null)
...{
if (theEntry.Name != String.Empty)
...{
fileName = Path.Combine(ZipedFolder, theEntry.Name);
/**////判断文件路径是否是文件夹
if (fileName.EndsWith("/") || fileName.EndsWith("/"))
...{
Directory.CreateDirectory(fileName);
continue;
}
streamWriter = File.Create(fileName);
int size = 2048;
byte[] data = new byte[2048];
while (true)
...{
size = s.Read(data, 0, data.Length);
if (size > 0)
...{
streamWriter.Write(data, 0, size);
}
else
...{
break;
}
}
}
}
}
finally
...{
if (streamWriter != null)
...{
streamWriter.Close();
streamWriter = null;
}
if (theEntry != null)
...{
theEntry = null;
}
if (s != null)
...{
s.Close();
s = null;
}
GC.Collect();
GC.Collect(1);
}
}
public void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)//压缩
...{
//如果文件没有找到,则报错
if (!System.IO.File.Exists(FileToZip))
...{
throw new System.IO.FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd");
}
System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
ZipEntry ZipEntry = new ZipEntry("ZippedFile");
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(CompressionLevel);
byte[] buffer = new byte[BlockSize];
System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
ZipStream.Write(buffer, 0, size);
try
...{
while (size < StreamToZip.Length)
...{
int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
ZipStream.Write(buffer, 0, sizeRead);
size += sizeRead;
}
}
catch (System.Exception ex)
...{
throw ex;
}
ZipStream.Finish();
ZipStream.Close();
StreamToZip.Close();
}
public void ZipFileMain(string[] args)
...{
string[] filenames = Directory.GetFiles(args[0]);
Crc32 crc = new Crc32();
ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));
s.SetLevel(6); // 0 - store only to 9 - means best compression
foreach (string file in filenames)
...{
//打开压缩文件
FileStream fs = File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(file);
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
s.Finish();
s.Close();
}
public void UnZip(string[] args)//解压
...{
ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
...{
string directoryName = Path.GetDirectoryName(args[1]);
string fileName = Path.GetFileName(theEntry.Name);
//生成解压目录
Directory.CreateDirectory(directoryName);
if (fileName != String.Empty)
...{
//解压文件到指定的目录
FileStream streamWriter = File.Create( args[1] + fileName );
int size = 2048;
byte[] data = new byte[2048];
while (true)
...{
size = s.Read(data, 0, data.Length);
if (size > 0)
...{
streamWriter.Write(data, 0, size);
}
else
...{
break;
}
}
streamWriter.Close();
}
}
s.Close();
}
#endregion
private void button1_Click(object sender, EventArgs e)
...{
CopyFiles(@"D:图标TecSmart", @"C:photo");
}
private void btnzip_Click(object sender, EventArgs e)
...{
string[] FileProperties = new string[2];
FileProperties[0] = @"D:图标TecSmart";//待压缩文件目录
FileProperties[1] = @"E:DBd.zip"; //压缩后的目标文件 ,E盘中须有DB文件夹
ZipFileMain(FileProperties);
}
private void btnunzip_Click(object sender, EventArgs e)
...{
string[] FileProperties = new string[2];
FileProperties[0] = @"E:DBd.zip";//待解压的文件
FileProperties[1] = "E:/DB/DataSet/";//解压后放置的目标目录
UnZip(FileProperties);
}
}