我是一个菜鸟,我只是在努力,2021正视自己,面对2021!

ZIP 压缩文件的类库

复制代码
public static class ZipHelper
{
    /// <summary>
    /// 压缩一个文件
    /// </summary>
    /// <param name="srcPath">源文件全路径</param>
    /// <param name="destPath">压缩后文件全路径</param>
    public static void ZipFile(string srcPath, string destPath)
    {
        using (var zip = new ZipFile())
        {
            Ionic.Zip.ZipEntry e = zip.AddFile(srcPath, @"\");
            e.Comment = "Added by Cheeso's CreateZip utility.";
            zip.Comment = String.Format("This zip archive was created by the CreateZip example application on machine '{0}'",
                Dns.GetHostName());
            zip.Save(destPath);
        }
    }

    /// <summary>
    /// 压缩一个目录下的所有文件
    /// </summary>
    /// <param name="filePath">文件根目录</param>
    /// <param name="savePath">保存为zip包目录</param>
    public static void ZipDirFile(string filePath, string savePath)
    {
        using (ZipFile zip = new ZipFile())
        {
            String[] filenames = Directory.GetFiles(filePath);
            foreach (String filename in filenames)
            {
                Ionic.Zip.ZipEntry e = zip.AddFile(filename, @"\");
                e.Comment = "Added by Cheeso's CreateZip utility.";
            }
            zip.Comment = String.Format("This zip archive was created by the CreateZip example application on machine '{0}'",
                Dns.GetHostName());
            zip.Save(savePath);
        }
    }

    public static byte[] ZipFiles(string[] filesPath) 
    {
        using (ZipFile zip = new ZipFile())
        {
            foreach (String filename in filesPath)
            {
                Ionic.Zip.ZipEntry e = zip.AddFile(filename, @"\");
                //e.Comment = "Added by Cheeso's CreateZip utility.";
            }
            zip.Comment = String.Format("This zip archive was created by the application on machine '{0}'",
                Dns.GetHostName());
            using (MemoryStream stream = new MemoryStream())
            {
                zip.Save(stream);
                return stream.ToArray();
            }
        }
    }

    //压缩文件夹:文件夹下包含文件夹,filePath:压缩的文件路径,savePath:压缩后保存的文件路径
    public static void ZipDir(string filePath, string savePath)
    {
        using (ZipFile zip = new ZipFile())
        {
            zip.StatusMessageTextWriter = Console.Out;
            zip.AddDirectory(filePath, @"\");
            zip.Save(savePath);
        }
    }

    /// <summary>
    /// 解压缩,结果包含在文件夹内
    /// </summary>
    /// <param name="zipFilePath">zip包源路径</param>
    /// <param name="savePath">解压后的文件夹保存路径</param>
    public static void UnZipFile(string zipFilePath, string savePath)
    {
        try
        {
            using (var s = new ZipInputStream(File.OpenRead(zipFilePath)))
            {
                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    string directoryName = Path.GetDirectoryName(theEntry.Name);
                    string fileName = Path.GetFileName(theEntry.Name);
                    string serverFolder = savePath;

                    Directory.CreateDirectory(serverFolder + "/" + directoryName);

                    if (fileName != String.Empty)
                    {
                        using (FileStream streamWriter = File.Create((serverFolder + "/" + 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;
                                }
                            }
                            streamWriter.Close();
                        }
                    }
                }
                s.Close();
            }
        }
        catch (Exception ex) 
        {
            throw ex;
        }
    }


    /// <summary>
    /// 解压缩,结果不包含文件夹
    /// </summary>
    /// <param name="zipFilePath">zip包源路径</param>
    /// <param name="savePath">解压后的文件保存路径</param>
    public static void UnzipFileWithoutFolder(string zipFilePath, string savePath)
    {
        var s = new ZipInputStream(File.OpenRead(zipFilePath));
        ZipEntry theEntry;
        while ((theEntry = s.GetNextEntry()) != null)
        {
            string fileName = Path.GetFileName(theEntry.Name);

            if (fileName != String.Empty)
            {
                FileStream streamWriter = File.Create((savePath));
                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;
                    }
                }
                streamWriter.Close();
            }
        }
        s.Close();
    }

}
复制代码
posted @   Kencery  阅读(190)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
友情链接:初心商城
点击右上角即可分享
微信分享提示