当前位置: 首页 > news >正文

使用GZipStream类压缩和解压文件夹

因为要求的压缩和解压非常简单,只有一级目录,而且文件很小,就没有使用SharpZipLib而是自己用.Net 2.0中的GZipStream类写了个简单的。将保存每个文件内容的byte数组和文件名的一个类型的示例放入arraylist里,再对其序列化,压缩序列化的流并保存为压缩包。其实对于多级目录在压缩时对其文件进行递归并在解压时根据文件名称和路径重新构建文件目录就也可以实现了。

None.gif using  System;
None.gif
using  System.Text;
None.gif
using  System.Runtime.Serialization;
None.gif
using  System.Runtime.Serialization.Formatters.Binary;
None.gif
using  System.Collections;
None.gif
using  System.IO;
None.gif
using  System.IO.Compression;
None.gif
None.gif
namespace  GreatCHN.GZipCompression
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class GZipCompress
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 对目标文件夹进行压缩,将压缩结果保存为指定文件
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="dirPath">目标文件夹</param>
ExpandedSubBlockEnd.gif        
/// <param name="fileName">压缩文件</param>

InBlock.gif        public static void Compress(string dirPath, string fileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ArrayList list 
= new ArrayList();
InBlock.gif            
foreach (string f in Directory.GetFiles(dirPath))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
byte[] destBuffer = File.ReadAllBytes(f);
InBlock.gif                SerializeFileInfo sfi 
= new SerializeFileInfo(f, destBuffer);
InBlock.gif                list.Add(sfi);
ExpandedSubBlockEnd.gif            }

InBlock.gif            IFormatter formatter 
= new BinaryFormatter();
InBlock.gif            
using (Stream s = new MemoryStream())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                formatter.Serialize(s, list);
InBlock.gif                s.Position 
= 0;
InBlock.gif                CreateCompressFile(s, fileName);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 对目标压缩文件解压缩,将内容解压缩到指定文件夹
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="fileName">压缩文件</param>
ExpandedSubBlockEnd.gif        
/// <param name="dirPath">解压缩目录</param>

InBlock.gif        public static void DeCompress(string fileName, string dirPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
using (Stream source = File.OpenRead(fileName))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
using (Stream destination = new MemoryStream())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
using (GZipStream input = new GZipStream(source, CompressionMode.Decompress, true))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
byte[] bytes = new byte[4096];
InBlock.gif                        
int n;
InBlock.gif                        
while ((n = input.Read(bytes, 0, bytes.Length)) != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            destination.Write(bytes, 
0, n);
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    destination.Flush();
InBlock.gif                    destination.Position 
= 0;
InBlock.gif                    DeSerializeFiles(destination, dirPath);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static void DeSerializeFiles(Stream s, string dirPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            BinaryFormatter b 
= new BinaryFormatter();
InBlock.gif            ArrayList list 
= (ArrayList)b.Deserialize(s);
InBlock.gif
InBlock.gif            
foreach (SerializeFileInfo f in list)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string newName = dirPath + Path.GetFileName(f.FileName);
InBlock.gif                
using (FileStream fs = new FileStream(newName, FileMode.Create, FileAccess.Write))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    fs.Write(f.FileBuffer, 
0, f.FileBuffer.Length);
InBlock.gif                    fs.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static void CreateCompressFile(Stream source, string destinationName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
using (Stream destination = new FileStream(destinationName, FileMode.Create, FileAccess.Write))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
using (GZipStream output = new GZipStream(destination, CompressionMode.Compress))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
byte[] bytes = new byte[4096];
InBlock.gif                    
int n;
InBlock.gif                    
while ((n = source.Read(bytes, 0, bytes.Length)) != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        output.Write(bytes, 
0, n);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Serializable]
InBlock.gif        
class SerializeFileInfo
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public SerializeFileInfo(string name, byte[] buffer)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                fileName 
= name;
InBlock.gif                fileBuffer 
= buffer;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
string fileName;
InBlock.gif            
public string FileName
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return fileName;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
byte[] fileBuffer;
InBlock.gif            
public byte[] FileBuffer
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return fileBuffer;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

      在System.IO.Compress命名空间下还有一个DeflateStream类,它使用Deflate 算法来进行压缩和解压,它和GZipStream的工作方式是一样的。

相关文章:

  • mysql sql语句中用括号处理or和and的运算顺序
  • c#中WebBrowser控件使用心得
  • [转载]编写超级可读代码的15个最佳实践
  • 定时器NSTimer
  • 查看系统默认路径
  • 递归法求数组中的最大数
  • codeforces 190A Vasya and the Bus
  • nodejs一周动态(2011-04-25 - 05-01)
  • Cocos2d-x之物理世界(物体之间的碰撞)
  • 在CentOS安装配置OpenMeetings。
  • 随手记
  • 服务器报E0D76错误代码
  • MongoDB 安装 Windows XP
  • 浅谈定制化OA突破传统OA瓶颈
  • 51CTO下载-HP+BL20p刀片服务器安装图解
  • [数据结构]链表的实现在PHP中
  • 【翻译】Mashape是如何管理15000个API和微服务的(三)
  • 【技术性】Search知识
  • 08.Android之View事件问题
  • Android开发 - 掌握ConstraintLayout(四)创建基本约束
  • dva中组件的懒加载
  • laravel 用artisan创建自己的模板
  • MySQL常见的两种存储引擎:MyISAM与InnoDB的爱恨情仇
  • MYSQL如何对数据进行自动化升级--以如果某数据表存在并且某字段不存在时则执行更新操作为例...
  • nodejs调试方法
  • php面试题 汇集2
  • Three.js 再探 - 写一个跳一跳极简版游戏
  • 纯 javascript 半自动式下滑一定高度,导航栏固定
  • 复习Javascript专题(四):js中的深浅拷贝
  • 关于List、List?、ListObject的区别
  • 后端_MYSQL
  • 基于HAProxy的高性能缓存服务器nuster
  • 前端之Sass/Scss实战笔记
  • 如何用Ubuntu和Xen来设置Kubernetes?
  • 算法---两个栈实现一个队列
  • 想写好前端,先练好内功
  • 在Docker Swarm上部署Apache Storm:第1部分
  • 怎么把视频里的音乐提取出来
  • AI算硅基生命吗,为什么?
  • 直播平台建设千万不要忘记流媒体服务器的存在 ...
  • #LLM入门|Prompt#2.3_对查询任务进行分类|意图分析_Classification
  • #周末课堂# 【Linux + JVM + Mysql高级性能优化班】(火热报名中~~~)
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • (09)Hive——CTE 公共表达式
  • (cljs/run-at (JSVM. :browser) 搭建刚好可用的开发环境!)
  • (大众金融)SQL server面试题(1)-总销售量最少的3个型号的车及其总销售量
  • (附源码)ssm高校升本考试管理系统 毕业设计 201631
  • (七)理解angular中的module和injector,即依赖注入
  • (转)大道至简,职场上做人做事做管理
  • .bat批处理(十):从路径字符串中截取盘符、文件名、后缀名等信息
  • .NET Core 中的路径问题
  • .NET/C# 使用 SpanT 为字符串处理提升性能
  • .NET是什么
  • .net用HTML开发怎么调试,如何使用ASP.NET MVC在调试中查看控制器生成的html?
  • []FET-430SIM508 研究日志 11.3.31