惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
人人都是产品经理
人人都是产品经理
Spread Privacy
Spread Privacy
IT之家
IT之家
F
Fortinet All Blogs
GbyAI
GbyAI
T
Threat Research - Cisco Blogs
月光博客
月光博客
WordPress大学
WordPress大学
V
Vulnerabilities – Threatpost
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
MyScale Blog
MyScale Blog
小众软件
小众软件
AI
AI
云风的 BLOG
云风的 BLOG
Jina AI
Jina AI
博客园 - 【当耐特】
T
Tailwind CSS Blog
Recorded Future
Recorded Future
Simon Willison's Weblog
Simon Willison's Weblog
I
Intezer
Schneier on Security
Schneier on Security
Engineering at Meta
Engineering at Meta
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
T
The Exploit Database - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
PCI Perspectives
PCI Perspectives
K
Kaspersky official blog
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Webroot Blog
Webroot Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
A
About on SuperTechFans
Stack Overflow Blog
Stack Overflow Blog
N
News and Events Feed by Topic
MongoDB | Blog
MongoDB | Blog
C
CERT Recently Published Vulnerability Notes
Security Latest
Security Latest
爱范儿
爱范儿
L
LangChain Blog
V
Visual Studio Blog
W
WeLiveSecurity
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
Cisco Blogs
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - 小肠与小豆子

博客园MetaWeblog使用帮助 [ASP.NET 1.1]Global.asax.cs中的方法的含义 Microsoft .NET Pet Shop 4 架构与技术分析 转载:键盘模拟鼠标(实现用键盘操作鼠标光标)(示例代码下载) 转载:Ajax 实现在WebForm中拖动控件并即时在服务端保存状态数据 (Asp.net 2.0)(示例代码下载) 转载:Asp.net 2.0 用C# 创建 PDF文件[引用] (示例代码下载) 转载:ASP.NET 2.0 HttpHandler实现对某种文件类型权限保护 转载:asp.net千奇百怪的日历 转载:ASP.net 2.0资料吐血收藏 解决:此页正在访问其它域的数据…… sql server系统表详细说明 数据库设计方法、规范与技巧 (转自Blogcn的我的窝) 14个数据库的设计技巧 (来自Blogcn中我的窝) SQL 数据库中所有的用户表,一个表的中的所有列信息 的SQL 转载:汉字转拼音_gb2312 C# C#中的一些常用属性 IIS状态一览表 IIS简介 IIS中的 MIME类型
转载:Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)
小肠与小豆子 · 2007-05-24 · via 博客园 - 小肠与小豆子

原文:http://blog.csdn.net/ChengKing/archive/2006/12/21/1452150.aspx

(一). 实现功能

    对文件及目录的压缩及解压功能

(二). 运行图片示例

  (三).代码

   1. 压缩类

  1/// <summary>
  2/// 压缩类
  3/// </summary>

  4public class ZipClass
  5{   
  6    public static void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)
  7    {
  8        //如果文件没有找到,则报错
  9        if (!System.IO.File.Exists(FileToZip))
 10        {
 11            throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!");
 12        }

 13
 14        System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);
 15        System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
 16        ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
 17        ZipEntry ZipEntry = new ZipEntry("ZippedFile");
 18        ZipStream.PutNextEntry(ZipEntry);
 19        ZipStream.SetLevel(CompressionLevel);
 20        byte[] buffer = new byte[BlockSize];
 21        System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
 22        ZipStream.Write(buffer, 0, size);
 23        try
 24        {
 25            while (size < StreamToZip.Length)
 26            {
 27                int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
 28                ZipStream.Write(buffer, 0, sizeRead);
 29                size += sizeRead;
 30            }

 31        }

 32        catch (System.Exception ex)
 33        {
 34            throw ex;
 35        }

 36        ZipStream.Finish();
 37        ZipStream.Close();
 38        StreamToZip.Close();
 39    }

 40
 41    /// <summary>
 42    /// 压缩目录
 43    /// </summary>
 44    /// <param name="args">数组(数组[0]: 要压缩的目录; 数组[1]: 压缩的文件名)</param>

 45    public static void ZipFileDictory(string[] args)
 46    {
 47        string[] filenames = Directory.GetFiles(args[0]);
 48
 49        Crc32 crc = new Crc32();
 50        ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));            
 51        s.SetLevel(6); 
 52        foreach (string file in filenames)
 53        {
 54            //打开压缩文件
 55            FileStream fs = File.OpenRead(file);
 56
 57            byte[] buffer = new byte[fs.Length];
 58            fs.Read(buffer, 0, buffer.Length);
 59            ZipEntry entry = new ZipEntry(file);
 60
 61            entry.DateTime = DateTime.Now;
 62            
 63            entry.Size = fs.Length;
 64            fs.Close();
 65
 66            crc.Reset();
 67            crc.Update(buffer);
 68
 69            entry.Crc = crc.Value;
 70
 71            s.PutNextEntry(entry);
 72
 73            s.Write(buffer, 0, buffer.Length);
 74
 75        }

 76
 77        s.Finish();
 78        s.Close();
 79    }

 80
 81    /// <summary>
 82    /// 压缩文件
 83    /// </summary>
 84    /// <param name="FileToZip">要进行压缩的文件名</param>
 85    /// <param name="ZipedFile">压缩后生成的压缩文件名</param>

 86    public static void ZipFile(string FileToZip, string ZipedFile)
 87    {
 88        //如果文件没有找到,则报错
 89        if (!File.Exists(FileToZip))
 90        {
 91            throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!");
 92        }
            
 93        FileStream fs = File.OpenRead(FileToZip);
 94        byte[] buffer = new byte[fs.Length];
 95        fs.Read(buffer, 0, buffer.Length);
 96        fs.Close();
 97
 98        FileStream ZipFile = File.Create(ZipedFile);
 99        ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
100        ZipEntry ZipEntry = new ZipEntry("ZippedFile");
101        ZipStream.PutNextEntry(ZipEntry);
102        ZipStream.SetLevel(6);
103        
104        ZipStream.Write(buffer, 0, buffer.Length);            
105        ZipStream.Finish();
106        ZipStream.Close();
107    }

108}

109
110/// <summary>
111///  解压类
112/// </summary>

113public class UnZipClass
114{
115    /// <summary>
116    /// 解压功能(解压压缩文件到指定目录)
117    /// </summary>
118    /// <param name="args">待解压的文件</param>
119    /// <param name="args">指定解压目标目录</param>

120    public static void UnZip(string[] args)
121    {
122        ZipInputStream s = new ZipInputStream(File.OpenRead(@args[0].Trim()));            
123        ZipEntry theEntry;
124        string directoryName = Path.GetDirectoryName(@args[1].Trim());
125        
126        if (!Directory.Exists(@args[1].Trim()))
127        {
128            Directory.CreateDirectory(directoryName);
129        }

130        while ((theEntry = s.GetNextEntry()) != null)
131        {
132            ;
133            string fileName = Path.GetFileName(theEntry.Name);
134
135            if (fileName != String.Empty)
136            {            
137                FileStream streamWriter = File.Create(@args[1].Trim() + fileName);
138
139                int size = 2048;
140                byte[] data = new byte[2048];
141                while (true)
142                {
143                    size = s.Read(data, 0, data.Length);
144                    if (size > 0)
145                    {
146                        streamWriter.Write(data, 0, size);
147                    }

148                    else
149                    {
150                        break;
151                    }

152                }

153
154                streamWriter.Close();
155            }

156        }

157        s.Close();
158    }

159

2. 前台页面代码

3. 后台页面代码

(). 示例代码下载

     ZIP.rar