zoukankan      html  css  js  c++  java
  • ziphelper

    using ICSharpCode.SharpZipLib.Checksums;
    using ICSharpCode.SharpZipLib.Zip;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text; 
    
    namespace MacCheck
    {
        /// <summary>   
        /// 适用与ZIP压缩   
        /// </summary>   
        public static class ZipHelper
        {
            /// <summary>   
            /// 递归压缩文件夹的内部方法   
            /// </summary>   
            /// <param name="folderToZip">要压缩的文件夹路径</param>   
            /// <param name="zipStream">压缩输出流</param>   
            /// <param name="parentFolderName">此文件夹的上级文件夹</param>   
            /// <returns></returns>   
            private static bool ZipDirectory(string folderToZip, ZipOutputStream zipStream, string parentFolderName)
            {
                bool result = false;
                if (Directory.Exists(folderToZip))
                {
                    try
                    {
                        string FolderName = Path.GetFileName(folderToZip);
                        ZipEntry theEntry = new ZipEntry(Path.Combine(parentFolderName, FolderName + "/"));
                        zipStream.PutNextEntry(theEntry);
                        zipStream.Flush();
                        foreach (string file in Directory.GetFiles(folderToZip))
                        {
                            using (FileStream fileStream = File.OpenRead(file))
                            {
                                theEntry = new ZipEntry(Path.Combine(parentFolderName, FolderName + "/" + Path.GetFileName(file)));
                                theEntry.DateTime = DateTime.Now;
                                theEntry.Size = fileStream.Length;
                                byte[] buffer = new byte[fileStream.Length];
                                fileStream.Read(buffer, 0, buffer.Length);
                                Crc32 crc = new Crc32();
                                crc.Update(buffer);
                                theEntry.Crc = crc.Value;
                                zipStream.PutNextEntry(theEntry);
                                zipStream.Write(buffer, 0, buffer.Length);
                            }
                        }
                        result = true;
                        foreach (string folder in Directory.GetDirectories(folderToZip))
                        {
                            if (!ZipDirectory(folder, zipStream, FolderName))
                            {
                                return false;
                            }
                        }
                    }
                    catch
                    {
                        result = false;
                    }
                }
                return result;
            }
            /// <summary>   
            /// 压缩文件夹    
            /// </summary>   
            /// <param name="folderToZip">要压缩的文件夹路径</param>   
            /// <param name="zipedFile">压缩文件完整路径</param>   
            /// <param name="password">密码</param>   
            /// <returns>是否压缩成功</returns>   
            public static bool ZipDirectory(string folderToZip, string zipedFile, string password)
            {
                bool result = false;
                if (Directory.Exists(folderToZip))
                {
                    using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedFile)))
                    {
                        if (!string.IsNullOrEmpty(password))
                        {
                            zipStream.Password = password;
                        }
                        zipStream.SetLevel(6);
                        result = ZipDirectory(folderToZip, zipStream, "");
                    }
                }
                return result;
            }
            /// <summary>   
            /// 压缩文件夹   
            /// </summary>   
            /// <param name="folderToZip">要压缩的文件夹路径</param>   
            /// <param name="zipedFile">压缩文件完整路径</param>   
            /// <returns>是否压缩成功</returns>   
            public static bool ZipDirectory(string folderToZip, string zipedFile)
            {
                return ZipDirectory(folderToZip, zipedFile, string.Empty);
            }
            /// <summary>   
            /// 压缩文件   
            /// </summary>   
            /// <param name="fileToZip">要压缩的文件全名</param>   
            /// <param name="zipedFile">压缩后的文件名</param>   
            /// <param name="password">密码</param>   
            /// <returns>压缩结果</returns>   
            public static bool ZipFile(string fileToZip, string zipedFile, string password)
            {
                bool successed = false;
                if (File.Exists(fileToZip))
                {
                    try
                    {
                        using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedFile)))
                        {
                            if (!string.IsNullOrEmpty(password))
                            {
                                zipStream.Password = password;
                            }
                            zipStream.SetLevel(6);
                            ZipEntry theEntry = new ZipEntry(Path.GetFileName(fileToZip));
                            zipStream.PutNextEntry(theEntry);
                            using (FileStream fileStream = File.OpenRead(fileToZip))
                            {
                                int size = 2048;
                                byte[] data = new byte[size];
                                while ((size = fileStream.Read(data, 0, data.Length)) > 0)
                                {
                                    zipStream.Write(data, 0, size);
                                }
                            }
                        }
                        successed = true;
                    }
                    catch
                    {
                        successed = false;
                    }
                }
                return successed;
            }
            /// <summary>   
            /// 压缩文件   
            /// </summary>   
            /// <param name="fileToZip">要压缩的文件全名</param>   
            /// <param name="zipedFile">压缩后的文件名</param>   
            /// <returns>压缩结果</returns>   
            public static bool ZipFile(string fileToZip, string zipedFile)
            {
                return ZipFile(fileToZip, zipedFile, string.Empty);
            }
            /// <summary>   
            /// 压缩文件或文件夹   
            /// </summary>   
            /// <param name="fileToZip">要压缩的路径</param>   
            /// <param name="zipedFile">压缩后的文件名</param>   
            /// <param name="password">密码</param>   
            /// <returns>压缩结果</returns>   
            public static bool Zip(string fileToZip, string zipedFile, string password)
            {
                bool result = false;
                if (Directory.Exists(fileToZip))
                    result = ZipDirectory(fileToZip, zipedFile, password);
                else if (File.Exists(fileToZip))
                    result = ZipFile(fileToZip, zipedFile, password);
                return result;
            }
            /// <summary>   
            /// 压缩文件或文件夹   
            /// </summary>   
            /// <param name="fileToZip">要压缩的路径</param>   
            /// <param name="zipedFile">压缩后的文件名</param>   
            /// <returns>压缩结果</returns>   
            public static bool Zip(string fileToZip, string zipedFile)
            {
                return Zip(fileToZip, zipedFile, string.Empty);
            }
            /// <summary>   
            /// 解压功能(解压压缩文件到指定目录)   
            /// </summary>   
            /// <param name="zipFilePath">待解压的文件</param>   
            /// <param name="zipedFolder">指定解压目标目录</param>   
            /// <param name="password">密码</param>   
            /// <returns>解压结果</returns>   
            public static bool UnZip(string zipFilePath, string zipedFolder, string password)
            {
                bool successed = false;
                if (File.Exists(zipFilePath))
                {
                    try
                    { 
                        using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipFilePath)))
                        {
                            if (!string.IsNullOrEmpty(password))
                            {
                                zipStream.Password = password;
                            }
                            ZipEntry theEntry;
                            while ((theEntry = zipStream.GetNextEntry()) != null)
                            {
                                string directoryName = Path.GetDirectoryName(theEntry.Name);
                                string fileName = Path.GetFileName(theEntry.Name);
                                if (directoryName.Length > 0 && !Directory.Exists(Path.Combine(zipedFolder, directoryName)))
                                {
                                    Directory.CreateDirectory(Path.Combine(zipedFolder, directoryName));
                                }
                                if (fileName != string.Empty)
                                {
                                    using (FileStream fileStreamm = File.Create(Path.Combine(zipedFolder, theEntry.Name)))
                                    {
                                        int size = 2048;
                                        byte[] data = new byte[size];
                                        while ((size = zipStream.Read(data, 0, data.Length)) > 0)
                                        {
                                            fileStreamm.Write(data, 0, size);
                                        }
                                    }
                                }
                            }
                        }
                        successed = true;
                    }
                    catch
                    {
                        successed = false;
                    }
                }
                return successed;
            }
            /// <summary>   
            /// 解压功能(解压压缩文件到指定目录)   
            /// </summary>   
            /// <param name="zipFilePath">待解压的文件</param>   
            /// <param name="zipedFolder">指定解压目标目录</param>   
            /// <returns>解压结果</returns>   
            public static bool UnZip(string zipFilePath, string zipedFolder)
            {
                return UnZip(zipFilePath, zipedFolder, string.Empty);
            }
        }
    }
  • 相关阅读:
    php 构造函数支持不同个数参数的方法
    浅谈管理系统操作日志设计(附操作日志类)
    PHP 远程文件下载的进度条实现
    PHP处理大文件下载
    c语言线性表
    c语言题目:找出一个二维数组的“鞍点”,即该位置上的元素在该行上最大,在该列上最小。也可能没有鞍点
    c语言:从一组数据中选出可以组成三角形并且周长最长的三个数(简单)
    关于Staruml与powerdesigner启动使用中的问题
    Win7下安装Apache+PHP+MySQL
    <转>java编译问题:使用了未经检查或不安全的操作
  • 原文地址:https://www.cnblogs.com/xyyshishuaige/p/9016926.html
Copyright © 2011-2022 走看看