ZIP文件解压
public class DZip
{
/// <summary>
/// 压缩为ZIP文件
/// </summary>
public void Zip(string directory,string fileName)
{
//using (var archive = ZipArchive.Create())
//{
// archive.AddAllFromDirectory(@"C:\\source");
// archive.SaveTo(@"C:\\new.zip", CompressionType.GZip);
//}
using (var archive = ZipArchive.Create())
{
archive.AddAllFromDirectory(directory);
archive.SaveTo(fileName, CompressionType.BZip2);
}
//using (var zip = File.OpenWrite(fileName))
//using (var zipWriter = WriterFactory.Open(File.Open(fileName,FileMode.Append),ArchiveType.Zip,CompressionType.BZip2))
//{
// string[] filesList = Directory.GetFiles(directory);
// foreach (var filePath in filesList)
// {
// zipWriter.Write(Path.GetFileName(filePath), filePath);
// }
//}
}
/// <summary>
/// ZIP文件解压
/// </summary>
/// <param name="pathZip">zip文件所在的路径</param>
/// <param name="toDirectory">要解压到的文件夹</param>
public void UnZip(string pathZip, string toDirectory)
{
var archive = ArchiveFactory.Open(pathZip);
foreach (var entry in archive.Entries)
{
if (!entry.IsDirectory)
{
entry.WriteToDirectory(toDirectory, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
}
}
}