c#压缩解压帮助类

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using ICSharpCode.SharpZipLib.Zip;

namespace GzRMIS.Main.Business
{
    public class ZipHelper
    {
        /// <summary>
        /// 存放待压缩的文件的绝对路径
        /// </summary>
        private List<string> AbsolutePaths { set; get; }
        public string errorMsg { set; get; }

        public ZipHelper()
        {
            errorMsg = "";
            AbsolutePaths = new List<string>();
        }
        /// <summary>
        /// 添加压缩文件或文件夹
        /// </summary>
        /// <param name="_fileAbsolutePath">文件或文件夹的绝对路径</param>
        public void AddFile(string _fileAbsolutePath)
        {
            AbsolutePaths.Add(_fileAbsolutePath);
        }
        /// <summary>
        /// 压缩文件或者文件夹
        /// </summary>
        /// <param name="_depositPath">压缩后文件的存放路径   如C:\\windows\abc.zip</param>
        ///  <param name="_Level">压缩级别0~9,数字越大压缩率越高,默认为5</param>
        /// <returns></returns>
        public bool CompressionZip(string _depositPath,int _Level=5)
        {
            bool result = true;
            FileStream fs = null;
            try
            {
                ZipOutputStream ComStream = new ZipOutputStream(File.Create(_depositPath));
                ComStream.SetLevel(_Level);      //压缩等级
                foreach (string path in AbsolutePaths)
                {
                    //如果是目录
                    if (Directory.Exists(path))
                    {
                        ZipFloder(path, ComStream, path);
                    }
                    else if (File.Exists(path))//如果是文件
                    {
                        fs = File.OpenRead(path);
                        byte[] bts = new byte[fs.Length];
                        fs.Read(bts, 0, bts.Length);
                        ZipEntry ze = new ZipEntry(new FileInfo(path).Name);
                        ComStream.PutNextEntry(ze);             //为压缩文件流提供一个容器
                        ComStream.Write(bts, 0, bts.Length);  //写入字节
                    }
                }
                ComStream.Finish(); // 结束压缩
                ComStream.Close();
            }
            catch (Exception ex)
            {
                if (fs != null)
                {
                    fs.Close();
                }
                errorMsg = ex.Message;
                result = false;
            }
            return result;
        }
        //压缩文件夹
        private void ZipFloder(string _OfloderPath, ZipOutputStream zos, string _floderPath)
        {
            foreach (FileSystemInfo item in new DirectoryInfo(_floderPath).GetFileSystemInfos())
            {
                if (Directory.Exists(item.FullName))
                {
                    ZipFloder(_OfloderPath, zos, item.FullName);
                }
                else if (File.Exists(item.FullName))//如果是文件
                {
                    DirectoryInfo ODir = new DirectoryInfo(_OfloderPath);
                    string fullName2 = new FileInfo(item.FullName).FullName;
                    string path = ODir.Name + fullName2.Substring(ODir.FullName.Length, fullName2.Length - ODir.FullName.Length);//获取相对目录
                    FileStream fs = File.OpenRead(fullName2);
                    byte[] bts = new byte[fs.Length];
                    fs.Read(bts, 0, bts.Length);
                    ZipEntry ze = new ZipEntry(path);
                    zos.PutNextEntry(ze);             //为压缩文件流提供一个容器
                    zos.Write(bts, 0, bts.Length);  //写入字节
                }
            }
        }
        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="_depositPath">压缩文件路径</param>
        /// <param name="_floderPath">解压的路径</param>
        /// <returns></returns>
        public bool DeCompressionZip(string _depositPath, string _floderPath)
        {
            bool result = true;
            FileStream fs = null;
            try
            {
                ZipInputStream InpStream = new ZipInputStream(File.OpenRead(_depositPath));
                ZipEntry ze = InpStream.GetNextEntry();//获取压缩文件中的每一个文件
                Directory.CreateDirectory(_floderPath);//创建解压文件夹
                while (ze != null)//如果解压完ze则是null
                {
                    if (ze.IsFile)//压缩zipINputStream里面存的都是文件。带文件夹的文件名字是文件夹\\文件名
                    {
                        string[] strs = ze.Name.Split('\\');//如果文件名中包含’\\‘则表明有文件夹
                        if (strs.Length > 1)
                        {
                            //两层循环用于一层一层创建文件夹
                            for (int i = 0; i < strs.Length - 1; i++)
                            {
                                string floderPath = _floderPath;
                                for (int j = 0; j < i; j++)
                                {
                                    floderPath = floderPath + "\\" + strs[j];
                                }
                                floderPath = floderPath + "\\" + strs[i];
                                Directory.CreateDirectory(floderPath);
                            }
                        }
                        fs = new FileStream(_floderPath + "\\" + ze.Name, FileMode.OpenOrCreate, FileAccess.Write);//创建文件
                        //循环读取文件到文件流中
                        while (true)
                        {
                            byte[] bts = new byte[1024];
                            int i = InpStream.Read(bts, 0, bts.Length);
                            if (i > 0)
                            {
                                fs.Write(bts, 0, i);
                            }
                            else
                            {
                                fs.Flush();
                                fs.Close();
                                break;
                            }
                        }
                    }
                    ze = InpStream.GetNextEntry();
                }
            }
            catch (Exception ex)
            {
                if (fs != null)
                {
                    fs.Close();
                }
                errorMsg = ex.Message;
                result = false;
            }
            return result;
        }

    }
}
<a target=_blank href="http://icsharpcode.github.io/SharpZipLib/" target="_blank">ICSharpCode.SharpZipLib.Zip下载</a>

时间: 2024-08-27 06:50:31

c#压缩解压帮助类的相关文章

使用SharpZIpLib写的压缩解压操作类

使用SharpZIpLib写的压缩解压操作类,已测试. public class ZipHelper { /// <summary> /// 压缩文件 /// </summary> /// <param name="directory"></param> /// <param name="targetPath"></param> public static void Zip(string dir

[No0000DF]C# ZipFileHelper ZIP类型操作,压缩解压 ZIP 类封装

using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.IO.Compression; using System.Linq; using System.Text.RegularExpressions; using System.Windows.Forms; using Shared; namespace Helpers { public stat

PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载

PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载 http://my.oschina.net/junn/blog/104464 PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有疑问欢迎交流.这里整理一下常用的示例供参考. 一.解压缩zip文件 ? 1 2 3 4 5 6 7 8 9 10 11 $zip = new ZipAr

文件压缩、解压工具类。文件压缩格式为zip

package com.JUtils.file; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; impor

Lucene4.2源码解析之fdt和fdx文件的读写——fdx文件存储一个个的Block,每个Block管理着一批Chunk,通过docID读取到document需要完成Segment、Block、Chunk、document四级查询,引入了LZ4算法对fdt的chunk docs进行了实时压缩/解压

前言 通常在搜索打分完毕后,IndexSearcher会返回一个docID序列,但是仅仅有docID我们是无法看到存储在索引中的document,这时候就需要通过docID来得到完整Document信息,这个过程就需要对fdx/fdt文件进行读操作.为了更清楚地了解fdx/fdt文件的作用,本文把fdx/fdt文件的读和写整合到了一起,尽管这在Lucene中是两个分开的过程. 1. 索引生成阶段 索引生成阶段包含着一个复杂的过程,所以了解本文前最好对Lucene的索引架构有一定的了解,可以参考博

基于哈夫曼编码的压缩解压程序

这个程序是研一上学期的课程大作业.当时,跨专业的我只有一点 C 语言和数据结构基础,为此,我查阅了不少资料,再加上自己的思考和分析,实现后不断调试.测试和完善,耗时一周左右,在 2012/11/19 完成.虽然这是一个很小的程序,但却是我完成的第一个程序. 源码托管在 Github:点此打开链接 一.问题描述: 名称:基于哈夫曼编码的文件压缩解压 目的:利用哈夫曼编码压缩存储文件,节省空间 输入:任何格式的文件(压缩)或压缩文件(解压) 输出:压缩文件或解压后的原文件 功能:利用哈夫曼编码压缩解

C# GZipStream 压缩 解压

关于GZipStream压缩解压,网上找了很多资料,完整的不多,要么是针对字符串压缩解压缩的,要么只实现了针对单个文件的压缩解压缩,还有的不支持子文件夹的压缩,实用性都不是很大. 以下整理了压缩解压缩的代码,供以后拿出来翻阅,在项目中可以直接使用这3个类,已通过测试. 1.首先是有一个描述要压缩的文件类GZipFileInfo,包含了一些文件信息 /// <summary> /// 要压缩的文件信息 /// </summary> public class GZipFileInfo

WebAPI性能优化之压缩解压

有时候为了提升WebAPI的性能,减少响应时间,我们会使用压缩和解压,而现在大多数客户端浏览器都提供了内置的解压支持.在WebAPI请求的资源越大时,使用压缩对性能提升的效果越明显,而当请求的资源很小时则不需要使用压缩和解压,因为压缩和解压同样也是需要耗费一定的时间的. 看见老外写了一篇ASP.NET Web API GZip compression ActionFilter with 8 lines of code 说实话被这标题吸引了,8行代码实现GZip压缩过滤器,我就照着他的去实践了一番

Keka for Mac(OS平台压缩解压工具) V1.1.23(3418)汉化版

Keka Mac版是一款比较常用的压缩软件,这个解压缩软件基本可以满足mac os平台的日常解压缩需求,体积小,简单易用,速度较快.是一款好用的解压缩软件.基本上,它只有三项优势,就是免费,免费和免费.支持压缩:7z, Zip, Tar, Gzip, Bzip2.支持解压:RAR, 7z, Lzma, Zip, Tar, Gzip, Bzip2, ISO, EXE, CAB, PAX, ACE (PPC). 地址:Keka for Mac Keka for Mac安装教程 下载软件完成后,打开软