正在阅读:用ASP.Net实现文件的在线压缩和解压缩用ASP.Net实现文件的在线压缩和解压缩

2006-05-18 09:57 出处: 作者:johnsuna 责任编辑:xietaoming

// ---------------------------------------------
// 2. UnZipClass.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
{
 public class UnZipClass
 {  
  /// <summary>
  /// 解压文件
  /// </summary>
  /// <param name="args">包含要解压的文件名和要解压到的目录名数组</param>
  public void UnZip(string[] args)
  {
   ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));
   try
   {   
    ZipEntry theEntry;
    while ((theEntry = s.GetNextEntry()) != null)
    {  
     string directoryName = Path.GetDirectoryName(args[1]);
     string fileName      = Path.GetFileName(theEntry.Name);
   
     //生成解压目录
     Directory.CreateDirectory(directoryName);
   
     if (fileName != String.Empty)
     {  
      //解压文件到指定的目录
      FileStream streamWriter = File.Create(args[1]+fileName);
   
      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();
   }
   catch(Exception eu)
   {
    throw eu;
   }
   finally
   {
    s.Close();
   }

  }//end UnZip

  public static bool UnZipFile(string file, string dir)
  {
   try
   {
    if (!Directory.Exists(dir))
     Directory.CreateDirectory(dir);
    string fileFullName = Path.Combine(dir,file);
    ZipInputStream s = new ZipInputStream(File.OpenRead( fileFullName ));

    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( Path.Combine(dir, directoryName));

     if (fileName != String.Empty)
     {
      FileStream streamWriter = File.Create( Path.Combine(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
       {
        break;
       }
      }

      streamWriter.Close();
     }
    }
    s.Close();
    return true;
   }
   catch (Exception)
   {
    throw;
   }
  }

 }//end UnZipClass
}

键盘也能翻页,试试“← →”键

相关文章

关注我们

最新资讯离线随时看 聊天吐槽赢奖品