C#压缩文件夹
using System;
using System.Collections.Generic;
using System.Text;
///第三方dll
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using log4net;
using log4net.Config;
using System.Text.RegularExpressions;
namespace Test.BLL
{
public class TestZipFile
{
protected static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
/// <summary>
/// 加密压缩包的方法
/// </summary>
/// <param name="strFile"></param>
/// <param name="strZip"></param>
/// <param name="sPassWord"></param>
public void ZipFile(string strFile, string strZip, string sPassWord)
{
if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
strFile += Path.DirectorySeparatorChar;
ZipOutputStream s = new ZipOutputStream(File.Create(strZip));
if (sPassWord != "")
{
s.Password = sPassWord; //Zip压缩文件密码
}
s.SetLevel(6);
zip(strFile, s, strFile);
s.Finish();
s.Close();
}
/// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="strFile"></param>
/// <param name="strZip"></param>
/// <param name="sPassWord"></param>
public void ZipFile(string strFile, string strZip)
{
if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
strFile += Path.DirectorySeparatorChar;
ZipOutputStream s = new ZipOutputStream(File.Create(strZip));
s.SetLevel(6);
zip(strFile, s, strFile);
s.Finish();
s.Close();
}
private void zip(string strFile, ZipOutputStream s, string staticFile)
{
if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) strFile += Path.DirectorySeparatorChar;
Crc32 crc = new Crc32();
string[] filenames = Directory.GetFileSystemEntries(strFile);
foreach (string file in filenames)
{
if (Directory.Exists(file))
{
zip(file, s, staticFile);
}
else // 否则直接压缩文件
{
//打开压缩文件
FileStream fs = File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string tempfile = file.Substring(staticFile.LastIndexOf("\\") + 1);
ZipEntry entry = new ZipEntry(tempfile);
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);
}
}
}
}
}
C#压缩文件夹的更多相关文章
- C#利用SharpZipLib解压或压缩文件夹实例操作
最近要做一个项目涉及到C#中压缩与解压缩的问题的解决方法,大家分享. 这里主要解决文件夹包含文件夹的解压缩问题. )下载SharpZipLib.dll,在http://www.icsharpcode. ...
- Java使用线程池递归压缩文件夹下面的所有子文件
本文将介绍Java中利用线程池递归的方式压缩文件夹下面的所有子文件,具体方法如下: Gzip单个文件压缩 对于单个文件使用GZip压缩. package date0805.demo1; import ...
- C++复制、压缩文件夹
之前写过一篇用zlib库来压缩的,但zlib只能压缩文件,我需要压缩文件夹,要想压缩文件夹还得利用zlib库自己写代码,我是真的服了,一个开源库这么不好用. C++复制文件夹也是麻烦事,网上这篇文章: ...
- C#压缩文件夹坑~
dotNet疯狂之路No.29 今天很残酷,明天更残酷,后天很美好,但是绝大部分人是死在明天晚上,只有那些真正的英雄才能见到后天的太阳. We're here to put a dent in t ...
- .net压缩文件夹
1,引用:using System.IO.Packaging; 2,压缩文件的方法: /// <summary> /// 压缩文件夹到制定的路径 /// </summary> ...
- C#压缩文件,C#压缩文件夹,C#获取文件
using System; using System.Data; using System.Configuration; using System.Collections.Generic; using ...
- 使用7zip批量压缩文件夹到不同压缩包
for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "%%X.7z" "%%X\" ...
- 简单测试Demo:如何用Java压缩文件夹和文件
一.直接贴出测试代码 package com.jason.zip; import java.io.File; import java.io.FileInputStream; import java.i ...
- Java 压缩文件夹工具类(包含解压)
依赖jar <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons ...
随机推荐
- Bug测试报告--在线考试系统--金州勇士
项目名:在线考试系统 组名:金州勇士 测试者:宫丽君(nice!团队) 代码地址: ssh:git@git.coding.net:handsomeman/examm.git https://g ...
- 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数001·3D函数
<zw版·Halcon-delphi系列原创教程> Halcon分类函数001·3D函数 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“**”,替换:“proce ...
- Android之使用Volley框架在ListView中加载大量图片
1.listview 中的条目要用 Volley 中的 NetworkImageView,如果直接用ImageView也可以,但是要在getView方法中使用url地址设置为imageView的tag ...
- maven私有库配置
私有库的配置 协同开发过程中私有库可以为团队提升很大效率,之前我的私有库一直存在问题导致jar包导入异常.现在在这分享一下私有库配置的几个点,可能因为我们学校比较简陋所以配置的比较简单,欢迎大家补充 ...
- UBUNTU下Y86模拟器的安装和使用
UBUNTU下Y86模拟器的安装和使用 由于上周在虚拟机中安装Y86模拟器中出现了一些问题并且没得到解决,所以上周实验是在实验楼上做的,这几天练习了UBUNTU下Y86模拟器的安装和使用. 参考博客: ...
- ReactJs入门教程
现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了ReactJS,ReactJs的虚拟DOM(Virtual DOM)和组件化的开发深深的吸引了我,下面来跟我一起领 ...
- easyui DataGrid 工具类之 列属性class
public class ColumnVO { /** * 列标题文本 */ private String title; /** * 列字段名称 */ pr ...
- Oracle介绍(初学者必须知道的)
1.为什么学习数据库?(两个概念) 数据库的概念: 数据库是按照数据结构组织,存储和管理数据的仓库. 数据库,简单来说是本身可视为电子化的文件柜——存储电子文件的处所,用户可以对文件中的数据进行新增. ...
- GITHUB使用简介
前提:本地开通SSH服务 我是Ubuntu OS,其他OS自行查找,不难目的:利用Github的免费托管服务,创建自己的repo或者fork别人的repo.步骤:·安装客户端 安装如下两个git ...
- Core Data
• Core Data 是 iOS SDK 里的一个很强大的框架,允许程序员 以面向对象 的方式储存和管理数据 .使用 Core Data 框架,程序员可以很轻松有效 地通过面向对象的接口 ...