利用ICSharpCode进行压缩和解压缩
说说我利用ICSharpCode进行压缩和解压缩的一些自己的一下实践过程
1:组件下载地址
参考文章:C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件
2: 文件类
//****************************************************************************************
//作者:轻狂书生
//博客地址:http://www.cnblogs.com/xiaoshuai1992
//create: 2014/2/26
//功能:实现文件压缩
//使用方法:设置参数进行压缩
//***************************************************************************************** using System;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using System.Text; namespace ZipCompress
{
public class CompressFile
{
/// <summary>
/// 压缩文件参数
/// </summary>
public ZipParameter ZipParameter { get; set; }
/// <summary>
/// 压缩文件参数
/// </summary>
public DeZipParameter DeZipParameter { get; set; }
/// <summary>
/// 压缩文件参数
/// </summary>
public ZipFolderParameter ZipFolderParameter { get; set; } /// <summary>
/// 压缩文件返回压缩后的信息
/// </summary>
/// <returns>string 返回压缩后的提示信息</returns>
public void Compress()
{
FileStream Zip_File;
ZipOutputStream ZipStream;
ZipEntry ZipEntry;
//string rtnMessage = "";//返回的信息 try
{
//循环文件,如果文件不存在就不添加的压缩里面
for (int i = ; i < ZipParameter.ZIPFileList.Count; i++)
{
if (!File.Exists(ZipParameter.ZIPFileList[i]))
{
ZipParameter.ZIPFileList.RemoveAt(i);
i--;
} }
//没有有文件下面的压缩不执行
if (ZipParameter.ZIPFileList.Count == )
{
return;
}
//没有目录进行创建
if (!Directory.Exists(ZipParameter.ZIPDirectoryName))
{
Directory.CreateDirectory(ZipParameter.ZIPDirectoryName);
} // 解决文档名称乱码问题,出现乱码就是因为CodePage不对
Encoding gbk = Encoding.GetEncoding("gbk");
ICSharpCode.SharpZipLib.Zip.ZipConstants.DefaultCodePage = gbk.CodePage; //文件路径,文档路径与文件名称
string strPath = ZipParameter.ZIPDirectoryName + ZipParameter.ZIPName; Zip_File = File.Create(strPath);
ZipStream = new ZipOutputStream(Zip_File); ZipStream.Password = ZipParameter.Password;
ZipStream.SetLevel(ZipParameter.Level); foreach (string FileToZip in ZipParameter.ZIPFileList)
{
Zip_File = File.OpenRead(FileToZip);
byte[] buffer = new byte[Zip_File.Length];
Zip_File.Read(buffer, , buffer.Length);
Zip_File.Close();
ZipEntry = new ZipEntry(Path.GetFileName(FileToZip));
ZipStream.PutNextEntry(ZipEntry);
ZipStream.Write(buffer, , buffer.Length);
}
ZipStream.Finish();
ZipStream.Close();
Zip_File.Close(); }
catch (Exception ex)
{
throw ex;
}
finally
{
GC.Collect();
GC.Collect();
}
} /// <summary>
/// 压缩文件夹
/// </summary>
/// <returns></returns>
public void CompressFolder()
{
FastZip fz = new FastZip();
fz.CreateZip(ZipFolderParameter.ZipFileName, ZipFolderParameter.SourceDirectory, ZipFolderParameter.Recurse, ZipFolderParameter.FileFilter); } /// <summary>
/// 解压缩文件,可以解压到下层文件夹
/// </summary>
/// <returns>提示信息</returns>
public void Decompress()
{
//路径处理
string strDirectory = DeZipParameter.ZIPDirectoryName;
if (strDirectory == "")
strDirectory = Directory.GetCurrentDirectory();
if (!strDirectory.EndsWith("\\"))
strDirectory = strDirectory + "\\";
try
{
using (ZipInputStream s = new ZipInputStream(File.OpenRead(DeZipParameter.ZIPFullPath)))
{
s.Password = DeZipParameter.Password;
ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = "";
string pathToZip = "";
pathToZip = theEntry.Name; if (pathToZip != "")
directoryName = Path.GetDirectoryName(pathToZip) + "\\"; string fileName = Path.GetFileName(pathToZip); Directory.CreateDirectory(strDirectory + directoryName); if (fileName != "")
{
if ((File.Exists(strDirectory + directoryName + fileName)) || (!File.Exists(strDirectory + directoryName + fileName)))
{
using (FileStream streamWriter = File.Create(strDirectory + directoryName + fileName))
{
int size = ;
byte[] data = new byte[];
while (true)
{
size = s.Read(data, , data.Length); if (size > )
streamWriter.Write(data, , size);
else
break;
}
streamWriter.Close();
}
}
}
}
s.Close();
}
}
catch (Exception ex)
{
throw ex;
}
} }
}
3:参数类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ZipCompress
{
/// <summary>
/// 解压缩的一些属性
/// </summary>
public class DeZipParameter
{
private string zip_FullPath = "";
private string zip_DirectoryName = "";
private string zip_Password = "";
/// <summary>
/// 要解压缩的文件的完整路径
/// </summary>
public string ZIPFullPath
{
get { return zip_FullPath; }
set { zip_FullPath = value; }
} /// <summary>
/// 解压缩的文件路径
/// </summary>
public string ZIPDirectoryName
{
get { return zip_DirectoryName; }
set { zip_DirectoryName = value; }
} /// <summary>
/// 加密密碼
/// </summary>
public string Password
{
get { return zip_Password; }
set { zip_Password = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ZipCompress
{
/// <summary>
/// 压缩文件的类
/// </summary>
public class ZipParameter
{
private string zip_Name = "";
private string zip_DirectoryName = "";
private List<string> zip_FileList = new List<string>();
private string zip_Password = "";
private int zip_Level = ;
/// <summary>
/// 压缩后的文件名称
/// </summary>
public string ZIPName
{
get { return zip_Name; }
set { zip_Name = value; }
} /// <summary>
/// 压缩的文件路径
/// </summary>
public string ZIPDirectoryName
{
get { return zip_DirectoryName; }
set { zip_DirectoryName = value; }
}
/// <summary>
/// 压缩的文件列表
/// </summary>
public List<string> ZIPFileList
{
get { return zip_FileList; }
set { zip_FileList = value; }
}
/// <summary>
/// 加密密碼
/// </summary>
public string Password
{
get { return zip_Password; }
set { zip_Password = value; }
}
/// <summary>
/// 压缩比例等级,默认为6,0 - store only to 9 - means best compression
/// </summary>
public int Level
{
get { return zip_Level; }
set { zip_Level = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ZipCompress
{
/// <summary>
/// 压缩文件夹的类
/// </summary>
public class ZipFolderParameter
{
private string zipFileName = "";
private string sourceDirectory = "";
private bool recurse = true;
private string fileFilter = "";
/// <summary>
/// 生成的压缩文件
/// </summary>
public string ZipFileName
{
get { return zipFileName; }
set { zipFileName = value; }
} /// <summary>
/// 待压缩的文件夹
/// </summary>
public string SourceDirectory
{
get { return sourceDirectory; }
set { sourceDirectory = value; }
}
/// <summary>
/// 文件过滤,即决定那些后缀的文件被压缩
/// </summary>
public string FileFilter
{
get { return fileFilter; }
set { fileFilter = value; }
}
/// <summary>
/// 是否遍历子目录,True就遍历,False则不遍历
/// </summary>
public bool Recurse
{
get { return recurse; }
set { recurse = value; }
}
}
}
4:使用
ZipParameter zp = new ZipParameter();
zp.Password = "";
zp.Level = ;
zp.ZIPDirectoryName = @"C:\Users\Public\Pictures\Sample Pictures\";
zp.ZIPName = "Test.zip";
zp.ZIPFileList.Add(@"C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg");
zp.ZIPFileList.Add(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg");
zp.ZIPFileList.Add(@"C:\Users\WH130406P\Documents\RelatedTemplate.xlsx"); DeZipParameter dzp = new DeZipParameter();
dzp.ZIPFullPath = @"D:\项目必备\开发实例\wf20131124.zip";
dzp.ZIPDirectoryName = @"C:\Users\Public\Pictures\Sample Pictures\Test";
dzp.Password = ""; ZipFolderParameter zfp = new ZipFolderParameter();
zfp.ZipFileName = @"C:\Users\Public\Pictures\Sample Pictures\zipFolder.zip";
zfp.SourceDirectory = @"D:\xs\項目";
zfp.Recurse = true; CompressFile cprFile = new CompressFile();
cprFile.ZipParameter = zp;
cprFile.DeZipParameter = dzp;
cprFile.ZipFolderParameter = zfp;
cprFile.Compress();
cprFile.Decompress();
cprFile.CompressFolder();
上面都是我使用的过程,都是实践成功了的
利用ICSharpCode进行压缩和解压缩的更多相关文章
- C# 利用ICSharpCode.SharpZipLib.dll 实现压缩和解压缩文件
我们 开发时经常会遇到需要压缩文件的需求,利用C#的开源组件ICSharpCode.SharpZipLib, 就可以很容易的实现压缩和解压缩功能. 压缩文件: /// <summary> ...
- 在C#中利用SharpZipLib进行文件的压缩和解压缩收藏
我在做项目的时候需要将文件进行压缩和解压缩,于是就从http://www.icsharpcode.net(http://www.icsharpcode.net/OpenSource/SharpZipL ...
- .net 利用 GZipStream 压缩和解压缩
1.GZipStream 类 此类在 .NET Framework 2.0 版中是新增的. 提供用于压缩和解压缩流的方法和属性 2.压缩byte[] /// <summary> /// 压 ...
- 利用SharpZipLib进行字符串的压缩和解压缩
http://www.izhangheng.com/sharpziplib-string-compression-decompression/ 今天搞了一晚上压缩和解压缩问题,java压缩的字符串,用 ...
- C#利用SharpZipLib进行文件的压缩和解压缩
我在做项目的时候需要将文件进行压缩和解压缩,于是就从http://www.icsharpcode.net下载了关于压缩和解压缩的源码,但是下载下来后,面对这么多的代码,一时不知如何下手.只好耐下心来, ...
- C#- 压缩和解压缩的研究 .
用了第二种方法,感觉很不错,其他都没用过了.摘录下来,做一个备忘. 最近在网上查了一下在.net中进行压缩和解压缩的方法,方法有很多,我找到了以下几种: 1.利用.net自带的压缩和解压缩方法GZip ...
- .net中压缩和解压缩的处理
最近在网上查了一下在.net中进行压缩和解压缩的方法,方法有很多,我找到了以下几种: 1.利用.net自带的压缩和解压缩方法GZip 参考代码如下: //======================= ...
- iOS中使用ZipArchive压缩和解压缩文件-备
为什么我需要解压缩文件 有许多原因能解释为什么我要在工程中使用压缩和解压缩功能,下面是几个常见的原因: 苹果App Store的50M下载限制 苹 果公司出于流量的考虑,规定在非WIFI环境下,限制用 ...
- Java 的zip压缩和解压缩
Java 的zip压缩和解压缩 好久没有来这写东西了,今天中秋节,有个东西想拿出来分享,一来是工作中遇到的问题,一来是和csdn问候一下,下面就分享一个Java中的zip压缩技术,代码实现比较简单,代 ...
随机推荐
- Web安全1&沙箱隔离
1.web安全 Web安全的本质是信任问题 •由于信任,正常处理用户恶意的输入导致问题的产生 •非预期的输入(就是不是程序员预期的客户的输入) 安全是木桶原理,短的那块板决定的木桶世纪能装多少水,同样 ...
- BP神经网络的手写数字识别
BP神经网络的手写数字识别 ANN 人工神经网络算法在实践中往往给人难以琢磨的印象,有句老话叫“出来混总是要还的”,大概是由于具有很强的非线性模拟和处理能力,因此作为代价上帝让它“黑盒”化了.作为一种 ...
- 《Cracking the Coding Interview》——第6章:智力题——题目1
2014-03-19 06:40 题目:有20瓶药,其中19瓶装的都是1.0克的药片,只有1瓶装了1.1克的药.给你一个能称出具体克数的电子秤,只允许你称一次,怎么找出那瓶不一样的? 解法:如果药片管 ...
- 把现有Unity3d游戏向Windows Phone 8.1移植(基础)
最近在将一款现有的游戏向Windows Phone平台移植,暂时完成了一个小阶段,做一个总结. 开发环境: Windows 8.1 系统及以上,愿意的话,用Windows 10 尝鲜也可以. 微软账号 ...
- Windows10使用pip安装python包时报错-UnicodeDecodeError: 'ascii' codec c
本人是Windows10,用的方法2解决的 原文链接http://blog.csdn.net/all_over_servlet/article/details/45112221 先交待下开发环境: 操 ...
- Python学习2,小程序
今天主要就是弄懂了一个循环判 for i in [1,2]: for j in [1, 2, 3]: print(i, j) break else: print("for-j") ...
- sklearn中predict()与predict_proba()用法区别
predict是训练后返回预测结果,是标签值. predict_proba返回的是一个 n 行 k 列的数组, 第 i 行 第 j 列上的数值是模型预测 第 i 个预测样本为某个标签的概率,并且每一行 ...
- React跨域问题解决
https://segmentfault.com/q/1010000012732581 非跨域问题报错 -rpccorsdomain="http://localhost:3000" ...
- URAL 1684. Jack's Last Word ( KMP next函数应用 )
题意:问第二行的串能不能恰好分割成几个串,使得这几个串都是第一行串的前缀.如果是,输出No, 并输出这几个串,否则输出Yes. 这题是Special Judge,把两个串连接起来,中间用一个未出现过的 ...
- windows mobile 开发:让GPS一直在待机模式下也能运行
最近,遇到一个需求,就是每 30 秒更新一次 GPS 位置,在测试过程中,发现在系统待机后,更新 GPS 位置就不能正常运行了,搜索后,发现如下的解决方案,实际应用了之后,有效,赞!!! http:/ ...