首先引用ICSharpCode.SharpZipLib.dll,没有在这里下载:http://files.cnblogs.com/files/cang12138/ICSharpCode.SharpZipLib.rar

压缩打包代码:

    /// <summary>
/// 生成压缩文件
/// </summary>
/// <param name="strZipPath">生成的zip文件的路径</param>
/// <param name="strZipTopDirectoryPath">源文件的上级目录</param>
/// <param name="intZipLevel">T压缩等级</param>
/// <param name="strPassword">压缩包解压密码</param>
/// <param name="filesOrDirectoriesPaths">源文件路径</param>
/// <returns></returns>
private bool Zip(string strZipPath, string strZipTopDirectoryPath, int intZipLevel, string strPassword, string[] filesOrDirectoriesPaths)
{
try
{
List<string> AllFilesPath = new List<string>();
if (filesOrDirectoriesPaths.Length > ) // get all files path
{
for (int i = ; i < filesOrDirectoriesPaths.Length; i++)
{
if (File.Exists(filesOrDirectoriesPaths[i]))
{
AllFilesPath.Add(filesOrDirectoriesPaths[i]);
}
else if (Directory.Exists(filesOrDirectoriesPaths[i]))
{
GetDirectoryFiles(filesOrDirectoriesPaths[i], AllFilesPath);
}
}
} if (AllFilesPath.Count > )
{ ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(strZipPath));
zipOutputStream.SetLevel(intZipLevel);
zipOutputStream.Password = strPassword; for (int i = ; i < AllFilesPath.Count; i++)
{
string strFile = AllFilesPath[i].ToString();
try
{
if (strFile.Substring(strFile.Length - ) == "") //folder
{
string strFileName = strFile.Replace(strZipTopDirectoryPath, "");
if (strFileName.StartsWith(""))
{
strFileName = strFileName.Substring();
}
ZipEntry entry = new ZipEntry(strFileName);
entry.DateTime = DateTime.Now;
zipOutputStream.PutNextEntry(entry);
}
else //file
{
FileStream fs = File.OpenRead(strFile); byte[] buffer = new byte[fs.Length];
fs.Read(buffer, , buffer.Length); string strFileName = strFile.Replace(strZipTopDirectoryPath, "");
if (strFileName.StartsWith(""))
{
strFileName = strFileName.Substring();
}
ZipEntry entry = new ZipEntry(strFileName);
entry.DateTime = DateTime.Now;
zipOutputStream.PutNextEntry(entry);
zipOutputStream.Write(buffer, , buffer.Length); fs.Close();
fs.Dispose();
}
}
catch
{
continue;
}
} zipOutputStream.Finish();
zipOutputStream.Close(); return true;
}
else
{
return false;
}
}
catch
{
return false;
}
} /// <summary>
/// Gets the directory files.
/// </summary>
/// <param name="strParentDirectoryPath">源文件路径</param>
/// <param name="AllFilesPath">所有文件路径</param>
private void GetDirectoryFiles(string strParentDirectoryPath, List<string> AllFilesPath)
{
string[] files = Directory.GetFiles(strParentDirectoryPath);
for (int i = ; i < files.Length; i++)
{
AllFilesPath.Add(files[i]);
}
string[] directorys = Directory.GetDirectories(strParentDirectoryPath);
for (int i = ; i < directorys.Length; i++)
{
GetDirectoryFiles(directorys[i], AllFilesPath);
}
if (files.Length == && directorys.Length == ) //empty folder
{
AllFilesPath.Add(strParentDirectoryPath);
}
} //调用 string strZipPath = @"D:\ConsultSystem\mgr\user.zip";
string strZipTopDirectoryPath = @"D:\ConsultSystem\mgr\";
int intZipLevel = ;
string strPassword = "";
string[] filesOrDirectoriesPaths = new string[] { @"D:\ConsultSystem\mgr\user.txt" };
Zip(strZipPath, strZipTopDirectoryPath, intZipLevel, strPassword, filesOrDirectoriesPaths);

出自:http://www.cnblogs.com/qiuweiguo/archive/2011/08/09/2132061.html

最新文章

  1. CSS预处理器Sass、LESS 和 Stylus
  2. STM32F10xx CAN BUS相关库文件&quot;stm32f10x_can.c&quot;内的库函数解析
  3. 继承(JAVA)
  4. python——第一天
  5. IT公司100题-1-二叉树转换为双链表
  6. android开发系列之socket编程
  7. c#抽象工厂模式
  8. 什么是C#,.NET,ASP.NET?
  9. .NET知识点总结二(笔记整合)
  10. linker command failed with exit code 1 (use -v to see
  11. (译)UEFI 启动:实际工作原理
  12. NewsDao
  13. Certificates does not conform to algorithm constraints
  14. Servlet工作原理解析 《深入分析java web 技术内幕》第九章
  15. js每隔一段时间执行函数
  16. easyui 布局之window和panel一起使用时,拉动window宽高时panel不跟随一起变化
  17. 转 消息中间件:RocketMQ 介绍(特性、术语、原理、优缺点、消息顺序、消息重复)
  18. 超级干货 :一文读懂数据可视化 ZT
  19. 鼠标监听事件MouseListener
  20. python--教你做个最简单的tcp通信。。

热门文章

  1. IFrame中Session丢失的解决办法
  2. 转:.NET 环境中使用RabbitMQ
  3. 【面试】惠普IT电面
  4. Tautology
  5. javascript(js)中的substring和substr方法
  6. [置顶] 2014年八大最热门IT技能
  7. 【模拟】Vijos P1005 超长数字串
  8. server 2008 ftp 环境重点说明
  9. Permutations II ——LeetCode
  10. nyoj 222 整数中的1个数以及这类问题