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

namespace Test{
    /// <summary>
    /// 压缩
    /// </summary>
    public class Compress
    {
        /// <summary>
        /// zip路径
        /// </summary>
        public class ZipPath
        {
            /// <summary>
            /// 是否是文件
            /// </summary>
            public bool IsFile { get; set; }
            /// <summary>
            /// zip中路径
            /// </summary>
            public string Path { get; set; }
            /// <summary>
            /// 绝对路径
            /// </summary>
            public string FullPath{get;set;}
        }
        /// <summary>
        /// 递归获取文件目录
        /// 王洪岐 2011-11-22
        /// </summary>
        /// <param name="arrPath"></param>
        /// <param name="listPath"></param>
        /// <param name="strMainPath"></param>
        public static void GetFiles(string[] arrPath, List<ZipPath> listPath, string strMainPath)
        {
            foreach (string strN in arrPath)
            {
                List<string> fileList = Directory.GetFiles(strN).ToList();
                string[] arrDir=Directory.GetDirectories(strN);
                if (fileList.Count == 0 && arrDir.Length == 0) listPath.Add(new ZipPath { IsFile = false, Path = strN.Substring(strMainPath.Length + 1) + "\\", FullPath = strN });
                for (int i = 0; i < fileList.Count; i++)
                {
                    listPath.Add(new ZipPath { IsFile = true, Path = fileList[i].Substring(strMainPath.Length + 1), FullPath = fileList[i] });
                }
                GetFiles(arrDir, listPath, strMainPath);
            }
        }

/// <summary>
        /// zip递归压缩
        /// 王洪岐 2011-11-22
        /// 例:Compress.ZipCompress(new string[] { @"E:\TrainUpdate\ViewWeb\File\SrcSamples\doc", @"E:\素材\icon", @"E:\重要资料\C#\SharpZipLib\压缩sample.cs" }, Server.MapPath("~/File/a.zip"));
        /// </summary>
        /// <param name="path">源文件夹路径</param>
        /// <param name="topath">目标文件路径</param>
        /// <param name="FileName">从源文件夹中指定某文件</param>
        /// <returns>-1=文件不存在,0=未知错误,1=成功</returns>
        public static int ZipCompress(string[] arrPath, string topath)
        {

try
            {
                List<ZipPath> listPath = new List<ZipPath>();
                foreach (string strN in arrPath)
                {
                    string strMainPath = string.Empty;
                    //文件夹不存在
                    if (!Directory.Exists(strN) && !File.Exists(strN))
                    {
                        return -1;
                    }
                    if (string.IsNullOrEmpty(strMainPath))
                    {
                        strMainPath = Path.GetDirectoryName(strN);
                    }
                    //是文件
                    if (File.Exists(strN))
                    {
                        listPath.Add(new ZipPath { IsFile = true, Path = strN.Substring(strMainPath.Length + 1), FullPath = strN });
                    }
                    else
                    {
                        List<string> fileList = Directory.GetFiles(strN).ToList();
                        if (fileList.Count == 0) listPath.Add(new ZipPath { IsFile = false, Path = strN.Substring(strMainPath.Length + 1), FullPath = strN });
                        for (int i = 0; i < fileList.Count; i++)
                        {
                            listPath.Add(new ZipPath { IsFile = true, Path = fileList[i].Substring(strMainPath.Length + 1), FullPath = fileList[i] });
                        }
                        GetFiles(Directory.GetDirectories(strN), listPath, strMainPath);
                    }
                }
                using (ZipOutputStream s = new ZipOutputStream(File.Create(topath)))
                {
                    s.SetLevel(9); // 0 - store only to 9 - means best compression
                    byte[] buffer = new byte[4096];
                    foreach (ZipPath file in listPath)
                    {
                        if (file.IsFile)//是文件
                        {
                            ZipEntry entry = new ZipEntry(file.Path);
                            entry.DateTime = DateTime.Now;
                            s.PutNextEntry(entry);
                            using (FileStream fs = File.OpenRead(file.FullPath))
                            {
                                int sourceBytes;
                                do
                                {
                                    sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                    s.Write(buffer, 0, sourceBytes);
                                } while (sourceBytes > 0);
                            }
                        }
                        else
                        {
                            ZipEntry entry = new ZipEntry(file.Path);
                            s.PutNextEntry(entry);
                        }
                    }
                    s.Finish();
                    s.Close();
                }
                return 1;
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// 解压缩
        /// 王洪岐
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="topath"></param>
        /// <returns></returns>
        public static int ZipUnCompress(string filePath,string topath)
        {
            //文件不存在
            if (!File.Exists(filePath))
            {
                return -1;
            }
            try
            {
                using (ZipInputStream s = new ZipInputStream(File.OpenRead(filePath)))
                {

ZipEntry theEntry;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        string directoryName = Path.GetDirectoryName(theEntry.Name);
                        string fileName = Path.GetFileName(theEntry.Name);

// create directory
                        if (directoryName.Length > 0)
                        {
                            Directory.CreateDirectory(topath + directoryName);
                        }
                        else if (!Directory.Exists(topath))
                        {
                            Directory.CreateDirectory(topath);
                        }
                        if (fileName != String.Empty)
                        {
                            using (FileStream streamWriter = File.Create(topath + theEntry.Name))
                            {

int size = 2048;
                                byte[] data = new byte[2048];
                                while (true)
                                {
                                    size = s.Read(data, 0, data.Length);
                                    if (size > 0)
                                    {
                                        streamWriter.Write(data, 0, size);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                return 1;
            }
            catch
            {
                return 0;
            }
        }
    }
}

SharpZipLib压缩解压相关推荐

  1. ICSharpCode.SharpZipLib压缩解压

    一.使用ICSharpCode.SharpZipLib.dll: 下载地址 http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.asp ...

  2. 压缩/解压(ICSharpCode.SharpZipLib 类库)

    我在 AgileIM 的开发中解决视频/音频会话功能时,发现传输的音/视频数据量太大,通过一些格式转换(如BMP->JPG.或 帧间预测编码)可以适当减少带宽的需求,但是仍然不能满足需求,于是我 ...

  3. 使用C#压缩解压文件

    为了便于文件在网络中的传输和保存,通常将文件进行压缩操作,常用的压缩格式有rar.zip和7z,本文将介绍在C#中如何对这几种类型的文件进行压缩和解压,并提供一些在C#中解压缩文件的开源库. 在C#. ...

  4. python压缩文件tar_python 实现tar文件压缩解压的实例详解

    python 实现tar文件压缩解压的实例详解 python 实现tar文件压缩解压的实例详解 压缩文件: import tarfile import os def tar(fname): t = t ...

  5. 【分享】AspxZip v2.0 在线压缩解压ZIP文档

    下载地址: http://download.csdn.net/detail/rrrfff/5756977 当前版本:2.0.20140609 AspxZip v2.0 特点: 1.能够在支持 ASP. ...

  6. Asp.net 2.0 C#实现压缩/解压功能

    Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载) (一). 实现功能 对文件及目录的压缩及解压功能 (二). 运行图片示例 (三).代码 1. 压缩类   1/**//// <s ...

  7. 测试掌握的Linux解压,轻松掌握Linux压缩/解压文件的方法

    对于在Linux下解压大型的*.zip文件,相信大家一般都会通过使用winrar直接在smb中来进行解压的操作,虽然说最终可能能够解压但有时候会存在解压时间长或者网络原因出错等故障的情况出现.那么有没 ...

  8. tar压缩解压命令详解

    tar命令详解 -c:建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其 ...

  9. 一章: CentOS6.5 网络配置、修改主机名、添加硬盘、压缩——解压方法、VNC—server配置

    1,配IP ,修改网络配置文件 配置网卡 # vim /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 HWADDR=00:50:56:83: ...

最新文章

  1. springMVC项目在jboss7中配置应用自己的log4j--转载
  2. Oracle Supplemental 补全日志介绍
  3. CPP_封装_继承_多态
  4. c oracle日志分析,oracle 日志分析
  5. 【第2篇】Python爬虫实战-PPT模板素材下载
  6. 华为数据之道_DT|华为数据之道,怕你学不会,这次干脆出了本书
  7. 运用提示原则证明线性无关
  8. 浏览器是否支持Html5
  9. SSIS高级转换任务—导出列
  10. 网络通信实验(DM9000,LWIP TCP/IP)
  11. icom对讲机写频线定义_哈罗CQ火腿社区 - QRP and DIY - 各种写频线的资料,放上来备用吧 - Powered by phpwind...
  12. 基于PythonGUI的原神圣遗物游戏装备管理与角色数值模拟系统
  13. 量化敏捷项目管理案例分享
  14. 正则表达式匹配任意文本中的网络图片链接
  15. 打开word时显示microsoft visual basic运行时错误没有注册类怎么解决?
  16. centos7 修改时间、时区问题
  17. eventlet 协程
  18. 计算机视觉技术学习路线(3D视觉)
  19. linux mysql 邮件_linux中mysql 自动备份发邮件 到指定邮箱代码
  20. html滑动仿悬浮球菜单

热门文章

  1. 工作207:修改表头按钮样式
  2. 工作51:后端vue学习地址
  3. 前端学习(1514):vue-router使用步骤
  4. 前端学习(1407):多人管理27代码优化
  5. 前端学习(544):node的全局模块
  6. mybatis学习(16):不使用接口的方式
  7. MySQL常用语法记录
  8. ajax传输json数据格式乱码_解决Ajax加载JSon数据中文乱码问题
  9. js中自己实现bind函数的方式
  10. CSS之flex需要知道的一切(二)