用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. 自定义配置JNOJ

    OJ 全名 online judge 在线判题系统,对于从事编程竞赛的人来说一点都不陌生,今天我们讨论的是怎么样自定义搭建 推荐的开源的OJ有hustOJ,JNOJ 因为hustOJ 是一键安装脚本, ...

  2. vsftp安装及配置

    一.安装Vsftp 1,安装 yum install vsftpd -y

  3. 开学第一课Java考试

    package moguiba;import java.text.DecimalFormat;import java.util.Scanner;import moguiba.ScoreInformat ...

  4. webkit 技术内幕 笔记 三

    浏览器内核及特性 在浏览器中,一个很重要的模块,是将页面转变成可视化的图像结果,这就是浏览器的内核,通常被称作渲染引擎.渲染:就是根据描述或者定义构建数学模型,通过模型生成图像的过程.浏览器的渲染引擎 ...

  5. 心理学实验程序编程(python)

    任务一:实现简单的屏幕的颜色之间的切换 import pygame from pygame.locals import * pygame.init() win = pygame.display.set ...

  6. word的一些运用技巧

    1.去除换行符 替换:^p > 空格

  7. git pull报错you do not have permission to pull from the repository

    you do not have permission to pull from the repository解决方法   使用git进行项目的版本管理,换了台电脑,配置了账号和邮箱后,pull一个项目 ...

  8. 字符串积累ing

    明天就要上网课拉拉啦啦! 数据库先在手机端登录然后转战客户端试之! 操作系统在客户端登录试一试! 马原用学习通试试啦! 首先,介绍一下strlen,strcpy,strcmp函数! 参考:https: ...

  9. 小白月赛22 G : 仓库地址

    G : 仓库地址 考察点: 二维中位数 坑点 : 做就 OK 析题得侃: 我们发现 x 和 y 是相互独立的,也就是说先移动 x 或者先移动 y 都是可以到达一个点的,所以我们可以先找到 横坐标的 中 ...

  10. 为什么在linux系统下安装anaconda的时候会报错

    报错界面 一开始是在官网下载的最新的包,出现了上述的报错,但是换成清华镜像之后,就没有上述报错了? 我猜测可能是因为 官网最新的版本的anaconda和你安装的python版本不兼容,而在镜像上的不是 ...