/// <summary>
/// 解壓縮文件(壓縮文件中含有子目錄)
/// </summary>
/// <param name="zipfilepath">待解壓縮的文件路徑</param>
/// <param name="unzippath">解壓縮到指定目錄</param>
/// <returns>解壓后的文件列表</returns>
public List<string> UnZip(string zipfilepath, string unzippath)
{
//解壓出來的文件列表
List<string> unzipFiles = new List<string>();
//檢查輸出目錄是否以“\\”結尾
if (unzippath.EndsWith("\\") == false || unzippath.EndsWith(":\\") == false)
{
unzippath += "\\";
}
ZipInputStream s = new ZipInputStream(File.OpenRead(zipfilepath));
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(unzippath);
string fileName = Path.GetFileName(theEntry.Name);
//生成解壓目錄【用戶解壓到硬盤根目錄時,不需要創建】
if (!string.IsNullOrEmpty(directoryName))
{
Directory.CreateDirectory(directoryName);
}
if (fileName != String.Empty)
{
//如果文件的壓縮后大小為0那么說明這個文件是空的,因此不需要進行讀出寫入
if (theEntry.CompressedSize == 0)
continue;
//解壓文件到指定的目錄
directoryName = Path.GetDirectoryName(unzippath + theEntry.Name);
//建立下面的目錄和子目錄
Directory.CreateDirectory(directoryName);
//記錄導出的文件
unzipFiles.Add(unzippath + theEntry.Name);
FileStream streamWriter = File.Create(unzippath + theEntry.Name);
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();
GC.Collect();
return unzipFiles;
}
rar,zip可用,其他沒試過。