代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
Zip方式压缩文件和解压缩文件代码
作者:
Dezai.CN
/ 发布于
2013/2/23
/
954
压缩和解压缩分为:单一文件压缩和整个文件夹压缩,其中文件夹压缩现在还没实现递归文件夹,现已够用测试通过
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.IO.Compression; using System.Collections; using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization; namespace Common { /// <summary> /// 压缩文件 /// </summary> public class Zip { #region 压缩解压单一文件 /// <summary> /// 压缩单一文件 /// </summary> /// <param name="sourceFile">源文件路径</param> /// <param name="destinationFile">目标文件路径</param> public static void CompressFile(string sourceFile, string destinationFile) { if (!File.Exists(sourceFile)) throw new FileNotFoundException(); using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read)) { byte[] buffer = new byte[sourceStream.Length]; int checkCounter = sourceStream.Read(buffer, 0, buffer.Length); if (checkCounter != buffer.Length) throw new ApplicationException(); using (FileStream destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write)) { using (GZipStream compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true)) { compressedStream.Write(buffer, 0, buffer.Length); } } } } public static void CompressFile(byte[] source, string destinationFile) { using (FileStream destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write)) { using (GZipStream compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true)) { compressedStream.Write(source, 0, source.Length); } } } /// <summary> /// 解压缩文件 /// </summary> /// <param name="sourceFile">源文件路径</param> /// <param name="destinationFile">目标文件路径</param> public static void DecompressFile(string sourceFile, string destinationFile) { if (!File.Exists(sourceFile)) throw new FileNotFoundException(); using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open)) { byte[] quartetBuffer = new byte[4]; int position = (int)sourceStream.Length - 4; sourceStream.Position = position; sourceStream.Read(quartetBuffer, 0, 4); sourceStream.Position = 0; int checkLength = BitConverter.ToInt32(quartetBuffer, 0); byte[] buffer = new byte[checkLength + 100]; using (GZipStream decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true)) { int total = 0; for (int offset = 0; ; ) { int bytesRead = decompressedStream.Read(buffer, offset, 100); if (bytesRead == 0) break; offset += bytesRead; total += bytesRead; } using (FileStream destinationStream = new FileStream(destinationFile, FileMode.Create)) { destinationStream.Write(buffer, 0, total); destinationStream.Flush(); } } } } #endregion #region 压缩解压文件夹 /**/ /// <summary> /// 对目标文件夹进行压缩,将压缩结果保存为指定文件 /// </summary> /// <param name="dirPath">目标文件夹</param> /// <param name="fileName">压缩文件</param> public static void CompressFolder(string dirPath, string fileName) { ArrayList list = new ArrayList(); foreach (string f in Directory.GetFiles(dirPath)) { byte[] destBuffer = File.ReadAllBytes(f); SerializeFileInfo sfi = new SerializeFileInfo(f, destBuffer); list.Add(sfi); } IFormatter formatter = new BinaryFormatter(); using (Stream s = new MemoryStream()) { formatter.Serialize(s, list); s.Position = 0; CreateCompressFile(s, fileName); } } /**/ /// <summary> /// 对目标压缩文件解压缩,将内容解压缩到指定文件夹 /// </summary> /// <param name="fileName">压缩文件</param> /// <param name="dirPath">解压缩目录</param> public static void DeCompressFolder(string fileName, string dirPath) { using (Stream source = File.OpenRead(fileName)) { using (Stream destination = new MemoryStream()) { using (GZipStream input = new GZipStream(source, CompressionMode.Decompress, true)) { byte[] bytes = new byte[4096]; int n; while ((n = input.Read(bytes, 0, bytes.Length)) != 0) { destination.Write(bytes, 0, n); } } destination.Flush(); destination.Position = 0; DeSerializeFiles(destination, dirPath); } } } private static void DeSerializeFiles(Stream s, string dirPath) { BinaryFormatter b = new BinaryFormatter(); ArrayList list = (ArrayList)b.Deserialize(s); foreach (SerializeFileInfo f in list) { string newName = dirPath + Path.GetFileName(f.FileName); using (FileStream fs = new FileStream(newName, FileMode.Create, FileAccess.Write)) { fs.Write(f.FileBuffer, 0, f.FileBuffer.Length); fs.Close(); } } } private static void CreateCompressFile(Stream source, string destinationName) { using (Stream destination = new FileStream(destinationName, FileMode.Create, FileAccess.Write)) { using (GZipStream output = new GZipStream(destination, CompressionMode.Compress)) { byte[] bytes = new byte[4096]; int n; while ((n = source.Read(bytes, 0, bytes.Length)) != 0) { output.Write(bytes, 0, n); } } } } #endregion } //测试 //private void btnZip_Click(object sender, EventArgs e) //{ // Common.Zip.CompressFile("database.mdb", "test.zip"); // Common.Zip.CompressFolder("sourcefolder", "sourcefolder.zip"); //} //private void btnUnZip_Click(object sender, EventArgs e) //{ // Common.Zip.DecompressFile("test.zip", "database2.mdb"); // Common.Zip.DeCompressFolder("sourcefolder.zip", "folder/"); //} [Serializable] class SerializeFileInfo { public SerializeFileInfo(string name, byte[] buffer) { fileName = name; fileBuffer = buffer; } string fileName; public string FileName { get { return fileName; } } byte[] fileBuffer; public byte[] FileBuffer { get { return fileBuffer; } } } }
试试其它关键字
压缩文件
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3