c#自带压缩类实现的多文件压缩和解压
用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#自带压缩类实现的多文件压缩和解压的更多相关文章
- linux常用命令:4文件压缩和解压命令
		
文件压缩和解压命令 压缩命令:gzip.tar[-czf].zip.bzip2 解压缩命令:gunzip.tar[-xzf].unzip.bunzip2 1. 命令名称:gzip 命令英文原意:GNU ...
 - Ionic.Zip.dll文件压缩和解压
		
Ionic.Zip.dll文件压缩和解压 下载地址: http://download.csdn.net/detail/yfz19890410/5578515 1.下载Ionic.Zip.dll组件,添 ...
 - .net文件压缩和解压及中文文件夹名称乱码问题
		
/**************************注释区域内为引用http://www.cnblogs.com/zhaozhan/archive/2012/05/28/2520701.html的博 ...
 - java 文件压缩和解压(ZipInputStream, ZipOutputStream)
		
最近在看java se 的IO 部分 , 看到 java 的文件的压缩和解压比较有意思,主要用到了两个IO流-ZipInputStream, ZipOutputStream,不仅可以对文件进行压缩,还 ...
 - 文件压缩和解压 FileStream GZipStream
		
using (FileStream reader=new FileStream (@"c:\1.txt",FileMode.Open,FileAccess.Read)) { usi ...
 - C# ICSharpCode.SharpZipLib.dll文件压缩和解压功能类整理,上传文件或下载文件很常用
		
工作中我们很多时候需要进行对文件进行压缩,比较通用的压缩的dll就是ICSharpCode.SharpZipLib.dll,废话不多了,网上也有很多的资料,我将其最常用的两个函数整理了一下,提供了一个 ...
 - C# ZipHelper C#公共类 -- ICSharpCode.SharpZipLib.dll实现压缩和解压
		
关于本文档的说明 本文档基于ICSharpCode.SharpZipLib.dll的封装,常用的解压和压缩方法都已经涵盖在内,都是经过项目实战积累下来的 1.基本介绍 由于项目中需要用到各种压缩将文件 ...
 - ZipArchive框架的文件压缩和解压
		
导入第三方框架ZipArchive之后还要在系统库文件中导入一个如下文件(搜索libz出来的任何一个都可以) 导入的头文件是#import "Main.h" 文件压缩 -(vo ...
 - 4_Linux_文件压缩和解压指令
		
3.4压缩解压命令.gz .tar.gz .zip .bz2 1)gzip 仅压缩文件 gzip命令用于压缩文件,英文原意为GNU zip,所在路径/bin/gzip,其语法格式为: gzip [文件 ...
 
随机推荐
- P1055 ISBN号码(getline(cin,s);  printf("%s",str); )
			
题目描述 每一本正式出版的图书都有一个 ISBN 号码与之对应,ISBN 码包括 9 位数字.1 位识别码和 3 位分隔符,其规定格式如 x-xxx-xxxxx-x,其中符号 - 就是分隔符(键盘上的 ...
 - Node.js核心模块-http
			
通过node中的http模块可以创建编写服务器 引入 const http = require('http') http举例使用: const http = require('http') //引入 ...
 - 以下几种情况转换成布尔类型会得到false
			
0 -0 '' NaN undefined null false document.all()
 - spring中JdbcTemplate使用
			
1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
 - 转:Flutter开发中踩过的坑
			
记录一下入手Flutter后实际开发中踩过的一些坑,这些坑希望后来者踩的越少越好.本文章默认读者已经掌握Flutter初步开发基础. 坑1问题:在debug模式下,App启动第一个页面会很慢,甚至是黑 ...
 - LeetCode 867. 转置矩阵
			
题目链接:https://leetcode-cn.com/problems/transpose-matrix/ 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵 ...
 - PHP0004:PHP基础3
			
php写在 标签里
 - Jquery实现挂号平台首页源码2
			
第二个版本:点击预约挂号可跳转到排班表,获取之后7个星期的排班 先放图 首先是index.html <!DOCTYPE html> <html lang="en" ...
 - wxPython学习笔记
			
------------恢复内容开始------------ 学习wxPython 资料 1.wxpython wiki Getting started with wxPython https://w ...
 - youhua