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);
}
}
}
}