Asp.Net 文件压缩
using System;
using System.Web;
using System.IO;
using System.Configuration;
using System.IO.Packaging;
using System.ComponentModel;
using System.Net;
/// <summary>
/// 文件压缩
/// </summary>
public class ZipHelper
{
private static readonly string FileUrl = ConfigurationManager.AppSettings["FileUrl"];
private static readonly string ApplicationPath = ConfigurationManager.AppSettings["ApplicationPath"];
/// <summary>
/// 文件地址
/// </summary>
private string zipFolderPath { get; set; }
/// <summary>
/// 压缩级别
/// </summary>
public CompressionOption comOption { get; set; }
/// <summary>
/// 资源锁标记
/// </summary>
private static object locker = new object();
private string zipDefName = string.Empty;
private int currentNum = 0;
/// <summary>
/// ZIP
/// </summary>
/// <param name="zipFolderPath">存放zip文件地址</param>
/// <param name="comOption">压缩级别</param>
public ZipHelper(string zipFolderPath, CompressionOption comOption)
{
zipDefName = Guid.NewGuid().ToString().Replace("-", "") + ".zip";
this.comOption = comOption;
if (!string.IsNullOrEmpty(zipFolderPath))
{
this.zipFolderPath = zipFolderPath;
}
else
{
this.zipFolderPath = HttpContext.Current.Server.MapPath(string.Format("{0}/ZipFolderCache/{1}", ApplicationPath, zipDefName));
}
}
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="urlLists">文件相对路径Url</param>
/// <param name="msg">消息</param>
/// <param name="urlType"></param>
/// <returns></returns>
public bool ZipFolderList(string urlLists, ref string msg, UrlTypeEnum urlType = UrlTypeEnum.Local)
{
if (string.IsNullOrEmpty(urlLists))
{
msg = "url地址错误";
return false;
}
string filePath = zipFolderPath.Substring(0, zipFolderPath.LastIndexOf(‘\\‘) + 1);
if (!Directory.Exists(filePath)) //文件夹树是否存在,不存在则重新创建文件目录
{
Directory.CreateDirectory(filePath);
}
string[] urlArr = urlLists.Trim(‘;‘).Split(‘;‘);
using (Package package = Package.Open(zipFolderPath, FileMode.OpenOrCreate))
{
foreach (string url in urlArr)
{
try
{
ZipUrlFolder(url, package, urlType);
}
catch (InvalidOperationException e)
{
continue;//文件相同,忽略
}
catch (Exception ex)
{
msg = "打包错误!";
return false;
}
}
}
msg = string.Format("{0}/ZipFolderCache/{1}", FileUrl, zipDefName);
return true;
}
/// <summary>
/// zip文件
/// </summary>
/// <param name="url">文件相对地址</param>
/// <param name="package">压缩包</param>
/// <param name="urlType"></param>
private void ZipUrlFolder(string url, Package package, UrlTypeEnum urlType = UrlTypeEnum.Local)
{
string zipNewFilePath = url.Substring(url.LastIndexOf("/"));
PackagePart part = package.CreatePart(new Uri(zipNewFilePath, UriKind.Relative), System.Net.Mime.MediaTypeNames.Application.Zip, comOption);
if (urlType == UrlTypeEnum.Local)
{
FileInfo fi = new FileInfo(HttpContext.Current.Server.MapPath(ApplicationPath + "/" + url));
using (FileStream fs = fi.OpenRead())
{
CopyStream(fs, part.GetStream());
}
}
else if (urlType == UrlTypeEnum.Online)
{
using (Stream stream = WebRequest.Create(url).GetResponse().GetResponseStream())
{
CopyStream(stream, part.GetStream());
}
}
}
/// <summary>
/// 流复制
/// </summary>
/// <param name="source">源流</param>
/// <param name="target">目标流</param>
private void CopyStream(Stream source, Stream target)
{
MemoryStream ms = new MemoryStream(); //防止{System.Net.ConnectStream}读取字节不全
source.CopyTo(ms); //写入内存流
byte[] b = new byte[ms.Length];
ms.Seek(0, SeekOrigin.Begin);
const int bufferSize = 0x1000;
byte[] buffer = new byte[bufferSize];
int readIndex = 0;
while ((readIndex = ms.Read(buffer, 0, bufferSize)) > 0)
{
target.Write(buffer, 0, bufferSize);
}
ms.Dispose();
ms.Close();
}
/// <summary>
///网络类型
/// </summary>
public enum UrlTypeEnum
{
/// <summary>
/// 网络Url
/// </summary>
[Description("网络Url")]
Online = 0,
/// <summary>
/// 本地服务Url
/// </summary>
[Description("本地服务Url")]
Local = 1
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。