• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#.net压缩文件解压上传及文件压缩下载

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Lib
{
public class ZipHelper
{
private volatile static ZipHelper _instance = null;
private static readonly object lockHelper = new object();
private ZipHelper() { }
/// <summary>
/// 单例模式, 考虑线程安全,只允许一个线程访问。
/// </summary>
public static ZipHelper Instance
{
get
{
if (_instance == null)
{
lock (lockHelper)
{
if (_instance == null)
_instance = new ZipHelper();
}
}
return _instance;
}
}
/// <summary>
/// 解压ZIP文件
/// </summary>
/// <param name="zipFile"></param>
/// <param name="targetDirectory"></param>
/// <param name="passWord"></param>
/// <param name="overWrite"></param>
public ResultEx UnZip(HttpPostedFileBase file, string targetDirectory, string passWord, bool overWrite = true)
{
try
{
if (Directory.Exists(targetDirectory))
Directory.CreateDirectory(targetDirectory);

if (!targetDirectory.EndsWith("\\")) targetDirectory += "\\";
using (ZipInputStream zipFiles = new ZipInputStream(file.InputStream))
{
zipFiles.Password = passWord;
ZipEntry theEntry;
while ((theEntry = zipFiles.GetNextEntry()) != null)
{
string directoryName = string.Empty;
string pathToZip = "";
pathToZip = theEntry.Name;

if (pathToZip != "")
directoryName = Path.GetDirectoryName(pathToZip) + "\\";
string fileName = Path.GetFileName(pathToZip);
Directory.CreateDirectory(targetDirectory + directoryName);

if (fileName != "")
{
if ((File.Exists(targetDirectory + directoryName + fileName) && overWrite) || (!File.Exists(targetDirectory + directoryName + fileName)))
{
using (FileStream streamWriter = File.Create(targetDirectory + directoryName + fileName))
{
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = zipFiles.Read(data, 0, data.Length);
if (size > 0)
streamWriter.Write(data, 0, size);
else
break;
}
streamWriter.Close();
}
}
}

}
}
return ResultEx.Init();
}
catch
{
return ResultEx.Init($"导入失败,当前只能导入ZIP格式请确认格式是否有问题!");
}

}

/// <summary>
/// 某目录压缩成ZIP压缩文件
/// </summary>
/// <param name="filesPath"></param>
/// <param name="zipFilePath"></param>
public static void CreateZipFile(string filesPath, string zipFilePath)
{
if (!Directory.Exists(filesPath))
{
Console.WriteLine("Cannot find directory '{0}'", filesPath);
return;
}
try
{
string[] filenames = Directory.GetFiles(filesPath);
using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
{

s.SetLevel(9); // 压缩级别 0-9
//s.Password = "123"; //Zip压缩文件密码
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();
}
}
catch (Exception ex)
{
Console.WriteLine("Exception during processing {0}", ex);
}
}

/// <summary>
/// 选中文件压缩成ZIP文件
/// </summary>
/// <param name="filenames">文件路径集合</param>
/// <param name="zipFilePath">压缩后ZIP路径</param>
/// <returns></returns>
public ResultEx CreateZipFile(List<string> filenames, string zipFilePath,string OutName)
{
if (filenames.Count == 0)
{
return ResultEx.Init(false, "没有找到可以导出的证件");
}
if (!Directory.Exists(zipFilePath))
Directory.CreateDirectory(zipFilePath);
var zipFile = $"{OutName}{ DateTime.Now.ToString("yyyyMMddHHmmss")}.zip";
try
{
using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath + zipFile)))
{
s.SetLevel(9); // 压缩级别 0-9
//s.Password = "123"; //Zip压缩文件密码
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 ResultEx.Init(true, "压缩成功", zipFile);
}
catch (Exception ex)
{
return ResultEx.Init(ex);
}
}
}
}
  

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#中两个窗体间的数据传递发布时间:2022-07-10
下一篇:
C#IIS站点管理--Microsoft.Web.Administration.dll发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap