c#解压,压缩文件!!!
最近要做一个项目涉及到C#中压缩与解压缩的问题的解决方法,大家分享。
这里主要解决文件夹包含文件夹的解压缩问题。
1)下载SharpZipLib.dll,在http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx中有最新免费版本,“Assemblies for .NET 1.1, .NET 2.0, .NET CF 1.0, .NET CF 2.0: Download [297 KB] ”点击Download可以下载,解压后里边有好多文件夹,因为不同的版本,我用的FW2.0。
2)引用SharpZipLib.dll,在项目中点击项目右键-->添加引用-->浏览,找到要添加的DLL-->确认
3)改写了文件压缩和解压缩的两个类,新建两个类名字为ZipFloClass.cs,UnZipFloClass.cs
源码如下
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
/// <summary>
/// ZipFloClass 的摘要说明
/// </summary>
public class ZipFloClass
{
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); // 0 - store only to 9 - means best compression
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);
}
}
}
}
、、、、、、、、、、、、、、、
using System;
using System.Data;
using System.Web;
using System.Text;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Checksums;
/// <summary>
/// UnZipFloClass 的摘要说明
/// </summary>
public class UnZipFloClass
{
public string unZipFile(string TargetFile, string fileDir)
{
string rootFile = " ";
try
{
//读取压缩文件(zip文件),准备解压缩
ZipInputStream s = new ZipInputStream(File.OpenRead(TargetFile.Trim()));
ZipEntry theEntry;
string path = fileDir;
//解压出来的文件保存的路径
string rootDir = " ";
//根目录下的第一个子文件夹的名称
while ((theEntry = s.GetNextEntry()) != null)
{
rootDir = Path.GetDirectoryName(theEntry.Name);
//得到根目录下的第一级子文件夹的名称
if (rootDir.IndexOf("\\") >= 0)
{
rootDir = rootDir.Substring(0, rootDir.IndexOf("\\") + 1);
}
string dir = Path.GetDirectoryName(theEntry.Name);
//根目录下的第一级子文件夹的下的文件夹的名称
string fileName = Path.GetFileName(theEntry.Name);
//根目录下的文件名称
if (dir != " " )
//创建根目录下的子文件夹,不限制级别
{
if (!Directory.Exists(fileDir + "\\" + dir))
{
path = fileDir + "\\" + dir;
//在指定的路径创建文件夹
Directory.CreateDirectory(path);
}
}
else if (dir == " " && fileName != "")
//根目录下的文件
{
path = fileDir;
rootFile = fileName;
}
else if (dir != " " && fileName != "")
//根目录下的第一级子文件夹下的文件
{
if (dir.IndexOf("\\") > 0)
//指定文件保存的路径
{
path = fileDir + "\\" + dir;
}
}
if (dir == rootDir)
//判断是不是需要保存在根目录下的文件
{
path = fileDir + "\\" + rootDir;
}
//以下为解压缩zip文件的基本步骤
//基本思路就是遍历压缩文件里的所有文件,创建一个相同的文件。
if (fileName != String.Empty)
{
FileStream streamWriter = File.Create(path + "\\" + fileName);
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;
}
}
streamWriter.Close();
}
}
s.Close();
return rootFile;
}
catch (Exception ex)
{
return "1; " + ex.Message;
}
}
}
4)引用,新建一个页面,添加两个按钮,为按钮添加Click事件
源码如下
protected void Button1_Click(object sender, EventArgs e)
{
string[] FileProperties = new string[2];
FileProperties[0] = "D:\\unzipped\\";//待压缩文件目录
FileProperties[1] = "D:\\zip\\a.zip"; //压缩后的目标文件
ZipFloClass Zc = new ZipFloClass();
Zc.ZipFile(FileProperties[0], FileProperties[1]);
}
protected void Button2_Click(object sender, EventArgs e)
{
string[] FileProperties = new string[2];
FileProperties[0] = "D:\\zip\\b.zip";//待解压的文件
FileProperties[1] = "D:\\unzipped\\";//解压后放置的目标目录
UnZipFloClass UnZc = new UnZipFloClass();
UnZc.unZipFile(FileProperties[0], FileProperties[1]);
}
在解压缩的时候会出现一个问题:size mismatch: XXXXXXX
有个办法,我还没仔细看,但是有效:
打开SharpZipLib源代码,你找到ZIP文件夹下的,ZipInputStream.cs文件
然后找到这段
- if ((flags & 8) == 0 && (inf.TotalIn != csize || inf.TotalOut != size)) {
- throw new ZipException("Size mismatch: " + csize + ";" + size + " <-> " + inf.TotalIn + ";" + inf.TotalOut);
- }
把如上代码注释掉,然后编译,从新引入DLL到项目中就可以了.
转载于:https://www.cnblogs.com/guowei1027/archive/2010/01/05/1639973.html
c#解压,压缩文件!!!相关推荐
- R语言使用unzip函数解压压缩文件(Extract or List Zip Archives)
R语言使用unzip函数解压压缩文件(Extract or List Zip Archives) 目录 R语言使用unzip函数解压压缩文件(Extract or List Zip Archives) ...
- python批量解压文件_python 批量解压压缩文件的实例代码
下面给大家介绍python 批量解压压缩文件的实例代码,代码如下所述: #/usr/bin/python#coding=utf-8import os,sys import zipfile open_p ...
- php tp5在线解压压缩文件
php tp5在线解压压缩文件 没啥原理,直接上代码把 解压方法 /*** 解压zip文件到指定目录* @param {string} $filepath: 文件路径* @param {string} ...
- centos 安装并使用rar解压压缩文件
下载 下载自己的版本,下边以64位的为例: wget http://www.rarlab.com/rar/rarlinux-x64-5.6.1.tar.gz 解压 到文件夹/usr/local/rar ...
- 使用shell脚本一键式解压压缩文件
话不多说,先上代码: #!/bin/bash cd /opt/software ----文件的目录位置 count=`ls -l | grep '^-' | wc -l` ----保存当前目录下的文件 ...
- 【Delphi】从内存读取或解压压缩文件(RAR、ZIP、TAR、GZIP等)(二)
D7zTool是一个基于7-zip库开发的解压缩示例程序,除了应用程序本身,仅包含一个7z.dll 主要界面: D7zTool示例程序demo的pas源码 unit Unit1;interfaceus ...
- Linux:shell 脚本 自动解压压缩文件tar.gz到指定目录
具体情境 Ubuntu16.04系统,将.tar.gz格式的文件从/home/myftp/upload/nuodongiot目录自动解压到/home/myftp/upload/backupcopy目录 ...
- Linux 解压 压缩文件
1.*.tar 用 tar -xvf 解压 2.*.gz 用 gzip -d或者gunzip 解压 3.*.tar.gz和*.tgz 用 tar -xzf 解压 4.*.bz2 用 bzip2 -d或 ...
- python解压打开文件过多_自动解压大量压缩文件 Python 脚本 | 学步园
之前写了一个自动解压压缩文件到压缩文件所在文件夹的脚本 后根据自己需要,写了另外两个.原理一样 都是使用winrar的命令 第一个脚本没考虑周到,只能解压rar文件 改进后可以支持winrar支持的各 ...
- Zlib解压/压缩实现
Zlib解压/压缩实现 针对目前2503平台,请参考以下方式实现zlib解压/压缩文件: 1:将plutommi\Customer\ResGenerator\zlib整个文件夹copy到贵司的modu ...
最新文章
- VTK:Filtering之TriangulateTerrainMap
- 前端学习(1860)vue之电商管理系统电商系统之渲染login组件并且实现路由重定向
- matlab乘幂的指数是矩阵,信号与系统MATLAB基本语法.ppt
- Python批量修改Word文档中特定关键字的颜色
- php中file文件操作函数readfile fread fgets fgetc以及不需要加fopen的file_get_contents file_put_contents file()
- 【LeetCode】【字符串】题号:*520. 检测大写字母
- css层叠实例,css 层叠与z-index的示例代码
- 五子棋java毕业设计论文_Java五子棋毕业设计论文
- 忘记保护密码情况下卸载瑞星杀毒软件
- IDEA修改项目war包名称
- 安卓手机卡顿怎么解决_苹果手机卡怎么办 小技巧解决ipone手机卡顿现象
- 生物学哲学:科学哲学的新视野
- aws mysql 费用_AWS都收了哪些费用?
- 详解GloVe词向量模型
- 如何用【Python】制作一个二维码生成器
- 关于退火法的粗浅理解
- 在Windows系统上使用WSL和Docker
- [转载]从受欢迎角度分析哪些美国主流网站使用了哪些JS框架
- Servlet技术,response 生成图片验证码
- 1.8 监督学习应用
热门文章
- VTK:几何对象之Cube
- OpenGL pointsprites点精灵的实例
- C++ number of positive divisors计算正除数的实现算法(附完整源码)
- C语言实现二分法检索binary search(附完整源码)
- c++TCP的三次握手和四次挥手
- 第四天:规划范围管理
- 关于CKEditor4.5.6的使用,自定义toolbar配置,上传图片案例(SpringMVC+MyBatis案例),自定义行高,去编辑器的中内容,将编辑器中内容设置到指定的位置等
- Linux下编写选择排序(C语言)
- Cygwin,Nutch安装配置,检验是否正确(对网友守望者博客的修改---在此感谢守望者)3
- 在vue中怎么写行内样式高_说说在 Vue.js 中如何绑定样式(class 或 style)