日期:2014-05-20  浏览次数:21010 次

压缩文件夹及子文件夹????????
一个文件夹含有文件和子文件夹,子文件夹中也含有文件,要把这个文件夹压缩到另一个目录中。然后从压缩文件中解压。
请问该怎么写????

------解决方案--------------------
你需要先到网上下载ICSharpCode.SharpZipLib.dll
然后引用

用下面的方法实现压缩和解压缩

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace NetStars
{
public class ZipFiles
{

/// <summary>
/// 创建一个压缩文件
/// </summary>
/// <param name= "zipFilename "> 压缩后的文件名 </param>
/// <param name= "sourceDirectory "> 待压缩文件的所在目录 </param>
public static void PackFiles(string zipFilename, string sourceDirectory)
{
FastZip fz = new FastZip();
fz.CreateEmptyDirectories = true;
fz.CreateZip(zipFilename, @sourceDirectory, true, " ");
fz = null;
}

/// <summary>
/// 解压缩文件
/// </summary>
/// <param name= "zipFile "> 待解压缩的文件 </param>
/// <param name= "directory "> 解压缩后文件存放的目录 </param>
public static bool UnpackFiles(string zipFile, string directory)
{
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);

ZipInputStream zis = new ZipInputStream(File.OpenRead(zipFile));
ZipEntry theEntry = null;
while ((theEntry = zis.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
if (directoryName != string.Empty)
Directory.CreateDirectory(directory + directoryName);

if (fileName != string.Empty)
{
FileStream streamWriter = File.Create(Path.Combine(directory, theEntry.Name));
int size = 2048;
byte[] data = new byte[size];
while (true)
{
size = zis.Read(data, 0, data.Length);
if (size > 0)
streamWriter.Write(data, 0, size);
else
break;
}

streamWriter.Close();
}
}

zis.Close();
return true;
}

}
}