c#FileHelper对文件压缩解压,压缩包加密
c#FileHelper对⽂件压缩解压,压缩包加密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;
/*****************************
* 概要:File
* 设计者:DuanXuWen
* 设计时间:20180309
* 版本:0.1
* 修改者:
* 修改时间:
* ***************************/
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();
}
}
/
// <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();
}
}
/// <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();
}
}
/
// <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";
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。