马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

c#使用SharpZipLib压缩与解压文件

[复制链接]
查看3669 | 回复0 | 2017-7-31 17:52:52 | 显示全部楼层 |阅读模式

[C#] 纯文本查看 复制代码
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;

namespace Zhong.Core
{
    /// <summary>
    /// 压缩解压操作类,使用的是SharpZipLib
    /// </summary>
    public class DCompress
    {
        private static object operateLock = new object();
        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="srcFile">要压缩的文件路径</param>
        /// <param name="destFile">生成的压缩文件路径</param>
        public static void CompressFile(string srcFile, string destFile)
        {
            lock (operateLock)
            {
                if (string.IsNullOrEmpty(srcFile) || string.IsNullOrEmpty(destFile))
                {
                    throw new ArgumentException("参数错误");
                }
                FileStream fileStreamIn = new FileStream(srcFile, FileMode.Open, FileAccess.Read);
                FileStream fileStreamOut = new FileStream(destFile, FileMode.Create, FileAccess.Write);
                ZipOutputStream zipOutStream = new ZipOutputStream(fileStreamOut);
                //zipOutStream.SetLevel(6);   //设置压缩等级,默认为6
                byte[] buffer = new byte[4096];
                ZipEntry entry = new ZipEntry(Path.GetFileName(srcFile));
                zipOutStream.PutNextEntry(entry);
                int size;
                do
                {
                    size = fileStreamIn.Read(buffer, 0, buffer.Length);
                    zipOutStream.Write(buffer, 0, size);
                } while (size > 0);
                zipOutStream.Dispose();
                fileStreamOut.Dispose();
                fileStreamIn.Dispose();
            }

        }
        /// <summary>
        /// 压缩多个文件
        /// </summary>
        /// <param name="srcFiles">多个文件路径</param>
        /// <param name="destFile">压缩文件的路径</param>
        public static void ZipFiles(string[] srcFiles, string destFile)
        {
            if (srcFiles == null || string.IsNullOrEmpty(destFile))
            {
                throw new ArgumentException("参数错误");
            }
            using (ZipFile zip = ZipFile.Create(destFile))
            {
                zip.BeginUpdate();
                //add file
                foreach (string filePath in srcFiles)
                {
                    zip.Add(filePath);
                }
                zip.CommitUpdate();
            }
            //(new FastZip()).CreateZip(@"E:\1.zip", @"E:\1\", true, ""); 
        }
        /// <summary>
        /// 压缩目录
        /// </summary>
        /// <param name="dir">目录路径</param>
        /// <param name="destFile">压缩文件路径</param>
        public static void ZipDir(string dir, string destFile)
        {
            if (string.IsNullOrEmpty(dir) || string.IsNullOrEmpty(destFile))
            {
                throw new ArgumentException("参数错误");
            }
            string[] files = Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories);
            ZipFiles(files, destFile);
        }
        /// <summary>
        /// 列表压缩文件里的所有文件
        /// </summary>
        /// <param name="zipPath">压缩文件路径</param>
        /// <returns></returns>
        public static List<string> GetFileList(string zipPath)
        {
            List<string> files = new List<string>();
            if (string.IsNullOrEmpty(zipPath))
            {
                throw new ArgumentException("参数错误");
            }
            using (ZipFile zip = new ZipFile(zipPath))
            {
                string list = string.Empty;
                foreach (ZipEntry entry in zip)
                {
                    if (entry.IsFile)
                    {
                        files.Add(entry.Name);
                    }
                }
            }
            return files;
        }
        /// <summary>
        /// 删除zip文件中的某个文件
        /// </summary>
        /// <param name="zipPath">压缩文件路径</param>
        /// <param name="files">要删除的某个文件</param>
        public static void DeleteFileFromZip(string zipPath, string[] files)
        {
            if (string.IsNullOrEmpty(zipPath) || files == null)
            {
                throw new ArgumentException("参数错误");
            }
            using (ZipFile zip = new ZipFile(zipPath))
            {
                zip.BeginUpdate();
                foreach (string f in files)
                {
                    zip.Delete(f);
                }
                zip.CommitUpdate();
            }
        }
        /// <summary>
        /// 解压文件
        /// </summary>
        /// <param name="zipPath">要解压的文件</param>
        /// <param name="outputDir">解压后放置的目录</param>
        public static void UnZipFile(string zipPath, string outputDir)
        {
            (new FastZip()).ExtractZip(zipPath, outputDir, "");
        }

        /// <summary>
        /// 解压文件
        /// </summary>
        /// <param name="srcFile">压缩文件路径</param>
        /// <param name="destDir">解压后文件夹的路径</param>
        public static void Decompress(string srcFile, string destDir)
        {
            lock (operateLock)
            {
                FileStream fileStreamIn = new FileStream(srcFile, FileMode.Open, FileAccess.Read);
                ZipInputStream zipInStream = new ZipInputStream(fileStreamIn);
                if (!Directory.Exists(destDir))
                {
                    Directory.CreateDirectory(destDir);
                }
                ZipEntry entry;
                while ((entry = zipInStream.GetNextEntry()) != null)
                {
                    FileStream fileStreamOut = new FileStream(destDir + @"\" + entry.Name, FileMode.Create, FileAccess.Write);
                    int size;
                    byte[] buffer = new byte[4096];
                    do
                    {
                        size = zipInStream.Read(buffer, 0, buffer.Length);
                        fileStreamOut.Write(buffer, 0, size);
                    } while (size > 0);
                    fileStreamOut.Dispose();
                }
                zipInStream.Dispose();
                fileStreamIn.Dispose();
            }

        }
    }
}


C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则