C#文件或文件夹压缩和解压方法有很多,本文通过使用ICSharpCode.SharpZipLib.dll来进行压缩解压

1、新建一个winform项目,选择项目右键 管理NuGet程序包,搜索ICSharpCode.SharpZipLib,进行安装,

如下所示

2、项目winform窗体添加测试按钮 压缩和解压

3、具体代码如下

using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms; namespace WindowsFormsApp2._0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Button12_Click(object sender, EventArgs e)
{
ZipFile(@"F:\Person\Longteng\LongtengSln\WindowsFormsApp2.0\bin\Debug\devicelog", @"F:\Person\Longteng\LongtengSln\WindowsFormsApp2.0\bin\Debug\devicelog.zip");
MessageBox.Show("压缩 成功");
} private void Button13_Click(object sender, EventArgs e)
{
UnZipFile(@"F:\Person\Longteng\LongtengSln\WindowsFormsApp2.0\bin\Debug\devicelog.zip", @"F:\Person\Longteng\LongtengSln\WindowsFormsApp2.0\bin\Debug\devicelog11");
MessageBox.Show("解压 成功");
} public void ZipFile(string strFile, string strZip)
{
if (strFile[strFile.Length - ] != Path.DirectorySeparatorChar)
{
strFile += Path.DirectorySeparatorChar;
}
ZipOutputStream outstream = new ZipOutputStream(File.Create(strZip));
outstream.SetLevel();
ZipCompress(strFile, outstream, strFile);
outstream.Finish();
outstream.Close();
} public void ZipCompress(string strFile, ZipOutputStream outstream, string staticFile)
{
if (strFile[strFile.Length - ] != Path.DirectorySeparatorChar)
{
strFile += Path.DirectorySeparatorChar;
}
Crc32 crc = new Crc32();
//获取指定目录下所有文件和子目录文件名称
string[] filenames = Directory.GetFileSystemEntries(strFile);
//遍历文件
foreach (string file in filenames)
{
if (Directory.Exists(file))
{
ZipCompress(file, outstream, staticFile);
}
//否则,直接压缩文件
else
{
//打开文件
FileStream fs = File.OpenRead(file);
//定义缓存区对象
byte[] buffer = new byte[fs.Length];
//通过字符流,读取文件
fs.Read(buffer, , buffer.Length);
//得到目录下的文件(比如:D:\Debug1\test),test
string tempfile = file.Substring(staticFile.LastIndexOf("\\") + );
ZipEntry entry = new ZipEntry(tempfile);
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
outstream.PutNextEntry(entry);
//写文件
outstream.Write(buffer, , buffer.Length);
}
}
} public string UnZipFile(string TargetFile, string fileDir)
{
string rootFile = "";
try
{
if (!Directory.Exists(fileDir))
{
Directory.CreateDirectory(fileDir);
}
//读取压缩文件(zip文件),准备解压缩
ZipInputStream inputstream = new ZipInputStream(File.OpenRead(TargetFile.Trim()));
ZipEntry entry;
string path = fileDir;
//解压出来的文件保存路径
string rootDir = "";
//根目录下的第一个子文件夹的名称
while ((entry = inputstream.GetNextEntry()) != null)
{
rootDir = Path.GetDirectoryName(entry.Name);
//得到根目录下的第一级子文件夹的名称
if (rootDir.IndexOf("\\") >= )
{
rootDir = rootDir.Substring(, rootDir.IndexOf("\\") + );
}
string dir = Path.GetDirectoryName(entry.Name);
//得到根目录下的第一级子文件夹下的子文件夹名称
string fileName = Path.GetFileName(entry.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("\\") > )
{
//指定文件保存路径
path = fileDir + "\\" + dir;
}
}
if (dir == rootDir)
{
//判断是不是需要保存在根目录下的文件
path = fileDir + "\\" + rootDir;
} //以下为解压zip文件的基本步骤
//基本思路:遍历压缩文件里的所有文件,创建一个相同的文件
if (fileName != String.Empty)
{
FileStream fs = File.Create(path + "\\" + fileName);
int size = ;
byte[] data = new byte[];
while (true)
{
size = inputstream.Read(data, , data.Length);
if (size > )
{
fs.Write(data, , size);
}
else
{
break;
}
}
fs.Close();
}
}
inputstream.Close();
return rootFile;
}
catch (Exception ex)
{
return ex.Message;
}
} /// <summary>
/// 批量压缩事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnBatchZipFlo_Click(object sender, EventArgs e)
{
string path1 = "D:\\123\\"; //待压缩的目录文件
string path2 = "D:\\456\\"; //压缩后存放目录文件
//获取指定目录下所有文件和子文件名称(所有待压缩的文件)
string[] files = Directory.GetFileSystemEntries(path1);
//ZipFloClass zc = new ZipFloClass();
//遍历指定目录下文件路径
foreach (string file in files)
{
//截取文件路径的文件名
var filename = file.Substring(file.LastIndexOf("\\") + );
//调用压缩方法(参数1:待压缩的文件目录,参数2:压缩后的文件目录(包含后缀))
ZipFile(path1 + filename, path2 + filename + ".zip");
}
} /// <summary>
/// 批量解压事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnBatchUnZipFlo_Click(object sender, EventArgs e)
{
string msg = "";
string path2 = "D:\\123\\";
string path3 = "D:\\456\\";
//获取指定目录下所有文件和子文件名称(所有待解压的压缩文件)
string[] files = Directory.GetFileSystemEntries(path2);
//UnZipFloClass uzc = new UnZipFloClass();
//遍历所有压缩文件路径
foreach (string file in files)
{
//获取压缩包名称(包括后缀名)
var filename = file.Substring(file.LastIndexOf("\\") + );
//得到压缩包名称(没有后缀)
filename = filename.Substring(, filename.LastIndexOf("."));
//判断解压的路径是否存在
if (!Directory.Exists(path3 + filename))
{
//没有,则创建这个路径
Directory.CreateDirectory(path3 + filename);
}
//调用解压方法(参数1:待解压的压缩文件路径(带后缀名),参数2:解压后存放的文件路径,参数3:返工是否解压成功)
UnZipFile(file, path3 + filename);
}
MessageBox.Show("批量解压成功");
}
}
}

原文链接:https://www.cnblogs.com/xielong/p/6165550.html

C#文件或文件夹压缩和解压的更多相关文章

  1. 【C#公共帮助类】WinRarHelper帮助类,实现文件或文件夹压缩和解压,实战干货

    关于本文档的说明 本文档使用WinRAR方式来进行简单的压缩和解压动作,纯干货,实际项目这种压缩方式用的少一点,一般我会使用第三方的压缩dll来实现,就如同我上一个压缩类博客,压缩的是zip文件htt ...

  2. C#文件或文件夹压缩和解压方法(通过ICSharpCode.SharpZipLib.dll)

    我在网上收集一下文件的压缩和解压的方法,是通过ICSharpCode.SharpZipLib.dll 来实现的 一.介绍的目录 第一步:下载压缩和解压的 ICSharpCode.SharpZipLib ...

  3. .net文件压缩和解压及中文文件夹名称乱码问题

    /**************************注释区域内为引用http://www.cnblogs.com/zhaozhan/archive/2012/05/28/2520701.html的博 ...

  4. java 文件压缩和解压(ZipInputStream, ZipOutputStream)

    最近在看java se 的IO 部分 , 看到 java 的文件的压缩和解压比较有意思,主要用到了两个IO流-ZipInputStream, ZipOutputStream,不仅可以对文件进行压缩,还 ...

  5. Ionic.Zip.dll文件压缩和解压

    Ionic.Zip.dll文件压缩和解压 下载地址: http://download.csdn.net/detail/yfz19890410/5578515 1.下载Ionic.Zip.dll组件,添 ...

  6. 利用c#自带的类对文件进行压缩和解压处理

    在做网络传输文件的小例子的时候,当传输的文件比较大的时候,我们通常都是将文件经过压缩之后才进行传输,以前都是利用第三方插件来对文件进行压缩的,但是现在我发现了c#自带的类库也能够实现文件的压缩,实际上 ...

  7. java实现文件的压缩和解压

    java实现文件的压缩和解压 代码压缩实现 package com.hjh.demo.zip; import java.io.BufferedInputStream; import java.io.F ...

  8. C#压缩和解压文件

    这里用两种方法实现C#压缩和解压文件 1.使用System.IO.Compression名称空间下的相关类(需引用 System.IO.Compression.FileSystem和System.IO ...

  9. linux常用命令:4文件压缩和解压命令

    文件压缩和解压命令 压缩命令:gzip.tar[-czf].zip.bzip2 解压缩命令:gunzip.tar[-xzf].unzip.bunzip2 1. 命令名称:gzip 命令英文原意:GNU ...

随机推荐

  1. 限制mongoDB内存的方法

    docker运行MongoDB,针对于docker容器来进行内存资源的限制 修改MongoDB的运行配置文件,并且重启mongodb storage: dbPath: /var/lib/mongodb ...

  2. redis键过期 (redis 2.6及以上)

    EXPIRE key seconds 用来对一个键设置一个过期时间,第二个参数表示经过多少秒后键过期. 一个键过期后, 这个键将会被自动删除. 在Redis术语中,带有过期时间的键经常被称作volat ...

  3. Tomcat启动超时设置

    Tomcat启动超时设置: 处理方法: 1. 在server中找到当前Tomcat双击. 2.在视图中进行修改.(如下图:)

  4. Android自动化测试探索(二)常用自动化工具

    Android常用自动化工具 ADB - 是Google提供的为Android编写UI测试用例的自动化工具, Android开发/测试人员不可替代的强大工具 uiautomator - 是Google ...

  5. CPN tools 帮助文档资料和实例

    1.替代变迁 包含有替代变迁的页面叫做父页,当CPN网使用替代变迁的时候,替代变迁所表达的逻辑必须在某一个位置得到实现,实现替代变迁逻辑页面叫做子页或者子网. 将替代变迁相邻的库所叫做槽库所,也即是在 ...

  6. Metasploit3

    1.之前使用的版本是Metasploit2的版本操作系统是基于Ubuntu的,渗透测测试模块也是基于metasploit的,基本上没有设置,Metasploitable3添加了很多安全机制 ,防火墙和 ...

  7. jquery属性文档事件等操作

    1.jq方法attr removeAttr script标签大部分都是写在body标签上.下面的情况下$符号是拿不到的. 将它放到上面就能拿到$对象了.但是不能获取body里的元素.因为代码执行顺序从 ...

  8. Schema学习【一】

    XML Schema 是基于 XML 的 DTD 替代者. 什么是 XML Schema? XML Schema 的作用是定义 XML 文档的合法构建模块,类似 DTD. XML Schema: 定义 ...

  9. 用js刷剑指offer(丑数)

    题目描述 把只包含质因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含质因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 思路 ...

  10. 零基础如何学好Python 之int 数字整型类型 定义int()范围大小转换

    本文主题是讲python数字类型python int整型使用方法及技巧.它是不可变数据类型中的一种,它的一些性质和字符串是一样的,注意是整型不是整形哦. Python int有多种数字类型:整型int ...