打包成Zip
using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using System.Collections;
/// <summary>
/// zipCommon 的摘要说明
/// </summary>
public class zipCommon
{
public zipCommon()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary> 压缩文件
/// 压缩文件
/// </summary>
/// <param name="sourceFilePath"></param>
/// <param name="destinationZipFilePath"></param>
public static void CreateZip(string sourceFilePath, string destinationZipFilePath)
{
if (sourceFilePath[sourceFilePath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
sourceFilePath += System.IO.Path.DirectorySeparatorChar;
ZipOutputStream zipStream = new ZipOutputStream(File.Create(destinationZipFilePath));
zipStream.SetLevel(6); // 压缩级别 0-9
CreateZipFiles(sourceFilePath, zipStream, sourceFilePath);
zipStream.Finish();
zipStream.Close();
}
/// <summary> 递归压缩文件
/// 递归压缩文件
/// </summary>
/// <param name="sourceFilePath">待压缩的文件或文件夹路径</param>
/// <param name="zipStream">打包结果的zip文件路径(类似 D:\WorkSpace\a.zip),全路径包括文件名和.zip扩展名</param>
/// <param name="staticFile"></param>
public static void CreateZipFiles(string sourceFilePath, ZipOutputStream zipStream, string staticFile)
{
Crc32 crc = new Crc32();
string[] filesArray = Directory.GetFileSystemEntries(sourceFilePath);
foreach (string file in filesArray)
{
if (Directory.Exists(file))//如果当前是文件夹,递归
{
CreateZipFiles(file, zipStream, staticFile);
}
else //如果是文件,开始压缩
{
FileStream fileStream = File.OpenRead(file);
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
string tempFile = file.Substring(staticFile.LastIndexOf("\\") + 1);
ZipEntry entry = new ZipEntry(tempFile);
entry.DateTime = DateTime.Now;
entry.Size = fileStream.Length;
fileStream.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zipStream.PutNextEntry(entry);
zipStream.Write(buffer, 0, buffer.Length);
}
}
}
/// <summary>
/// 过滤特殊字符
/// </summary>
/// <param name="strtxt"></param>
/// <returns></returns>
public static string FilterSpecial(string strtxt)
{
if (string.IsNullOrEmpty(strtxt) == true)
{
return strtxt;
}
string[] aryReg = { ":", "?", "<", ">", "\\", "/", "|", "*", "\"" };
for (int i = 0; i < aryReg.Length; i++)
{
strtxt = strtxt.Replace(aryReg[i], string.Empty);
}
strtxt = strtxt.Replace(@"""", "");
strtxt = strtxt.Replace(@"\", "");
return strtxt;
}
/// <summary>
/// 导出word
/// </summary>
/// <param name="sText">导出内容</param>
/// <param name="sBookMark">书签</param>
/// <param name="sfileName">模板word文件路径</param>
/// <param name="sAtherFileName">导出word文件路径</param>
public static void OUTword(Hashtable httable, string sfileName, string sAtherFileName)
{
if (File.Exists(sAtherFileName.ToString()) == true)
{
File.Delete(sAtherFileName.ToString());
}
File.Copy(sfileName.ToString(), sAtherFileName.ToString());
Aspose.Words.Document doc = new Aspose.Words.Document(sAtherFileName);
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
foreach (DictionaryEntry de in httable)
{
if (builder.MoveToBookmark(de.Key.ToString()))
{
if (de.Value.ToString().IndexOf("../") >= 1)
{
builder.InsertImage(HttpContext.Current.Server.MapPath(de.Value.ToString()));
}
else
{
builder.InsertHtml(de.Value.ToString());
}
}
}
doc.Save(sAtherFileName);
}
}