用c#自带的System.IO.Compression命名空间下的压缩类实现的多文件压缩和解压功能,缺点是多文件压缩包的解压只能调用自身的解压方法,和现有的压缩软件不兼容。下面的代码没有把多文件的目录结构加进去,有需要的可以自己改下。

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression; namespace Test.Zip
{
class CompressHelper
{
/// <summary>
/// 单文件压缩(生成的压缩包和第三方的解压软件兼容)
/// </summary>
/// <param name="sourceFilePath"></param>
/// <returns></returns>
public string CompressSingle(string sourceFilePath)
{
string zipFileName = sourceFilePath + ".gz";
using (FileStream sourceFileStream = new FileInfo(sourceFilePath).OpenRead())
{
using (FileStream zipFileStream = File.Create(zipFileName))
{
using (GZipStream zipStream = new GZipStream(zipFileStream, CompressionMode.Compress))
{
sourceFileStream.CopyTo(zipStream);
}
}
}
return zipFileName;
}
/// <summary>
/// 自定义多文件压缩(生成的压缩包和第三方的压缩文件解压不兼容)
/// </summary>
/// <param name="sourceFileList">文件列表</param>
/// <param name="saveFullPath">压缩包全路径</param>
public void CompressMulti(string[] sourceFileList, string saveFullPath)
{
MemoryStream ms = new MemoryStream();
foreach (string filePath in sourceFileList)
{
Console.WriteLine(filePath);
if (File.Exists(filePath))
{
string fileName = Path.GetFileName(filePath);
byte[] fileNameBytes = System.Text.Encoding.UTF8.GetBytes(fileName);
byte[] sizeBytes = BitConverter.GetBytes(fileNameBytes.Length);
ms.Write(sizeBytes, , sizeBytes.Length);
ms.Write(fileNameBytes, , fileNameBytes.Length);
byte[] fileContentBytes = System.IO.File.ReadAllBytes(filePath);
ms.Write(BitConverter.GetBytes(fileContentBytes.Length), , );
ms.Write(fileContentBytes, , fileContentBytes.Length);
}
}
ms.Flush();
ms.Position = ;
using (FileStream zipFileStream = File.Create(saveFullPath))
{
using (GZipStream zipStream = new GZipStream(zipFileStream, CompressionMode.Compress))
{
ms.Position = ;
ms.CopyTo(zipStream);
}
}
ms.Close();
} /// <summary>
/// 多文件压缩解压
/// </summary>
/// <param name="zipPath">压缩文件路径</param>
/// <param name="targetPath">解压目录</param>
public void DeCompressMulti(string zipPath, string targetPath)
{
byte[] fileSize = new byte[];
if (File.Exists(zipPath))
{
using (FileStream fStream = File.Open(zipPath, FileMode.Open))
{
using (MemoryStream ms = new MemoryStream())
{
using (GZipStream zipStream = new GZipStream(fStream, CompressionMode.Decompress))
{
zipStream.CopyTo(ms);
}
ms.Position = ;
while (ms.Position != ms.Length)
{
ms.Read(fileSize, , fileSize.Length);
int fileNameLength = BitConverter.ToInt32(fileSize, );
byte[] fileNameBytes = new byte[fileNameLength];
ms.Read(fileNameBytes, , fileNameBytes.Length);
string fileName = System.Text.Encoding.UTF8.GetString(fileNameBytes);
string fileFulleName = targetPath + fileName;
ms.Read(fileSize, , );
int fileContentLength = BitConverter.ToInt32(fileSize, );
byte[] fileContentBytes = new byte[fileContentLength];
ms.Read(fileContentBytes, , fileContentBytes.Length);
using (FileStream childFileStream = File.Create(fileFulleName))
{
childFileStream.Write(fileContentBytes, , fileContentBytes.Length);
}
}
}
}
}
}
}
}

调用示例:

 List<string> strList = new List<string>() { @"D:\文档\soapUI工程\Synchro-soapui-project.xml", @"D:\文档\soapUI工程\PKBSML-soapui-project.xml", @"D:\文档\soapUI工程\PKBSML-soapui-project.xml" };
var zipHelper = new Test.Zip.CompressHelper();
zipHelper.CompressMulti(strList.ToArray(), @"D:\wulala.gz");
zipHelper.DeCompressMulti(@"D:\wulala.gz", @"D:\web\");

c#自带压缩类实现的多文件压缩和解压的更多相关文章

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

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

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

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

  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. 文件压缩和解压 FileStream GZipStream

    using (FileStream reader=new FileStream (@"c:\1.txt",FileMode.Open,FileAccess.Read)) { usi ...

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

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

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

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

  8. ZipArchive框架的文件压缩和解压

    导入第三方框架ZipArchive之后还要在系统库文件中导入一个如下文件(搜索libz出来的任何一个都可以)   导入的头文件是#import "Main.h" 文件压缩 -(vo ...

  9. 4_Linux_文件压缩和解压指令

    3.4压缩解压命令.gz .tar.gz .zip .bz2 1)gzip 仅压缩文件 gzip命令用于压缩文件,英文原意为GNU zip,所在路径/bin/gzip,其语法格式为: gzip [文件 ...

随机推荐

  1. TotalUninstall6破解步骤卸载软件更彻底更专业

    软件包分享,这里使用的是6.24版本 链接:https://pan.baidu.com/s/1aVd6bclk5A6puyWBfAOA2A提取码:mhl9 接下来开始安装 安装步骤: 点击安装.基本上 ...

  2. 报表生成(POI,jquery.table2excel.js,Echarts)

    最近公司要弄个报表相关的功能,话不多说,先上图 前一种是POI 生成的,后一种是Echarts生成的.报表我想大家都不陌生,基本上在公司业务中都会使用到.先说说POI,jquery.table2exc ...

  3. 位运算基础知识及简单例题(待补全Hamilton)

    位运算 +++ 1 : 0000000000...01 2 : 0000000000...10 3 : 0000000000...11 补码 1 + x = 0000000000...00 1 + 1 ...

  4. 输出redis cluster集群所有节点指定的参数的配置

    需要:实现类似redis-trib.rb call 命令的功能,输出redis cluster集群所有节点指定的参数的配置 redis-trib.rb的输出 [redis@lxd-vm3 ~]$ re ...

  5. 数据库中间件DBLE学习(一) 基础介绍和快速搭建

    dble基本架构简介 dble是上海爱可生信息技术股份有限公司基于mysql的高可用扩展性的分布式中间件.江湖人送外号MyCat Plus.开源地址 我们首先来看架构图,外部应用通过NIO/AIO进行 ...

  6. LOJ #6402. yww 与校门外的树 多项式求逆

    蛮神的一道题. code: #include <cmath> #include <cstring> #include <algorithm> #include &l ...

  7. rownum按某字段排序查询

    特点:rownum伪列,查询结果按顺序从1递增排列 用途:按某字段排序查询第几名到第几名的数据 但加上按字段排序条件后,rownum并不会从1递增 需把按字段排序查询的数据作为一张表,再次查询,row ...

  8. LeetCode 160. 相交链表 (找出两个链表的公共结点)

    题目链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists/ 编写一个程序,找到两个单链表相交的起始节点. 如下面的两 ...

  9. 【H5适配 笔记1】rem适配

    设备像素比(dpr)= 物理像素(手机渲染像素.分辨率)/设备独立像素(手机所显示元素的大小) 视口(viewport) 布局视口(获取浏览器布局视口高度,包括内边距,但不包括垂直滚动条.边框和外边距 ...

  10. Sass和Scss

    Sass:https://www.sass.hk/ Sass是什么 Sass 是一款强化 CSS 的辅助工具,它在 CSS 语法的基础上增加了变量 (variables).嵌套 (nested rul ...