使用开源类库ICSharpCode.SharpZipLib.Zip可以实现压缩与解压缩功能,源代码和DLL可以从http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx下载;

有两种方式可以实现压缩与解压缩,这里只提供了ZIP格式;

1、DLL本身提供的快速压缩与解压缩,直接代码:

using System;
using ICSharpCode.SharpZipLib.Zip; namespace Zip
{
public static class FastZipHelper
{
#region CreateZipFile public static bool CreateZip(string zipFileName, string sourceDirectory, bool recurse, string fileFilter,
string directoryFilter, bool createEmptyDirectories,
bool restoreAttributesOnExtract, bool restoreDateTimeOnExtract)
{
try
{
FastZip fastZip = new FastZip();
fastZip.CreateEmptyDirectories = createEmptyDirectories;
fastZip.RestoreAttributesOnExtract = restoreAttributesOnExtract;
fastZip.RestoreDateTimeOnExtract = restoreDateTimeOnExtract;
fastZip.CreateZip(zipFileName, sourceDirectory, recurse, fileFilter, directoryFilter);
return true;
}
catch (Exception ex)
{
return false;
}
} public static bool CreateZip(string zipFileName, string sourceDirectory, bool recurse, string fileFilter,
string directoryFilter, bool createEmptyDirectories)
{
return CreateZip(zipFileName, sourceDirectory, recurse, fileFilter, directoryFilter, createEmptyDirectories,
true, true);
} public static bool CreateZip(string zipFileName, string sourceDirectory, bool recurse, string fileFilter,
string directoryFilter)
{
return CreateZip(zipFileName, sourceDirectory, recurse, fileFilter, directoryFilter, true);
} public static bool CreateZip(string zipFileName, string sourceDirectory, bool recurse, string fileFilter)
{
return CreateZip(zipFileName, sourceDirectory, recurse, fileFilter, string.Empty);
} public static bool CreateZip(string zipFileName, string sourceDirectory, bool recurse)
{
return CreateZip(zipFileName, sourceDirectory, recurse, string.Empty);
} public static bool CreateZip(string zipFileName, string sourceDirectory)
{
return CreateZip(zipFileName, sourceDirectory, true);
} #endregion #region ExtractZip public static bool ExtractZip(string zipFileName, string targetDirectory, FastZip.Overwrite overwrite,
FastZip.ConfirmOverwriteDelegate confirmDelegate, string fileFilter,
string directoryFilter, bool restoreDateTime, bool createEmptyDirectories,
bool restoreAttributesOnExtract, bool restoreDateTimeOnExtract)
{
try
{
FastZip fastZip = new FastZip();
fastZip.CreateEmptyDirectories = createEmptyDirectories;
fastZip.RestoreAttributesOnExtract = restoreAttributesOnExtract;
fastZip.RestoreDateTimeOnExtract = restoreDateTimeOnExtract;
fastZip.ExtractZip(zipFileName, targetDirectory, overwrite, confirmDelegate, fileFilter, directoryFilter,
restoreDateTime);
return true;
}
catch (Exception ex)
{
return false;
}
} public static bool ExtractZip(string zipFileName, string targetDirectory, FastZip.Overwrite overwrite,
FastZip.ConfirmOverwriteDelegate confirmDelegate, string fileFilter,
string directoryFilter, bool restoreDateTime, bool createEmptyDirectories)
{
return ExtractZip(zipFileName, targetDirectory, overwrite, confirmDelegate, fileFilter, directoryFilter,
restoreDateTime, createEmptyDirectories, true, true);
} public static bool ExtractZip(string zipFileName, string targetDirectory, FastZip.Overwrite overwrite,
FastZip.ConfirmOverwriteDelegate confirmDelegate, string fileFilter,
string directoryFilter, bool restoreDateTime)
{
return ExtractZip(zipFileName, targetDirectory, overwrite, confirmDelegate, fileFilter, directoryFilter,
restoreDateTime, true);
} public static bool ExtractZip(string zipFileName, string targetDirectory, FastZip.Overwrite overwrite,
FastZip.ConfirmOverwriteDelegate confirmDelegate, string fileFilter,
string directoryFilter)
{
return ExtractZip(zipFileName, targetDirectory, overwrite, confirmDelegate, fileFilter, directoryFilter,
true);
} public static bool ExtractZip(string zipFileName, string targetDirectory, FastZip.Overwrite overwrite,
FastZip.ConfirmOverwriteDelegate confirmDelegate, string fileFilter)
{
return ExtractZip(zipFileName, targetDirectory, overwrite, confirmDelegate, fileFilter, string.Empty);
} public static bool ExtractZip(string zipFileName, string targetDirectory)
{
return ExtractZip(zipFileName, targetDirectory, string.Empty);
} public static bool ExtractZip(string zipFileName, string targetDirectory, string fileFilter)
{
return ExtractZip(zipFileName, targetDirectory, FastZip.Overwrite.Always, null, fileFilter);
} #endregion
}
}

  

2、自己实现:

using System;
using System.Collections.Generic;
using System.IO;
using ICSharpCode.SharpZipLib.Zip; namespace Zip
{
public static class ZipHelper
{
public static string[] GetDirectories(string sourceDirectory)
{
List<string> result = new List<string>(); string[] directories = Directory.GetDirectories(sourceDirectory);
if (directories.Length > 0)
{
foreach (var directory in directories)
{
result.Add(directory);
result.AddRange(GetDirectories(directory));
}
}
return result.ToArray();
} public static string[] GetFiles(string sourceDirectory)
{
List<string> result = new List<string>();
result.AddRange(Directory.GetFiles(sourceDirectory));
string[] directories = Directory.GetDirectories(sourceDirectory);
if (directories.Length > 0)
{
foreach (var directory in directories)
{
result.AddRange(GetFiles(directory));
}
}
return result.ToArray();
} #region CreateZipFile public static void CreateZip(string sourceDirectory)
{
CreateZip(sourceDirectory, null);
} public static void CreateZip(string sourceDirectory, string zipFileName)
{
if (string.IsNullOrEmpty(sourceDirectory))
{
throw new ArgumentNullException("sourceDirectory", "sourceDirectory can not be null or empty.");
}
if (!Directory.Exists(sourceDirectory))
{
throw new DirectoryNotFoundException(sourceDirectory + " can not be found.");
}
if (string.IsNullOrEmpty(zipFileName))
{
zipFileName = sourceDirectory.TrimEnd('\\').TrimEnd('/') + ".zip";
}
// Depending on the directory this could be very large and would require more attention
// in a commercial package.
//string[] filenames = Directory.GetFiles(sourceDirectory);
string[] directories = GetDirectories(sourceDirectory);
string[] filenames = GetFiles(sourceDirectory);
if (!Directory.Exists(Path.GetDirectoryName(zipFileName)))
{
Directory.CreateDirectory(Path.GetDirectoryName(zipFileName));
}
// 'using' statements guarantee the stream is closed properly which is a big source
// of problems otherwise. Its exception safe as well which is great.
using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFileName)))
{
s.SetLevel(9); // 0 - store only to 9 - means best compression ZipEntryFactory factory = new ZipEntryFactory();
foreach (var directory in directories)
{
string virtualDirectory = directory.Replace(sourceDirectory, string.Empty);
ZipEntry zipEntry = factory.MakeDirectoryEntry(virtualDirectory);
zipEntry.DateTime = DateTime.Now;
s.PutNextEntry(zipEntry);
} byte[] buffer = new byte[4096]; foreach (string file in filenames)
{
// Using GetFileName makes the result compatible with XP
// as the resulting path is not absolute.
string newfileName = file.Replace(sourceDirectory, string.Empty);
ZipEntry entry = factory.MakeFileEntry(newfileName); // Setup the entry data as required. // Crc and size are handled by the library for seakable streams
// so no need to do them here. // Could also use the last write time or similar for the file.
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry); using (FileStream fs = File.OpenRead(file))
{
// Using a fixed size buffer here makes no noticeable difference for output
// but keeps a lid on memory usage.
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
} // Finish/Close arent needed strictly as the using statement does this automatically // Finish is important to ensure trailing information for a Zip file is appended. Without this
// the created file would be invalid.
s.Finish(); // Close is important to wrap things up and unlock the file.
s.Close();
}
} #endregion #region ExtractZip public static void ExtractZip(string zipFileName)
{
ExtractZip(zipFileName, null);
} public static void ExtractZip(string zipFileName, string targetDirectory)
{
if (string.IsNullOrEmpty(zipFileName))
{
throw new ArgumentNullException("zipFileName", "zipFileName can not be null or empty.");
} if (!File.Exists(zipFileName))
{
throw new FileNotFoundException(zipFileName + " can not be found.");
} if (string.IsNullOrEmpty(targetDirectory))
{
targetDirectory = Path.Combine(Path.GetDirectoryName(zipFileName),
Path.GetFileNameWithoutExtension(zipFileName));
}
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFileName)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
//create directory
string targetPath = Path.Combine(targetDirectory, theEntry.Name);
if (theEntry.IsDirectory)
{
Directory.CreateDirectory(targetPath);
}
if (theEntry.IsFile)
{
if (!Directory.Exists(Path.GetDirectoryName(targetPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(targetPath));
}
using (FileStream streamWriter = File.Create(targetPath))
{
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
}
}
}
}
} #endregion
}
}

  

使用ICSharpCode.SharpZipLib.Zip实现压缩与解压缩的更多相关文章

  1. 基于ICSharpCode.SharpZipLib.Zip的压缩解压缩

    原文:基于ICSharpCode.SharpZipLib.Zip的压缩解压缩 今天记压缩解压缩的使用,是基于开源项目ICSharpCode.SharpZipLib.Zip的使用. 一.压缩: /// ...

  2. C#调用 ICSharpCode.SharpZipLib.Zip 实现解压缩功能公用类

    最近想用个解压缩功能 从网上找了找 加自己修改,个人感觉还是比较好用的,直接上代码如下 using System; using System.Linq; using System.IO; using ...

  3. 利用ICSharpCode.SharpZipLib.Zip进行文件压缩

    官网http://www.icsharpcode.net/ 支持文件和字符压缩. 创建全新的压缩包 第一步,创建压缩包 using ICSharpCode.SharpZipLib.Zip; ZipOu ...

  4. C# ICSharpCode.SharpZipLib.dll文件压缩和解压功能类整理,上传文件或下载文件很常用

    工作中我们很多时候需要进行对文件进行压缩,比较通用的压缩的dll就是ICSharpCode.SharpZipLib.dll,废话不多了,网上也有很多的资料,我将其最常用的两个函数整理了一下,提供了一个 ...

  5. C# ZipHelper C#公共类 -- ICSharpCode.SharpZipLib.dll实现压缩和解压

    关于本文档的说明 本文档基于ICSharpCode.SharpZipLib.dll的封装,常用的解压和压缩方法都已经涵盖在内,都是经过项目实战积累下来的 1.基本介绍 由于项目中需要用到各种压缩将文件 ...

  6. 使用NPOI读取Excel报错ICSharpCode.SharpZipLib.Zip.ZipException:Wrong Local header signature

    写了一个小程序利用NPOI来读取Excel,弹出这样的报错: ICSharpCode.SharpZipLib.Zip.ZipException:Wrong Local header signature ...

  7. C# 利用ICSharpCode.SharpZipLib.dll 实现压缩和解压缩文件

    我们 开发时经常会遇到需要压缩文件的需求,利用C#的开源组件ICSharpCode.SharpZipLib, 就可以很容易的实现压缩和解压缩功能. 压缩文件: /// <summary> ...

  8. ICSharpCode.SharpZipLib.Zip

    //压缩整个目录下载 var projectFolder = Request.Params["folder"] != null ? Request.Params["fol ...

  9. 利用Java进行zip文件压缩与解压缩

    摘自: https://www.cnblogs.com/alphajuns/p/12442315.html 工具类: package com.alphajuns.util; import java.i ...

随机推荐

  1. HDU 4294 Multiple(搜索+数学)

    题意: 给定一个n,让求一个M,它是n个倍数并且在k进制之下 M的不同的数字最少. 思路: 这里用到一个结论就是任意两个数可以组成任何数的倍数.知道这个之后就可以用搜索来做了.还有一个问题就是最多找n ...

  2. 处理ASP.NET 请求(IIS)(2)

    ASP.NET与IIS是紧密联系的,由于IIS6.0与IIS7.0的工作方式的不同,导致ASP.NET的工作原理也发生了相应的变化. IIS6(IIS7的经典模式)与IIS7的集成模式的不同 IIS6 ...

  3. typeerror $.ajax is not a function

    在web开发中使用jQuery进行前端开发遇到这么个问题,纠结了很久终于解决了,下面说一下解决方法. 大家可以参照下面几种排查的方法. 1.首先检查是否引用jQuery的库. 2.页面如果使用的ifr ...

  4. 怎么让自己的java系统使用支付接口

    昨天花了好久的时间学习了支付接口的教,我看了前7集,就够用了,大家上网搜索一下传智播客在线支付还不错. 1.一开始有一个form表单 2.这个表单是他帮你写好的,有很多银行,银行的name都是特定的 ...

  5. maven发布的资源文件到tomcat项目下

    问题:项目中有hibernate的资源文件,src/main/java和src/main/resources都有这些文件,当启动项目时发现出错.但是src/main/java已经修改好了, 经查tom ...

  6. iOS Instruments之Core Animation动画性能调优(工具复选框选项介绍)

    Core Animation工具用来监测Core Animation性能.它给我们提供了周期性的FPS,并且考虑到了发生在程序之外的动画(见图12.4) Core Animation工具提供了一系列复 ...

  7. Spring在代码中获取bean的几种方式(转:http://www.dexcoder.com/selfly/article/326)

    方法一:在初始化时保存ApplicationContext对象 方法二:通过Spring提供的utils类获取ApplicationContext对象 方法三:继承自抽象类ApplicationObj ...

  8. Java如何连接到MySQL数据库的

    下载:mysql-connector-java-5.1.38.tar.gz http://dev.mysql.com/downloads/connector/j/ tar zxvf mysql-con ...

  9. iOS图片的伪裁剪(改变图片的像素值)

    0x00 原理 利用一张图片事先画好的图片(以下称为蒙板),盖在要被裁剪的的图片上,然后遍历蒙板上的像素点,修改被裁剪图片对应位置的像素的色值即可得到一些我们想要的不规则图片了(比如人脸) 0x01 ...

  10. paramiko SSH 模块简单应用。

    目的:需要ssh链接到Linux主机,执行telnet 命令,抓回显匹配制定内容. ssh --->执行telnet到本地端口--->执行类似 ls 的命令.匹配命令执行后的特定回显字段. ...