首 页  资讯中心 下载中心 资讯教程 最新下载 发布软件 发布文章 网通站 电信站繁體中文
设为首页
加入收藏
联系我们
 
您当前的位置:曾子源码软件下载站 -> 网络编程 -> asp.net -> 文章内容 退出登录 用户管理
热门文章
· 常用C,VC,C++书籍下..
· 新概念英语视频教程..
· 常用 JAVA JAVA2 J..
· 《梦幻麻将馆9雀圣争..
· 新东方英语视频教程..
· 常用VB,Visual Basi..
· [组图] After Effect..
· WINDOWS 所有系统文..
· [组图] 让机器运行多..
· 全美经典学习指导系..
相关文章
· 用Administrator权限..
· 用ASP编程控制在IIS..
· 用ASP.Net实现文件的..
· 用ASP.Net实现文件的..
· 用ASP.Net实现文件的..
· 学习使用ASP对象和组..
· Asp.net中创建和使用..
· Asp.net中创建和使用..
· 将图片插入数据库并..
· 将图片插入数据库并..
用ASP.Net实现文件的在线压缩和解压缩(3)
作者: johnsuna     来源:不详  发布时间:2006-6-6 19:03:25  发布人:admin

减小字体 增大字体

     // ----------------------------------------------
  // 3. ZipClass.cs
  // ----------------------------------------------
  using System;
  using System.IO;
  using ICSharpCode.SharpZipLib.Zip;
  using ICSharpCode.SharpZipLib.GZip;
  using ICSharpCode.SharpZipLib.BZip2;
  using ICSharpCode.SharpZipLib.Checksums;
  using ICSharpCode.SharpZipLib.Zip.Compression;
  using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
  
  namespace WebZipUnzip
  {
   /// <summary>
   /// 压缩文件
   /// </summary>
   public class ZipClass
   {
   public void ZipFile(string FileToZip, string ZipedFile ,int CompressionLevel, int BlockSize,string password)
   {
   //如果文件没有找到,则报错
   if (! System.IO.File.Exists(FileToZip))
   {
   throw new System.IO.FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd");
   }
  
   System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip,System.IO.FileMode.Open , System.IO.FileAccess.Read);
   System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
   ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
   ZipEntry ZipEntry = new ZipEntry("ZippedFile");
   ZipStream.PutNextEntry(ZipEntry);
   ZipStream.SetLevel(CompressionLevel);
   byte[] buffer = new byte[BlockSize];
   System.Int32 size =StreamToZip.Read(buffer,0,buffer.Length);
   ZipStream.Write(buffer,0,size);
   try
   {
   while (size < StreamToZip.Length)
   {
   int sizeRead =StreamToZip.Read(buffer,0,buffer.Length);
   ZipStream.Write(buffer,0,sizeRead);
   size += sizeRead;
   }
   }
   catch(System.Exception ex)
   {
   throw ex;
   }
   ZipStream.Finish();
   ZipStream.Close();
   StreamToZip.Close();
   }
  
   public void ZipFileMain(string[] args)
   {
   //string[] filenames = Directory.GetFiles(args[0]);
   string[] filenames = new string[]{args[0]};
  
   Crc32 crc = new Crc32();
   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;
  
   // set Size and the crc, because the information
   // about the size and crc should be stored in the header
   // if it is not set it is automatically written in the footer.
   // (in this case size == crc == -1 in the header)
   // Some ZIP programs have problems with zip files that don't store
   // the size and crc in the header.
   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();
   }
   }
  }  做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
[] [返回上一页] [打 印] [收 藏]
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [发表评论...]
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 网站信息排名查询
Copyright © 2004-2006 Zasp.Net. All Rights Reserved .