C#Zip方式压缩文件和解压缩文件代码
来源:互联网 发布:程序员标准形象 编辑:程序博客网 时间:2024/06/05 10:59
压缩和解压缩分为:单一文件压缩和整个文件夹压缩,其中文件夹压缩现在还没实现递归文件夹,现已够用测试通过,代码如下:
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; } } }}
- C#Zip方式压缩文件和解压缩文件代码
- Java压缩文件和解压缩文件
- Java用ZIP格式压缩和解压缩文件
- Linux中巧用zip命令压缩和解压缩文件
- zip 压缩文件
- zip压缩文件
- Delphi XE2 新增 System.Zip 单元,压缩和解压缩文件
- 【Java】IO操作之使用zip包压缩和解压缩文件
- IO操作之使用zip包压缩和解压缩文件
- 使用java.util.zip包压缩和解压缩文件
- Java ZIP压缩和解压缩文件(解决中文文件名乱码问题)
- 使用java.util.zip包压缩和解压缩文件
- Java操作zip压缩和解压缩文件工具类
- Java操作zip压缩和解压缩文件工具类
- 压缩和解压缩文件
- 压缩和解压缩文件
- Unity3D压缩和解压缩文件
- ZipArchive压缩和解压缩文件
- 过华为论-观华为二十年盛衰史有感
- Linux配置教程之五:apache安装配置
- 一个学习silverlight游戏的网站
- 黑马程序员--数组与集合
- Extjs+struts2 实现文件上传
- C#Zip方式压缩文件和解压缩文件代码
- 文件(3)NTFS文件格式解析完了
- sybase Sybase Central v6.0启动 弹出插件无法加载
- Linux系列之vim
- vi/vim 键盘图(中英文)及vim 快捷键壁纸
- node.js初接触
- 文件(3)NTFS文件格式解析完了
- 基于VC++实现APC注入
- ref和ou