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. MYSQL 存储引擎概述

    一.存储引擎 Mysql中的数据用各种不同的技术存储在文件(或者内存)中.这些技术中每一种技术都使用了不同的存储机制,索引技巧.锁定水平并且最终提供广泛的不同功能和能力.通过选择不同的技术,你能够获得 ...

  2. grpc的简单用例 (C++实现)

    这个用例的逻辑很简单, 服务器运行一个管理个人信息的服务, 提供如下的四个服务: (1) 添加一个个人信息 注: 对应于Unary RPCs, 客户端发送单一消息给服务器, 服务器返回单一消息 (2) ...

  3. django--模型字段引用

    如果内置字段不起作用,您可以尝试使用django-localflavor(文档),其中包含对特定国家和文化有用的各种代码片段. 此外,您可以轻松编写自己的自定义模型字段. 注意 从技术上讲,这些模型是 ...

  4. WampServer详解

    php能做什么,它是运行在服务器端的,web网站大部分数据都是存储在服务器上的,PHP就是用来处理这些存储在服务器的数据.跨平台,服务器可以是多种平台上的服务器,脚本语言,免费. wampserver ...

  5. [daily] 使用thunderbird通过davmail代理访问Microsoft Exchange Service(OWA)

    前言 我需要接入某企业的邮件服务器, 该服务器没有开通pop3, 没有smtp, 没有imap, 只有exchange. 也就是说必须要使用outlook才能访问. 但是我没有outlook. 方案一 ...

  6. 怎么查看二进制文件内容?linux下nm命令告诉你!

    linux下强大的文件分析工具 -- nm 什么是nm nm命令是linux下自带的特定文件分析工具,一般用来检查分析二进制文件.库文件.可执行文件中的符号表,返回二进制文件中各段的信息. 目标文件. ...

  7. git rebase 版本。。变基

    git rebase,顾名思义,就是重新定义(re)起点(base)的作用,即重新定义分支的版本库状态.要搞清楚这个东西,要先看看版本库状态切换的两种情况: 我们知道,在某个分支上,我们可以通过git ...

  8. linux 非root用户安装nginx

    第一步:首先下载依赖包 下载地址 pcre(www.pcre.org),zlib(www.zlib.org),openssl(www.openssl.org) 第二步:上传那个nginx的安装包 下载 ...

  9. HDU-2204-Eddy's爱好-容斥求n以内有多少个数形如M^K

    HDU-2204-Eddy's爱好-容斥求n以内有多少个数形如M^K [Problem Description] 略 [Solution] 对于一个指数\(k\),找到一个最大的\(m\)使得\(m^ ...

  10. django-ContentType的简单使用

    ContentType 一般我们有多张表同时外键关联同一张表的时候,可以考虑使用ContentType models.py from django.db import models from djan ...