当前位置:编程学习 > asp >>

asp.net 压缩解压缩zip文件

[csharp] using System; 
using System.IO; 
using System.Diagnostics; 
using Microsoft.Win32; 
 
using ICSharpCode.SharpZipLib.Checksums; 
using ICSharpCode.SharpZipLib.Zip; 
 
///压缩、解压缩类 
namespace DotNet.Utilities 

    public class SharpZip 
    { 
        public SharpZip() 
        { } 
 
        /// <summary> 
        /// 压缩 
        /// </summary>  
        /// <param name="filename"> 压缩后的文件名(包含物理路径)</param> 
        /// <param name="directory">待压缩的文件夹(包含物理路径)</param> 
        public static void PackFiles(string filename, string directory) 
        { 
            try 
            { 
                FastZip fz = new FastZip(); 
                fz.CreateEmptyDirectories = true; 
                fz.CreateZip(filename, directory, true, ""); 
                fz = null; 
            } 
            catch (Exception) 
            { 
                throw; 
            } 
        } 
 
        /// <summary> 
        /// 解压缩 
        /// </summary> 
        /// <param name="file">待解压文件名(包含物理路径)</param> 
        /// <param name="dir"> 解压到哪个目录中(包含物理路径)</param> 
        public static bool UnpackFiles(string file, string dir) 
        { 
            try 
            { 
                if (!Directory.Exists(dir)) 
                { 
                    Directory.CreateDirectory(dir); 
                } 
                ZipInputStream s = new ZipInputStream(File.OpenRead(file)); 
                ZipEntry theEntry; 
                while ((theEntry = s.GetNextEntry()) != null) 
                { 
                    string directoryName = Path.GetDirectoryName(theEntry.Name); 
                    string fileName = Path.GetFileName(theEntry.Name); 
                    if (directoryName != String.Empty) 
                    { 
                        Directory.CreateDirectory(dir + directoryName); 
                    } 
                    if (fileName != String.Empty) 
                    { 
                        FileStream streamWriter = File.Create(dir + 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 
                            { 
                          
补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,