[DNN学习所得]让IE也能实现解压缩功能(提供演示源码下载)

来源:互联网 发布:回归分析法ab值算法 编辑:程序博客网 时间:2024/06/09 21:37
在看DNN时发现了一个很酷的功能:能通过IE浏览器实现对Zip文件的压缩和生成Zip文件文件压缩包的功能。在仔细看过程序以后发现它是调用的SharpZipLib.dll类库中的内容实现的压缩与解压功能。上网查了一下SharpZipLib,发现它居然是开源的,在http://www.icsharpcode.net网站上有下。在网站里关于SharpZipLib的源文件和调用演示包括帮助文档都有下,不过当然全是E文的。(真不知在中国有哪家公司在做.net的开源,真的十分想看国产的优秀开源项目)

在SharpZipLib中实现解压的方法(演示代码):

using System;
using System.Text;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data;

using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip;


class MainClass
{            
    
// 在控制命令行下输入要解压的Zip文件名
    public static void Main(string[] args)
    
{
        
// 创建读取Zip文件对象
        ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));
        
// Zip文件中的每一个文件
        ZipEntry theEntry;
        
// 循环读取Zip文件中的每一个文件
        while ((theEntry = s.GetNextEntry()) != null{
            
            Console.WriteLine(theEntry.Name);
            
            
string directoryName = Path.GetDirectoryName(theEntry.Name);
            
string fileName      = Path.GetFileName(theEntry.Name);
            
            
// create directory
            Directory.CreateDirectory(directoryName);
            
            
if (fileName != String.Empty) {
                
// 解压文件
                FileStream streamWriter = File.Create(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();
    }

}

在SharpZipLib中实现压缩的方法:
using System;
using System.IO;

using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;

class MainClass
{
    
// 输入要压缩的文件夹和要创建的Zip文件文件名称
    public static void Main(string[] args)
    
{
        
string[] filenames = Directory.GetFiles(args[0]);
        
        Crc32 crc 
= new Crc32();
        
// 创建输出Zip文件对象
        ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));
        
// 设置压缩等级
        s.SetLevel(6); // 0 - store only to 9 - means best compression
        
// 循环读取要压缩的文件,写入压缩包        
        foreach (string file in filenames) {
            FileStream fs 
= File.OpenRead(file);
            
            
byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 
0, buffer.Length);
            ZipEntry entry 
= new ZipEntry(file);
            
            entry.DateTime 
= DateTime.Now;
            
            entry.Size 
= fs.Length;
            fs.Close();
            
            crc.Reset();
            crc.Update(buffer);
            
            entry.Crc  
= crc.Value;
            
            s.PutNextEntry(entry);
            
            s.Write(buffer, 
0, buffer.Length);
            
        }

        
        s.Finish();
        s.Close();
    }

}

  类中应该还有其他功能,还没有来得及看。具体SharpZipLib中是如何实行的也还没有来得及看,先试一试最简单功能,大家有兴趣可以下载看看。默认提供的演示程序是控制台下运行的(好像还有其他环境中运行的,不过我没有试),我照着做了一个WEB应用程序的,希望能对大家有用。

下载WEB下的演示程序>>
更多相关内容>>

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=382983 

原创粉丝点击