怎么样使用组件多类型文件解压缩组件sharpziplib.dll 官网

[转]注册ICSharpCode.SharpZipLib.dll
在之前的一篇&小喵用一个类别把SharpZip做了一个包装,让使用上能够更直观。小喵心想如果能够再把他写成COM+组件的话,那么就能够让ASP或者VB呼叫使用,可用范围更扩大。
转成COM+的组件需要一些额外的设定,这与一般COM+组件的写法是一样的,有需要的请参考这篇:
但是小喵遇到一个状况,当小喵写好的组件注册后,要呼叫使用的时候却出现以下的错误讯息
无法加载档案或组件&'ICSharpCode.SharpZipLib,
Version=0.85.4.369, Culture=neutral,
PublicKeyToken=1b03e6acf1164f73'&或其相依性的其中之一。&系统找不到指定的档案。
原因在于ICSharpCode.SharpZipLib.dll没有注册到系统中,因此当小喵写的COM+要呼叫该dll的时候,就找不到了。
因此必须在OS中把这个dll注册。由于该dll并非COM+的dll,所以要用以下的方式来注册
首先开启Visual Studio
2005的命令提示字符(VS2005的命令窗口,需安装SDK才有),接着将ICSharpCode.SharpZipLib.dll复制到某个存放dll的文件夹,然后输入以下的两个指令:
RegAsm /tlb
ICSharpCode.SharpZipLib.dllGACUTIL /i ICSharpCode.SharpZipLib.dll
这样会将该dll产生tlb并且注册到GAC中。COM+就能够正确的呼叫该dll并执行压缩/解压缩了。
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。1088人阅读
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:105489次
排名:千里之外
转载:23篇
(2)(1)(1)(3)(1)(3)(4)(2)(2)(7)(3)C# ICSharpCode.SharpZipLib.dll文件压缩和解压功能类整理,上传文件或下载文件很常用 - 推酷
C# ICSharpCode.SharpZipLib.dll文件压缩和解压功能类整理,上传文件或下载文件很常用
1 using ICSharpCode.SharpZipLib.Z
3 using System.IO;
5 namespace ZTO.WayBill.Utilities
/// &summary&
/// 压缩类
/// /kissdodog/p/3525295.html
/// &/summary&
public class ZipHelper
/// &summary&
/// 压缩文件夹
/// &/summary&
/// &param name=&source&&源目录&/param&
/// &param name=&s&&ZipOutputStream对象&/param&
public static void Compress(string source, ZipOutputStream s)
string[] filenames = Directory.GetFileSystemEntries(source);
foreach (string file in filenames)
if (Directory.Exists(file))
// 递归压缩子文件夹
Compress(file, s);
using (FileStream fs = File.OpenRead(file))
byte[] buffer = new byte[4 * 1024];
// 此处去掉盘符,如D:\123\1.txt 去掉D:
ZipEntry entry = new ZipEntry(file.Replace(Path.GetPathRoot(file), &&));
entry.DateTime = DateTime.N
s.PutNextEntry(entry);
int sourceB
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes & 0);
/// &summary&
/// 解压缩
/// &/summary&
/// &param name=&sourceFile&&压缩包完整路径地址&/param&
/// &param name=&targetPath&&解压路径是哪里&/param&
/// &returns&&/returns&
public static bool Decompress(string sourceFile, string targetPath)
if (!File.Exists(sourceFile))
throw new FileNotFoundException(string.Format(&未能找到文件 '{0}' &, sourceFile));
if (!Directory.Exists(targetPath))
Directory.CreateDirectory(targetPath);
using (var s = new ZipInputStream(File.OpenRead(sourceFile)))
ZipEntry theE
while ((theEntry = s.GetNextEntry()) != null)
if (theEntry.IsDirectory)
string directorName = bine(targetPath, Path.GetDirectoryName(theEntry.Name));
string fileName = bine(directorName, Path.GetFileName(theEntry.Name));
if (!Directory.Exists(directorName))
Directory.CreateDirectory(directorName);
if (!String.IsNullOrEmpty(fileName))
using (FileStream streamWriter = File.Create(fileName))
int size = 4096;
byte[] data = new byte[size];
while (size & 0)
streamWriter.Write(data, 0, size);
size = s.Read(data, 0, data.Length);
return true;
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)_asp.net开发_ThinkSAAS
免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)
免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)
内容来源: 网络
前面介绍了六种.NET组件,其中有一种组件是写文件的压缩和解压,现在介绍另一种文件的解压缩组件SharpZipLib。在这个组件介绍系列中,只为简单的介绍组件的背景和简单的应用,读者在阅读时可以结合官网的相关介绍和在本地实际操作。
相关的组件功能非常强大,在笔者的介绍中只是提及到简单的应用,需要了解更多的操作和特性,可以根据官网介绍,或者查看DLL文件的相关类和方法,以此来扩展相关的业务需要。
SharpZipLib是一个完全在C#中为.NET平台编写的Zip,GZip,Tar和BZip2库。
一.SharpZipLib组件概述:
ziplib(SharpZipLib,以前的NZipLib)是一个完全在C#为.NET平台编写的Zip,GZip,Tar和BZip2库。它实现为一个程序集(可安装在GAC中),因此可以轻松地集成到其他项目(任何.NET语言)中。 #ziplib的创建者这样说:“我已经将zip库移植到C#,因为我需要gzip / zip压缩,我不想使用libzip.dll或类似的东西我想要的所有在纯C#“。
SharpZipLib官网提供的下载操作:.NET 1.1,.NET 2.0(3.5,4.0),.NET CF 1.0,.NET CF 2.0的装配:下载237 KB,源代码和示例下载708 KB;源代码和示例下载708 KB;帮助文件下载1208 KB;
SharpZipLib是在GPL下发布,遵守开源协议。
二.SharpZipLib核心类和方法介绍:
以上简单的介绍了SharpZipLib组件的相关背景,现在具体看一下该组件的相关核心类和方法:
1.ZipOutputStream类PutNextEntry():
public void PutNextEntry(ZipEntry entry)
bool hasC
if (entry == null)
throw new ArgumentNullException("entry");
if (this.entries == null)
throw new InvalidOperationException("ZipOutputStream was finished");
if (this.curEntry != null)
this.CloseEntry();
if (this.entries.Count == 0x7fffffff)
throw new ZipException("Too many entries for Zip file");
CompressionMethod compressionMethod = pressionM
int defaultCompressionLevel = this.defaultCompressionL
entry.Flags &= 0x800;
this.patchEntryHeader = false;
if (entry.Size == 0L)
pressedSize = entry.S
entry.Crc = 0L;
compressionMethod = CompressionMethod.S
hasCrc = true;
hasCrc = (entry.Size &= 0L) && entry.HasC
if (compressionMethod == CompressionMethod.Stored)
if (!hasCrc)
if (!base.CanPatchEntries)
compressionMethod = CompressionMethod.D
defaultCompressionLevel = 0;
pressedSize = entry.S
hasCrc = entry.HasC
if (!hasCrc)
if (!base.CanPatchEntries)
entry.Flags |= 8;
this.patchEntryHeader = true;
if (base.Password != null)
entry.IsCrypted = true;
if (entry.Crc & 0L)
entry.Flags |= 8;
entry.Offset = this.
pressionMethod = compressionM
this.curMethod = compressionM
this.sizePatchPos = -1L;
if ((this.useZip64_ == UseZip64.On) || ((entry.Size & 0L) && (this.useZip64_ == UseZip64.Dynamic)))
entry.ForceZip64();
this.WriteLeInt(0x4034b50);
this.WriteLeShort(entry.Version);
this.WriteLeShort(entry.Flags);
this.WriteLeShort((byte) pressionMethodForHeader);
this.WriteLeInt((int) entry.DosTime);
if (hasCrc)
this.WriteLeInt((int) entry.Crc);
if (entry.LocalHeaderRequiresZip64)
this.WriteLeInt(-1);
this.WriteLeInt(-1);
this.WriteLeInt(entry.IsCrypted ? (((int) pressedSize) + 12) : ((int) pressedSize));
this.WriteLeInt((int) entry.Size);
if (this.patchEntryHeader)
this.crcPatchPos = base.baseOutputStream_.P
this.WriteLeInt(0);
if (this.patchEntryHeader)
this.sizePatchPos = base.baseOutputStream_.P
if (entry.LocalHeaderRequiresZip64 || this.patchEntryHeader)
this.WriteLeInt(-1);
this.WriteLeInt(-1);
this.WriteLeInt(0);
this.WriteLeInt(0);
byte[] buffer = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
if (buffer.Length & 0xffff)
throw new ZipException("Entry name too long.");
ZipExtraData extraData = new ZipExtraData(entry.ExtraData);
if (entry.LocalHeaderRequiresZip64)
extraData.StartNewEntry();
if (hasCrc)
extraData.AddLeLong(entry.Size);
extraData.pressedSize);
extraData.AddLeLong(-1L);
extraData.AddLeLong(-1L);
extraData.AddNewEntry(1);
if (!extraData.Find(1))
throw new ZipException("Internal error cant find extra data");
if (this.patchEntryHeader)
this.sizePatchPos = extraData.CurrentReadI
extraData.Delete(1);
if (entry.AESKeySize & 0)
AddExtraDataAES(entry, extraData);
byte[] entryData = extraData.GetEntryData();
this.WriteLeShort(buffer.Length);
this.WriteLeShort(entryData.Length);
if (buffer.Length & 0)
base.baseOutputStream_.Write(buffer, 0, buffer.Length);
if (entry.LocalHeaderRequiresZip64 && this.patchEntryHeader)
this.sizePatchPos += base.baseOutputStream_.P
if (entryData.Length & 0)
base.baseOutputStream_.Write(entryData, 0, entryData.Length);
this.offset += (30 + buffer.Length) + entryData.L
if (entry.AESKeySize & 0)
this.offset += entry.AESOverheadS
this.curEntry =
this.crc.Reset();
if (compressionMethod == CompressionMethod.Deflated)
base.deflater_.Reset();
base.deflater_.SetLevel(defaultCompressionLevel);
this.size = 0L;
if (entry.IsCrypted)
if (entry.AESKeySize & 0)
this.WriteAESHeader(entry);
else if (entry.Crc & 0L)
this.WriteEncryptionHeader(entry.DosTime && 0x10);
this.WriteEncryptionHeader(entry.Crc);
2.ZipOutputStream类Finish():
public override void Finish()
if (this.entries != null)
if (this.curEntry != null)
this.CloseEntry();
long count = this.entries.C
long sizeEntries = 0L;
foreach (ZipEntry entry in this.entries)
this.WriteLeInt(0x2014b50);
this.WriteLeShort(0x33);
this.WriteLeShort(entry.Version);
this.WriteLeShort(entry.Flags);
this.WriteLeShort((short) pressionMethodForHeader);
this.WriteLeInt((int) entry.DosTime);
this.WriteLeInt((int) entry.Crc);
if (entry.IsZip64Forced() || (pressedSize &= 0xffffffffL))
this.WriteLeInt(-1);
this.WriteLeInt((int) pressedSize);
if (entry.IsZip64Forced() || (entry.Size &= 0xffffffffL))
this.WriteLeInt(-1);
this.WriteLeInt((int) entry.Size);
byte[] buffer = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
if (buffer.Length & 0xffff)
throw new ZipException("Name too long.");
ZipExtraData extraData = new ZipExtraData(entry.ExtraData);
if (entry.CentralHeaderRequiresZip64)
extraData.StartNewEntry();
if (entry.IsZip64Forced() || (entry.Size &= 0xffffffffL))
extraData.AddLeLong(entry.Size);
if (entry.IsZip64Forced() || (pressedSize &= 0xffffffffL))
extraData.pressedSize);
if (entry.Offset &= 0xffffffffL)
extraData.AddLeLong(entry.Offset);
extraData.AddNewEntry(1);
extraData.Delete(1);
if (entry.AESKeySize & 0)
AddExtraDataAES(entry, extraData);
byte[] entryData = extraData.GetEntryData();
byte[] buffer3 = (ment != null) ? ZipConstants.ConvertToArray(entry.Flags, ment) : new byte[0];
if (buffer3.Length & 0xffff)
throw new ZipException("Comment too long.");
this.WriteLeShort(buffer.Length);
this.WriteLeShort(entryData.Length);
this.WriteLeShort(buffer3.Length);
this.WriteLeShort(0);
this.WriteLeShort(0);
if (entry.ExternalFileAttributes != -1)
this.WriteLeInt(entry.ExternalFileAttributes);
else if (entry.IsDirectory)
this.WriteLeInt(0x10);
this.WriteLeInt(0);
if (entry.Offset &= 0xffffffffL)
this.WriteLeInt(-1);
this.WriteLeInt((int) entry.Offset);
if (buffer.Length & 0)
base.baseOutputStream_.Write(buffer, 0, buffer.Length);
if (entryData.Length & 0)
base.baseOutputStream_.Write(entryData, 0, entryData.Length);
if (buffer3.Length & 0)
base.baseOutputStream_.Write(buffer3, 0, buffer3.Length);
sizeEntries += ((0x2e + buffer.Length) + entryData.Length) + buffer3.L
using (ZipHelperStream stream = new ZipHelperStream(base.baseOutputStream_))
stream.WriteEndOfCentralDirectory(count, sizeEntries, this.offset, this.zipComment);
this.entries = null;
3.ZipEntry类Clone():
public object Clone()
ZipEntry entry = (ZipEntry) base.MemberwiseClone();
if (this.extra != null)
entry.extra = new byte[this.extra.Length];
Array.Copy(this.extra, 0, entry.extra, 0, this.extra.Length);
return
4.ZipOutputStream类Write():
public override void Write(byte[] buffer, int offset, int count)
if (this.curEntry == null)
throw new InvalidOperationException("No open entry.");
if (buffer == null)
throw new ArgumentNullException("buffer");
if (offset & 0)
throw new ArgumentOutOfRangeException("offset", "Cannot be negative");
if (count & 0)
throw new ArgumentOutOfRangeException("count", "Cannot be negative");
if ((buffer.Length - offset) & count)
throw new ArgumentException("Invalid offset/count combination");
this.crc.Update(buffer, offset, count);
this.size +=
switch (this.curMethod)
case CompressionMethod.Stored:
if (base.Password != null)
this.CopyAndEncrypt(buffer, offset, count);
base.baseOutputStream_.Write(buffer, offset, count);
break;
case CompressionMethod.Deflated:
base.Write(buffer, offset, count);
break;
三.SharpZipLib实例:
1.压缩单个文件:
/// &summary&
/// 压缩单个文件
/// &/summary&
/// &param name="fileToZip"&要压缩的文件&/param&
/// &param name="zipedFile"&压缩后的文件&/param&
/// &param name="compressionLevel"&压缩等级&/param&
/// &param name="blockSize"&每次写入大小&/param&
public static void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize)
if (string.IsNullOrEmpty(fileToZip))
throw new ArgumentNullException(fileToZip);
if (string.IsNullOrEmpty(zipedFile))
throw new ArgumentNullException(zipedFile);
if (!File.Exists(fileToZip))
throw new FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
using (var zipFile = File.Create(zipedFile))
using (var zipStream = new ZipOutputStream(zipFile))
using (var streamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read))
var fileName = fileToZip.Substring(fileToZip.LastIndexOf("", StringComparison.Ordinal) + 1);
var zipEntry = new ZipEntry(fileName);
zipStream.PutNextEntry(zipEntry);
zipStream.SetLevel(compressionLevel);
var buffer = new byte[blockSize];
int sizeR
sizeRead = streamToZip.Read(buffer, 0, buffer.Length);
zipStream.Write(buffer, 0, sizeRead);
while (sizeRead & 0);
catch (Exception ex)
throw new Exception(ex.Message);
streamToZip.Close();
zipStream.Finish();
zipStream.Close();
zipFile.Close();
catch (IOException ioex)
throw new IOException(ioex.Message);
catch (Exception ex)
throw new Exception(ex.Message);
2. 压缩单个文件:
/// &summary&
/// 压缩单个文件
/// &/summary&
/// &param name="fileToZip"&要进行压缩的文件名&/param&
/// &param name="zipedFile"&压缩后生成的压缩文件名&/param&
public static void ZipFile(string fileToZip, string zipedFile)
if (string.IsNullOrEmpty(fileToZip))
throw new ArgumentException(fileToZip);
if (string.IsNullOrEmpty(zipedFile))
throw new ArgumentException(zipedFile);
if (!File.Exists(fileToZip))
throw new FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
using (var fs = File.OpenRead(fileToZip))
var buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
using (var zipFile = File.Create(zipedFile))
using (var zipStream = new ZipOutputStream(zipFile))
var fileName = fileToZip.Substring(fileToZip.LastIndexOf("", StringComparison.Ordinal) + 1);
var zipEntry = new ZipEntry(fileName);
zipStream.PutNextEntry(zipEntry);
zipStream.SetLevel(5);
zipStream.Write(buffer, 0, buffer.Length);
zipStream.Finish();
zipStream.Close();
catch (IOException ioex)
throw new IOException(ioex.Message);
catch (Exception ex)
throw new Exception(ex.Message);
3.压缩多层目录:
/// &summary&
/// 压缩多层目录
/// &/summary&
/// &param name="strDirectory"&目录&/param&
/// &param name="zipedFile"&压缩文件&/param&
public static void ZipFileDirectory(string strDirectory, string zipedFile)
if (string.IsNullOrEmpty(strDirectory))
throw new ArgumentException(strDirectory);
if (string.IsNullOrEmpty(zipedFile))
throw new ArgumentException(zipedFile);
using (var zipFile = File.Create(zipedFile))
using (var s = new ZipOutputStream(zipFile))
ZipSetp(strDirectory, s, "");
4.递归遍历目录:
/// &summary&
/// 递归遍历目录
/// &/summary&
/// &param name="strDirectory"&目录&/param&
/// &param name="s"&ZipOutputStream对象&/param&
/// &param name="parentPath"&父路径&/param&
private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
strDirectory += Path.DirectorySeparatorC
var crc = new Crc32();
var filenames = Directory.GetFileSystemEntries(strDirectory);
// 遍历所有的文件和目录
foreach (var file in filenames)
// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
if (Directory.Exists(file))
var pPath = parentP
pPath += file.Substring(file.LastIndexOf("", StringComparison.Ordinal) + 1);
pPath += "";
ZipSetp(file, s, pPath);
// 否则直接压缩文件
//打开压缩文件
using (var fs = File.OpenRead(file))
var buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
var fileName = parentPath + file.Substring(file.LastIndexOf("", StringComparison.Ordinal) + 1);
var entry = new ZipEntry(fileName)
DateTime = DateTime.Now,
Size = fs.Length
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.V
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
catch (IOException ioex)
throw new IOException(ioex.Message);
catch (Exception ex)
throw new Exception(ex.Message);
5.解压缩一个 zip 文件:
/// &summary&
/// 解压缩一个 zip 文件。
/// &/summary&
/// &param name="zipedFile"&The ziped file.&/param&
/// &param name="strDirectory"&The STR directory.&/param&
/// &param name="password"&zip 文件的密码。&/param&
/// &param name="overWrite"&是否覆盖已存在的文件。&/param&
public void UnZip(string zipedFile, string strDirectory, string password, bool overWrite)
if (string.IsNullOrEmpty(zipedFile))
throw new ArgumentException(zipedFile);
if (string.IsNullOrEmpty(strDirectory))
throw new ArgumentException(strDirectory);
if (string.IsNullOrEmpty(password))
throw new ArgumentException(password);
if (strDirectory == "")
strDirectory = Directory.GetCurrentDirectory();
if (!strDirectory.EndsWith(""))
strDirectory = strDirectory + "";
using (var s = new ZipInputStream(File.OpenRead(zipedFile)))
s.Password =
ZipEntry theE
while ((theEntry = s.GetNextEntry()) != null)
var directoryName = string.E
var pathToZip = theEntry.N
if (pathToZip != "")
directoryName = Path.GetDirectoryName(pathToZip) + "";
var fileName = Path.GetFileName(pathToZip);
Directory.CreateDirectory(strDirectory + directoryName);
if (fileName == "") continue;
if ((!File.Exists(strDirectory + directoryName + fileName) || !overWrite) &&
(File.Exists(strDirectory + directoryName + fileName))) continue;
using (var streamWriter = File.Create(strDirectory + directoryName + fileName))
var data = new byte[2048];
while (true)
var size = s.Read(data, 0, data.Length);
if (size & 0)
streamWriter.Write(data, 0, size);
break;
streamWriter.Close();
s.Close();
catch (IOException ioex)
throw new IOException(ioex.Message);
catch (Exception ex)
throw new Exception(ex.Message);
四.总结:
以上是对SharpZipLib组件的相关介绍,本文的讲解上比较的浅显,如果需要深入的学习可以进入官网进行详细的学习。组件的功能是很强大的,如何在项目中使用组件,完成我们在项目中需要实现的功能,这就是对每个开发者提出了要求,需要我们仔细的去考虑。
任何学习都需要我们自己去探索和思考,对于一个开发者来说,最重要的就是思考,因为在我们的职业生涯中,没有什么的重要性能够超过思考。如果有不足之处还望各位读者包含,并留言指正。
.NET组件介绍系列:
一款开源免费的.NET文档操作组件DocX(.NET组件介绍之一)
高效而稳定的企业级.NET Office 组件Spire(.NET组件介绍之二)
最好的.NET开源免费ZIP库DotNetZip(.NET组件介绍之三)
免费开源的DotNet二维码操作组件ThoughtWorks.QRCode(.NET组件介绍之四)
免费开源的DotNet任务调度组件Quartz.NET(.NET组件介绍之五)
免费高效实用的Excel操作组件NPOI(.NET组件介绍之六)
免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)内容来源:
PHP开发框架
开发工具/编程工具
服务器环境
ThinkSAAS商业授权:
ThinkSAAS为用户提供有偿个性定制开发服务
ThinkSAAS将为商业授权用户提供二次开发指导和技术支持
让ThinkSAAS更好,把建议拿来。
开发客服微信}

我要回帖

更多关于 sharpziplib 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信