• 使用SharpZipLib压缩与解压文件


    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();
                }
    
            }
        }
    }
  • 相关阅读:
    根据地球上两个坐标点,计算出距离
    判断一个日期距离今天是不是过了指定的天数
    二维码
    修改、删除 触发器
    sql server 数据加密
    C#下载apk文件
    java 判断一个字符串是否包含某个字符串中的字符
    CenterOS 设置静态IP
    VMware 克隆 CenterOS 虚拟机
    VMware 安装CenterOS
  • 原文地址:https://www.cnblogs.com/godbell/p/7190538.html
Copyright © 2020-2023  润新知