using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Common
{
public class FileHelper
{
/// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="dirPath">文件夹路径</param>
/// <param name="password">压缩包设置密码(注:可为空)</param>
/// <param name="zipFilePath">压缩包路径+名称+后缀(注:可为空,默认同目录)</param>
/// <returns></returns>
public string ZipFiles(string dirPath, string password, string zipFilePath)
{
if (zipFilePath == string.Empty)
{
//压缩文件名为空时使用文件夹名+.zip
zipFilePath = GetZipFilePath(dirPath);
}
try
{
string[] filenames = Directory.GetFiles(dirPath);
using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
{
s.SetLevel(9);
s.Password = password;
byte[] buffer = new byte[4096];
foreach (string file in filenames)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
s.Finish();
s.Close();
}
return zipFilePath;
}
catch (Exception ex)
{
return ex.ToString();
}
}
/// <summary>
/// 减压文件夹
/// </summary>
/// <param name="zipFilePath">压缩包地址+名称+后缀</param>
/// <param name="password">密码(注:可为空)</param>
/// <param name="unZippath">减压后保存的路径(注:可为空,默认同目录)</param>
/// <returns></returns>
public string UnZips(string zipFilePath, string password, string unZippath)
{
try
{
if (unZippath == string.Empty)
{
//解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹
unZippath = GetUnZipFilePath(zipFilePath);
}
if (CreatePath(unZippath) && IsExistFilePath(zipFilePath))
{
string directoryName = Path.GetDirectoryName(unZippath);
{
using (ZipInputStream zipInStream = new ZipInputStream(File.OpenRead(zipFilePath)))
{
zipInStream.Password = password;
ZipEntry entry = zipInStream.GetNextEntry();
do
{
using (FileStream fileStreamOut = File.Create(directoryName + "\\" + entry.Name))
{
int size = 2048;
byte[] buffer = new byte[size];
do
{
size = zipInStream.Read(buffer, 0, buffer.Length);
fileStreamOut.Write(buffer, 0, size);
} while (size > 0);
fileStreamOut.Close();
fileStreamOut.Dispose();
}
} while ((entry = zipInStream.GetNextEntry()) != null);
zipInStream.Close();
zipInStream.Dispose();
return unZippath;
}
}
}
return "请确认压缩包文件地址与解压后保存地址是否可以访问!";
}
catch (Exception ex)
{
return ex.ToString();
}
}
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="dirFilePath">文件路径+名称+后缀</param>
/// <param name="password">压缩包设置密码(注:可为空)</param>
/// <param name="zipFilePath">压缩包路径+名称+后缀(注:可为空,默认同目录)</param>
public string ZipFile(string dirFilePath, string password, string zipFilePath)
{
try
{
if (IsExistFilePath(dirFilePath))
{
if (zipFilePath == string.Empty)
{
zipFilePath = GetZipFilePath(dirFilePath.Replace(Path.GetExtension(dirFilePath), ""));
}
string filename = Path.GetFileName(dirFilePath);
FileStream streamToZip = new FileStream(dirFilePath, FileMode.Open, FileAccess.Read);
FileStream zipFile = File.Create(zipFilePath);
using (ZipOutputStream zipStream = new ZipOutputStream(zipFile))
{
ZipEntry zipEntry = new ZipEntry(filename);
zipStream.PutNextEntry(zipEntry);
zipStream.SetLevel(9);
zipStream.Password = password;
byte[] buffer = new byte[2048];
System.Int32 size = streamToZip.Read(buffer, 0, buffer.Length);
zipStream.Write(buffer, 0, size);
while (size < streamToZip.Length)
{
int sizeRead = streamToZip.Read(buffer, 0, buffer.Length);
zipStream.Write(buffer, 0, sizeRead);
size += sizeRead;
}
zipStream.Finish();
zipStream.Close();
streamToZip.Close();
}
return zipFilePath;
}
return "请确认压缩包文件地址与解压后保存地址是否可以访问!";
}
catch (Exception ex)
{
return ex.ToString();
}
}
/// <summary>
/// 创建路径
/// </summary>
/// <param name="path">路径</param>
/// <returns></returns>
public bool CreatePath(string path)
{
try
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
return true;
}
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 文件是否存在
/// </summary>
/// <param name="filePath">路劲+名称+后缀</param>
/// <returns></returns>
public bool IsExistFilePath(string filePath)
{
if (!File.Exists(filePath))
{
return false;
}
return true;
}
/// <summary>
/// 获取默认压缩路径+文件名+后缀【.zip】
/// </summary>
/// <param name="path">需要压缩的文件夹路径(注:不包含.后缀)</param>
/// <returns>与压缩文件同一目录路径</returns>
public string GetZipFilePath(string path)
{
if (path.EndsWith("\\"))
{
path = path.Substring(0, path.Length - 1);
}
return path + ".zip";
}
/// <summary>
/// 获取默认解压路径
/// </summary>
/// <param name="path">需要解压的压缩包文件地址</param>
/// <returns>与解压文件同一目录路径</returns>
public string GetUnZipFilePath(string path)
{
path = path.Replace(Path.GetFileName(path), Path.GetFileNameWithoutExtension(path));
if (!path.EndsWith("/"))
{
path += "/";
}
return path;
}
/// <summary>
/// 获取路径中所有文件
/// </summary>
/// <param name="path">路径</param>
/// <returns></returns>
private Hashtable getAllFies(string path)
{
Hashtable FilesList = new Hashtable();
DirectoryInfo fileDire = new DirectoryInfo(path);
if (fileDire.Exists)
{
this.getAllDirFiles(fileDire, FilesList);
this.getAllDirsFiles(fileDire.GetDirectories(), FilesList);
}
this.getAllDirFiles(fileDire, FilesList);
this.getAllDirsFiles(fileDire.GetDirectories(), FilesList);
return FilesList;
}
/// <summary>
/// 获取一个文件夹下的所有文件夹里的文件
/// </summary>
/// <param name="dirs"></param>
/// <param name="filesList"></param>
private void getAllDirsFiles(DirectoryInfo[] dirs, Hashtable filesList)
{
foreach (DirectoryInfo dir in dirs)
{
foreach (FileInfo file in dir.GetFiles("*.*"))
{
filesList.Add(file.FullName, file.LastWriteTime);
}
this.getAllDirsFiles(dir.GetDirectories(), filesList);
}
}
/// <summary>
/// 获取一个文件夹下的文件
/// </summary>
/// <param name="strDirName">目录名称</param>
/// <param name="filesList">文件列表HastTable</param>
private void getAllDirFiles(DirectoryInfo dir, Hashtable filesList)
{
foreach (FileInfo file in dir.GetFiles("*.*"))
{
filesList.Add(file.FullName, file.LastWriteTime);
}
}
}
}