使用GZip解压缩文件

 
★搜Asp.net★(www.soAsp.net),为专业技术文档网站。
包括Asp.net开发技术文档·C#开发技术文档·Access/SQL Server数据库开发技术文档·VB.NET开发技术文档。
还包括·项目实战经验总结·开发经验技巧总结·项目开发心得。
使用GZip解压缩文件
 

在互联网普及的今天,网络资源变得越来越丰富,大多的网络资源都是以压缩文件的形式存在,通过本实例您将会了解相关的知识。

本示例演示了通过FileStream类和GzipStream类实现了对文件的在线解压缩。

 

程序开发步骤:

(1)新建一个网站,然后将其主页命名为Default.aspx。

(2)Default.aspx页面涉及的控件及用途如表所示。

 

控件类型

控件名称

用途

 

HTML/Table

Tabel1

布局页面

 

标准/TextBox

TextBox1

解压后的文件

 

标准/Button

Button1

解压

btnExit

退出

 

标准/FileUpload

FileUpload1

选择要解压的文件

 

(3)单击【开始解压】按钮,程序对选定的压缩文件进行指定格式的解压。【开始解压】按钮的Click事件代码如下。

protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            decompressFile.DecompressFile(FileUpload1.PostedFile.FileName, TextBox1.Text.Trim());
            Response.Write("<script>alert('解压成功。')</script>");
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.ToString() + "')</script>");
        }
    }

【开始解压】按钮的Click事件中调用了公共类decompressFile中的DecompressFile(string sourceFile,string destinationFile)方法,该方法主要用来将选定的压缩文件按指定格式进行解压,无返回值类型的静态方法,它有两个参数:sourceFile和destinationFile,其中,sourceFile参数表示要进行解压的文件及其路径,destinationFile参数表示解压后的文件及存放路径。其关键代码如下。

public static void DecompressFile(string sourceFile, string destinationFile)
    {
        if (!File.Exists(sourceFile)) throw new FileNotFoundException();
        using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open))
        {
            byte[] quartetBuffer = new byte[4];
            int position = (int)sourceStream.Length - 4;
            sourceStream.Position = position;
            sourceStream.Read(quartetBuffer, 0, 4);
            sourceStream.Position = 0;
            int checkLength = BitConverter.ToInt32(quartetBuffer, 0);
            byte[] buffer = new byte[checkLength + 100];
            using (GZipStream decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, True))
            {
                int total = 0;
                for (int offset = 0; ; )
                {
                    int bytesRead = decompressedStream.Read(buffer, offset, 100);
                    if (bytesRead == 0) break;
                    offset += bytesRead;
                    total += bytesRead;
                }
                using (FileStream destinationStream = new FileStream(destinationFile, FileMode.Create))
                {
                    destinationStream.Write(buffer, 0, total);
                    destinationStream.Flush();
                }
            }
        }
    }

    说明:

① 使用.NET Framework类库解压文件时,一定要先引用using System.IO.Compression命名空间。

② 本实例只能解压用FileStream类和GZipStream类压缩的文件,对于标准的ZIP文件或RAR文件,在解压时,会出现错误,根目录下提供了利用GZIP类压缩的文件experiment.RAR,以方便读者利用其进行GZIP的解压缩。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值