ZIP文件压缩和解压
最近要做一个文件交互,上传和下载, 都是zip压缩文件,所以研究了下,写了如下的示例
注意引用 ICSharpCode.SharpZipLib.dll 文件
该dll文件可以到官方网站去下载, 我这里提供一个 .Net FrameWork 2.0 版本的 ,点此下载
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using ICSharpCode.SharpZipLib.Zip; namespace ConZIPTools
{
class Program
{
public static void Main(string[] args)
{
try
{
// ZipTool.UnZIP(@"F:\SharpZipLib_0860_Bin.zip", @"F:\M\SharpZipLib_0860_Bin\"); ZipTool.CreateZIP(@"F:\M\SharpZipLib_0860_Bin\net-11", @"F:\m.zip");
}
catch (Exception ex)
{ Console.WriteLine(ex.Message);
}
Console.WriteLine("程序执行完毕");
Console.ReadLine();
}
} public class ZipTool
{
/// <summary>
/// zip 压缩文件, 解压
/// </summary>
/// <param name="zipFilePath">zip压缩文件路径, 例如;F:\SharpZipLib_0860_Bin.zip</param>
/// <param name="saveDirectory">压缩文件加压后的路径,例如 F:\M\SharpZipLib_0860_Bin\ ,注意后面要带'\'</param>
public static void UnZIP(string zipFilePath, string saveDirectory)
{
// Perform simple parameter checking.
if (!File.Exists(zipFilePath))
{
Console.WriteLine("指定的文件路径找不到:" + zipFilePath);
return;
}
if (!Directory.Exists(saveDirectory))
{
//解压后存放的 文件夹路径
Directory.CreateDirectory(saveDirectory);
} using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
{ ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
Console.WriteLine("解压出来的文件名:" + theEntry.Name); string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name); // create directory
if (directoryName.Length > )
{
//创建目录
string saveDir = saveDirectory + directoryName;
Directory.CreateDirectory(saveDir);
} if (fileName != String.Empty)
{
string saveFilePath = saveDirectory + theEntry.Name;
using (FileStream streamWriter = File.Create(saveFilePath))
{
int size = ;
byte[] data = new byte[];
while (true)
{
size = s.Read(data, , data.Length);
if (size > )
{
streamWriter.Write(data, , size);
}
else
{
break;
}
}
}
}
}
} } /// <summary>
/// 创建 zip压缩文件
/// </summary>
/// <param name="fileDirectoryPath">需要压缩的文件夹路径,注意只压缩里面的文件, 不包括该文件夹下的子文件夹</param>
/// <param name="saveZipFile">保存后zip文件路径</param>
public static void CreateZIP(string fileDirectoryPath, string saveZipFile)
{
// Perform some simple parameter checking. More could be done
// like checking the target file name is ok, disk space, and lots
// of other things, but for a demo this covers some obvious traps. if (!Directory.Exists(fileDirectoryPath))
{
Console.WriteLine("Cannot find directory '{0}'", fileDirectoryPath);
return;
} try
{
// Depending on the directory this could be very large and would require more attention
// in a commercial package.
string[] filenames = Directory.GetFiles(fileDirectoryPath); if (filenames.Length<)
{
Console.WriteLine("该路径下的没有文件:" + fileDirectoryPath);
return;
}
// '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(saveZipFile)))
{
s.SetLevel(); // 0 - store only to 9 - means best compression byte[] buffer = new byte[]; foreach (string file in filenames)
{ // Using GetFileName makes the result compatible with XP
// as the resulting path is not absolute.
var entry = new ZipEntry(Path.GetFileName(file)); // 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, , buffer.Length);
s.Write(buffer, , sourceBytes);
} while (sourceBytes > );
}
} // 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();
}
}
catch (Exception ex)
{
Console.WriteLine("Exception during processing {0}", ex); // No need to rethrow the exception as for our purposes its handled.
}
}
}
}
ZIP文件压缩和解压的更多相关文章
- python zip文件压缩和解压
压缩 import shutil zipOutputName = "1234" # 输出1234.zip fileType = "zip" # 文件类型zip ...
- Ionic.Zip.dll文件压缩和解压
Ionic.Zip.dll文件压缩和解压 下载地址: http://download.csdn.net/detail/yfz19890410/5578515 1.下载Ionic.Zip.dll组件,添 ...
- linux常用命令:4文件压缩和解压命令
文件压缩和解压命令 压缩命令:gzip.tar[-czf].zip.bzip2 解压缩命令:gunzip.tar[-xzf].unzip.bunzip2 1. 命令名称:gzip 命令英文原意:GNU ...
- .net文件压缩和解压及中文文件夹名称乱码问题
/**************************注释区域内为引用http://www.cnblogs.com/zhaozhan/archive/2012/05/28/2520701.html的博 ...
- .NET中zip的压缩和解压
在.NET可以通过多种方式实现zip的压缩和解压:1.使用System.IO.Packaging:2.使用第三方类库:3.通过 System.IO.Compression 命名空间中新增的ZipArc ...
- c#自带压缩类实现的多文件压缩和解压
用c#自带的System.IO.Compression命名空间下的压缩类实现的多文件压缩和解压功能,缺点是多文件压缩包的解压只能调用自身的解压方法,和现有的压缩软件不兼容.下面的代码没有把多文件的目录 ...
- java 文件压缩和解压(ZipInputStream, ZipOutputStream)
最近在看java se 的IO 部分 , 看到 java 的文件的压缩和解压比较有意思,主要用到了两个IO流-ZipInputStream, ZipOutputStream,不仅可以对文件进行压缩,还 ...
- 黄聪:.NET中zip的压缩和解压——SharpCompress
使用Packaging无法实现通用的zip(使用其他工具压缩)的解压,只支持通过Packaging压缩包zip的解压,而SharpZipLib是基于“GPL”开源方式,风险比较大.在codeplex找 ...
- 文件压缩和解压 FileStream GZipStream
using (FileStream reader=new FileStream (@"c:\1.txt",FileMode.Open,FileAccess.Read)) { usi ...
随机推荐
- spring读取配置文件,且获取bean实例
import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.Xm ...
- 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_发布者策略控制
在 读经典——<CLR via C#>(Jeffrey Richter著) 笔记_高级管理控制(配置)中,是由程序集的发布者将程序集的一个新版本发送给管理员,后者安装程序集,并手动编辑应用 ...
- linux 底层 基础命令 路径信息
z基础命令: 打印 :echo "hello world“ 切换目录 cd / 显示当前路径 pwd 显示 目录下所有文件 ls 显示所有文件包括隐藏文件 ls ...
- P3796 【模板】AC自动机
传送门 AC自动机的模板 简单的理解就是字典树上的KMP 注意数组不要开太大 不然每次memset耗时太多 有一个小优化 每次走 fail 边找匹配时只有一些会更新答案 那么就可以把没用的fail边压 ...
- Swagger的坑
swagger.pathPatterns如果是譬如/w/.*,那么如果API中以w开头的描述就会在swagger-ui中显示不出来
- oracle Clob类型转换成String类型
转载:https://www.cnblogs.com/itmyhome/p/4131339.html Clob类型转换成String类型 oracle中表结构如下: create table GRID ...
- Vue.js递归组件实现动态树形菜单
使用Vue递归组件实现动态菜单 现在很多项目的菜单都是动态生成的,之前自己做项目也是遇到这种需求,翻看了官网案例,和网上大神的案例.只有两个感觉,官网的案例太简洁,没有什么注释,看起来不太好理解,大神 ...
- 这是通过 Open Live Writer(是个博客编辑器) 发布的
Open Live Writer 是开源的win10上的博客编辑器
- 百度webuploader 上传演示例子
前端代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="baiduWebU ...
- CTeX里面CTRL-Space和中文输入法的冲突问题解决
我使用的是windows xp,相信下面的方法也能应用到win7等windows系统上. 我希望在CTex套件的WinEdt 6.0里使用模板自动插入内容时,想快速从上到下遍历” * “并修改. 通过 ...