需要用到SharpLibZip
Zip
public class Zip
{
/// <summary>
/// 壓縮文件夾
/// </summary>
/// <param name=
/// <param name=
/// <param name=
/// <param name=
public void CompressDirectory(string dirPath
{
byte[] buffer = new byte[bufferSize];
using (ZipOutputStream s = new ZipOutputStream(File
{
s
CompressDirectory(dirPath
s
s
}
}
/// <summary>
/// 壓縮文件夾
/// </summary>
/// <param name=
/// <param name=
/// <param name=
/// <param name=
private void CompressDirectory(string root
{
root = root
string[] fileNames = Directory
string[] dirNames = Directory
string relativePath = path
if (relativePath !=
{
relativePath = relativePath
}
int sourceBytes;
foreach (string file in fileNames)
{
ZipEntry entry = new ZipEntry(relativePath + Path
entry
s
using (FileStream fs = File
{
do
{
sourceBytes = fs
s
} while (sourceBytes >
}
}
foreach (string dirName in dirNames)
{
string relativeDirPath = dirName
ZipEntry entry = new ZipEntry(relativeDirPath
s
CompressDirectory(root
}
}
/// <summary>
/// 解壓縮zip文件
/// </summary>
/// <param name=
/// <param name=
/// <param name=
public void Extract(string zipFilePath
{
extractPath = extractPath
byte[] data = new byte[bufferSize];
int size;
using (ZipInputStream s = new ZipInputStream(File
{
ZipEntry entry;
while ((entry = s
{
string directoryName = Path
string fileName = Path
//先創建目錄
if (directoryName
{
Directory
}
if (fileName != String
{
using (FileStream streamWriter = File
{
while (true)
{
size = s
if (size >
{
streamWriter
}
else
{
break;
}
}
}
}
}
}
}
}
網上找了很久 包括老外的site也找了 沒找到現成的 自己寫了一個
使用方法
使用方法
Zip z = new Zip();
z
//上面的解壓縮沒啥大問題
z
這個代碼有一點點小問題 沒有異常處理 壓縮過程中 有文件被編輯或者被刪除 那就無法壓縮了
如果這樣那也會異常
z
From:http://tw.wingwit.com/Article/program/net/201311/13330.html