压缩文件夹实例代码
来源:互联网 发布:淘宝客服礼貌用语大全 编辑:程序博客网 时间:2024/05/17 08:00
首先要添加ICSharpCode.SharpZipLib引用
public class ZipFileDownload
{
/// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="DirectoryToZip">需要压缩的文件夹(绝对路径)</param>
/// <param name="ZipedPath">压缩后的文件路径(绝对路径)</param>
/// <param name="ZipedFileName">压缩后的文件名称(文件名,默认 同源文件夹同名)</param>
/// <param name="IsEncrypt">是否加密(默认 加密)</param>
public static string ZipDirectory(string DirectoryToZip, string ZipedPath, string ZipedFileName = "", bool IsEncrypt = true)
{
//需要压缩的文件夹不存在则返回
if (!System.IO.Directory.Exists(DirectoryToZip))
{
return string.Empty;
}
string ZipFileName = string.IsNullOrEmpty(ZipedFileName) ? ZipedPath + new DirectoryInfo(DirectoryToZip).Name + ".zip" : ZipedPath + ZipedFileName + ".zip";
using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipFileName))
{
using (ZipOutputStream s = new ZipOutputStream(ZipFile))
{
if (IsEncrypt)
{ }
ZipSetp(DirectoryToZip, s, "");
}
}
return ZipFileName;
}
/// <summary>
/// 递归遍历目录
/// </summary>
/// <param name="strDirectory">需要压缩的文件夹(绝对路径)</param>
/// <param name="s"></param>
/// <param name="parentPath"></param>
private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
{
if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
{
strDirectory += Path.DirectorySeparatorChar;
}
Crc32 crc = new Crc32();
string[] filenames = Directory.GetFileSystemEntries(strDirectory);
foreach (string file in filenames)// 遍历所有的文件和目录
{
if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
{
string pPath = parentPath;
pPath += file.Substring(file.LastIndexOf("\\") + 1);
pPath += "\\";
ZipSetp(file, s, pPath);
}
else // 否则直接压缩文件
{
using (FileStream fs = File.OpenRead(file))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1);
ZipEntry entry = new ZipEntry(fileName);
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
File.Delete(file);
}
}
}
}
- 压缩文件夹实例代码
- 压缩文件夹代码
- Java压缩文件夹和压缩文件代码:
- Java代码压缩文件夹及多文件压缩---简单案例
- C#利用SharpZipLib解压或压缩文件夹实例操作
- 压缩文件夹
- 压缩文件夹
- 压缩文件夹 .
- 压缩文件夹
- Java创建文件夹及文件实例代码
- 遍历文件夹内的文件 实例代码
- 网上找的压缩文件夹代码,方便以后使用
- 文件夹下面的图片压缩上传服务器-----之代码封装
- 压缩任意多层的文件夹 Java代码实现
- zip压缩和解压缩文件夹
- C# DataSet String Byte[] 压缩 解压缩 代码 实例
- Asp.net在线文件压缩与解压实例代码
- 【SQLite】常用操作SQL及压缩SQLite的实例代码
- liferay中工程类型说明
- Python类的实例属性详解
- 通过shape如何画一个圆?
- Oracle创建表空间、用户、赋予权限、修改密码
- 图的半径
- 压缩文件夹实例代码
- TextView设置超链接
- 【Docker】 导入对应的war包进行docker操作
- 重定向地址栏信息不改变
- HTML5自适应手机屏幕
- 关于调用淘宝IP获取城市信息-- 随笔
- JSP中的application,它的基类竟然是ServletContext!
- [noip2011tg] 铺地毯
- ViewPager子View滑动事件冲突解决