现在的位置: 首页 > 综合 > 正文

在ASP.NET中实现压缩多个文件为.zip文件,实现批量下载功能

2013年03月12日 ⁄ 综合 ⁄ 共 16493字 ⁄ 字号 评论关闭

使用ICSharpCode.SharpZipLib.dll;  下载地址   http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx

下面是对#ZipLib进行.net下的解压缩的方法的介绍。  

  1.BZip2     
     加入ICSharpCode.SharpZipLib.dll的引用,在#Develop的安装目录下的\SharpDevelop\bin目录下。然后在程序中使用using语句把BZip2  
    
  类库包含进来。     
  压缩:使用BZip2的静态方法Compress。     
          它的第一个参数是所要压缩的文件所代表的输入流,可以使用System.IO.File的静态方法OpenRead。    
          第二个参数是要建立的压缩文件所代表的输出流,可以使用System.IO.File的静态方法Create创建,压缩文件名是所要压缩文件的文件名  
    
  加上压缩后缀.bz(同样你也可以取其他的文件名)。     
          第三个参数是要压缩的块大小(一般为2048的整数)。     
    
  解压:使用BZip2的静态方法Decompress。     
          它的第一个参数是所要解压的压缩文件所代表的输入流,可以使用System.IO.File的静态方法OpenRead。    
          第二个参数是要建立的解压文件所代表的输出流,可以使用System.IO.File的静态方法Create创建,因为解压文件的文件名是去掉了压缩  
    
  文件扩展名的压缩文件名(你也可以做成解压文件与压缩文件不同名的)。     
  编译你的程序,然后在命令行方式下输入bzip2   文件名(假设建立的C#文件是bzip2,就可以生成压缩文件;输入bzip2   -d   文件名,就会解压  
  出文件来(-d是用来表示解压,你也可以使用其他的符号)。     
  呵呵,原来做压缩可以这么简单的,压缩效果也可以啊。  

 using   System;     
  using   System.IO;     
  using   ICSharpCode.SharpZipLib.BZip2;     
    
  class   MainClass     
  {     
        public   static   void   Main(string[]   args)     
        {     
              if   (args[0]   ==   "-d")   {   //   解压     
                    BZip2.Decompress(File.OpenRead(args[1]),   File.Create(Path.GetFileNameWithoutExtension(args[1])));     
              }   else   {   //压缩     
                    BZip2.Compress(File.OpenRead(args[0]),   File.Create(args[0]   +   ".bz"),   4096);     
              }     
        }     
  }     
2.GZip       
        加入ICSharpCode.SharpZipLib.dll的引用,在#Develop的安装目录下的\SharpDevelop\bin目录下。然后在程序中使用using语句把GZip类  
    
  库包含进来。       
        由于GZip没有BZip2的简单解压缩方法,因此只能使用流方法来进行解压缩。具体的方法见程序的说明。    
        编译程序,然后在命令行方式下输入GZip   文件名(假设建立的C#文件是GZip,就可以生成压缩文件;输入GZip   -d   文件名,就会解压出文  
    
  件来(-d是用来表示解压,你也可以使用其他的符号)。       
 using   System;     
  using   System.IO;     
    
  using   ICSharpCode.SharpZipLib.GZip;     
    
  class   MainClass     
  {     
        public   static   void   Main(string[]   args)     
        {     
              if   (args[0]   ==   "-d")   {   //   解压     
                    Stream   s   =   new   GZipInputStream(File.OpenRead(args[1]));     
                    //生成一个GZipInputStream流,用来打开压缩文件。     
                  
//因为GZipInputStream由Stream派生,所以它可以赋给Stream。     
                      
//它的构造函数的参数是一个表示要解压的压缩文件所代表的文件流     
                    FileStream   fs   =   File.Create(Path.GetFileNameWithoutExtension(args[1]));     
                    //生成一个文件流,它用来生成解压文件     
                    
//可以使用System.IO.File的静态函数Create来生成文件流     
                    int   size   =   2048;//指定压缩块的大小,一般为2048的倍数     
                    byte[]   writeData   =   new   byte[size];//指定缓冲区的大小     
                    while   (true)   {     
                          size   =   s.Read(writeData,   0,   size);//读入一个压缩块     
                          if   (size   >   0)   {     
                                fs.Write(writeData,   0,   size);//写入解压文件代表的文件流     
                          }   else   {     
                                break;//若读到压缩文件尾,则结束     
                          }     
                    }     
                    s.Close();     
              }   else   {   //   压缩     
                    Stream   s   =   new   GZipOutputStream(File.Create(args[0]   +   ".gz"));     
                    //生成一个GZipOutputStream流,用来生成压缩文件。     
                                                  
//因为GZipOutputStream由Stream派生,所以它可以赋给Stream。     
                      FileStream   fs   =   File.OpenRead(args[0]);     
                    /生成一个文件流,它用来打开要压缩的文件     
                    //可以使用System.IO.File的静态函数OpenRead来生成文件流     
                    byte[]   writeData   =   new   byte[fs.Length];     
                    //指定缓冲区的大小     
                    fs.Read(writeData,   0,   (int)fs.Length);     
                    //读入文件     
                    s.Write(writeData,   0,   writeData.Length);     
                    //写入压缩文件     
                    s.Close();     
                    //关闭文件     
              }     
        }     
  } 

   使用这个类库固然好,但是也有些缺陷,它只能压缩文件夹第一级子目录中的“文件”(不包括文件夹和子目录)的情况,这也不能满足我的要求,我想要的是可以压缩任意路径的多个文件。
      没办法,只好再想别的办法。郁闷了很久之后在别人的一篇文章中终于找到了灵感,别人的一篇文章是写在java中实现压缩zip文件,我看了后发现在java中实现压缩为zip文件很容易。灵机一动我想到了.net中的J#,J#中应该有java中的这样类,如果有的话,那么我在我的C#程序中就可以引用了(利用.net特有的语言互操作性)。于是我就上网搜这方面的内容,终于在MSDN中找到了这样的例子

http://msdn.microsoft.com/en-gb/library/aa686114(zh-cn).aspx#EHAA得来全不费功夫啊),贴出来找到的代码,大家共同学习。

using System;
using System.Collections;
using java.util;
using java.util.zip;

namespace CsZip
{
    public delegate Enumeration EnumerationMethod();

    /// <summary>
    
/// Wraps java enumerators 
    
/// </summary>
    public class EnumerationAdapter : IEnumerable
    {
        private class EnumerationWrapper : IEnumerator
        {
            private EnumerationMethod m_Method;
            private Enumeration m_Wrapped;
            private object m_Current;

            public EnumerationWrapper(EnumerationMethod method)
            {
                m_Method = method;
            }

            // IEnumerator
            public object Current
            {
                get { return m_Current; }
            }

            public void Reset()
            {
                m_Wrapped = m_Method();
                if (m_Wrapped == null)
                    throw new InvalidOperationException();
            }

            public bool MoveNext()
            {
                if (m_Wrapped == null)
                    Reset();
                bool Result = m_Wrapped.hasMoreElements();
                if (Result)
                    m_Current = m_Wrapped.nextElement();
                return Result;
            }
        }

        private EnumerationMethod m_Method;

        public EnumerationAdapter(EnumerationMethod method)
        {
            if (method == null)
                throw new ArgumentException();
            m_Method = method;
        }

        // IEnumerable
        public IEnumerator GetEnumerator()
        {
            return new EnumerationWrapper(m_Method);
        }
    }

    public delegate bool FilterEntryMethod(ZipEntry e);

    /// <summary>
    
/// Zip stream utils
    
/// </summary>
    public class ZipUtility
    {
        public static void CopyStream(java.io.InputStream from, java.io.OutputStream to)
        {
            sbyte[] buffer = new sbyte[8192];
            int got;
            while ((got = from.read(buffer, 0, buffer.Length)) > 0)
                to.write(buffer, 0, got);
        }

        public static void ExtractZipFile(ZipFile file, string path, FilterEntryMethod filter)
        {
            foreach (ZipEntry entry in new EnumerationAdapter(new EnumerationMethod(file.entries)))
            {
                if (!entry.isDirectory())
                {
                    if ((filter == null || filter(entry)))
                    {
                        java.io.InputStream s = file.getInputStream(entry);
                        try
                        {
                            string fname = System.IO.Path.GetFileName(entry.getName());
                            string newpath = System.IO.Path.Combine(path, System.IO.Path.GetDirectoryName(entry.getName()));

                            System.IO.Directory.CreateDirectory(newpath);

                            java.io.FileOutputStream dest = new java.io.FileOutputStream(System.IO.Path.Combine(newpath, fname));
                            try
                            {
                                CopyStream(s, dest);
                            }
                            finally
                            {
                                dest.close();
                            }
                        }
                        finally
                        {
                            s.close();
                        }
                    }
                }
            }
        }

        /// <summary>
        
/// 创建新的Zip文件
        
/// </summary>
        
/// <param name="fileName">Zip文件的路径</param>
        
/// <returns>Zip文件的路径</returns>
        public static ZipFile CreateEmptyZipFile(string fileName)
        {
            new ZipOutputStream(new java.io.FileOutputStream(fileName)).close();
            return new ZipFile(fileName);
        }

        /// <summary>
        
/// 向存在的Zip文件中添加待压缩的文件
        
/// </summary>
        
/// <param name="file">Zip文件</param>
        
/// <param name="filter"></param>
        
/// <param name="newFiles">待压缩的文件的路径</param>
        
/// <returns></returns>
        public static ZipFile UpdateZipFile(ZipFile file, FilterEntryMethod filter, string[] newFiles)
        {
            string prev = file.getName();
            string tmp = System.IO.Path.GetTempFileName();
            ZipOutputStream to = new ZipOutputStream(new java.io.FileOutputStream(tmp));
            try
            {
                CopyEntries(file, to, filter);
                // add entries here
                if (newFiles != null)
                {
                    foreach (string f in newFiles)
                    {
                        ZipEntry z = new ZipEntry(f.Remove(0, System.IO.Path.GetPathRoot(f).Length));
                        z.setMethod(ZipEntry.DEFLATED);
                        to.putNextEntry(z);
                        try
                        {
                            java.io.FileInputStream s = new java.io.FileInputStream(f);
                            try
                            {
                                CopyStream(s, to);
                            }
                            finally
                            {
                                s.close();
                            }
                        }
                        finally
                        {
                            to.closeEntry();
                        }
                    }
                }
            }
            finally
            {
                to.close();
            }
            file.close();

            // now replace the old file with the new one
            System.IO.File.Copy(tmp, prev, true);
            System.IO.File.Delete(tmp);

            return new ZipFile(prev);
        }

        public static void CopyEntries(ZipFile from, ZipOutputStream to)
        {
            CopyEntries(from, to, null);
        }

        public static void CopyEntries(ZipFile from, ZipOutputStream to, FilterEntryMethod filter)
        {
            foreach (ZipEntry entry in new EnumerationAdapter(new EnumerationMethod(from.entries)))
            {
                if (filter == null || filter(entry))
                {
                    java.io.InputStream s = from.getInputStream(entry);
                    try
                    {
                        to.putNextEntry(entry);
                        try
                        {
                            CopyStream(s, to);
                        }
                        finally
                        {
                            to.closeEntry();
                        }
                    }
                    finally
                    {
                        s.close();
                    }
                }
            }
        }
    }
}

使用国外开源加压解压库ICSharpCode.SharpZipLib实现加压,该库的官方网站为

http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx

使用体验:可以照着例子实现简单的加压解压,可以加压一个文件夹中的所有文件,但没有提供加压子文件夹的说明。
目前网上的一些代码有的无法加压空文件夹,有的加压了用rar解不开,这是一点需要改进的。
但如果只需要加压文件夹第一级子目录中的“文件”(不包括文件夹和子目录)的情况,使用这个库是很方便的。而且是正常zip格式。
比.Net提供的GZipStream类强在它可以按照标准zip格式加压多个文件,而GZipStream没有提供加压多个文件的方法,需要自己定义,
这样解压也只有使用自己的程序才可以,通用性方面不如SharpZipLib。

#region 加压解压方法
        /// <summary>
        
/// 功能:压缩文件(暂时只压缩文件夹下一级目录中的文件,文件夹及其子级被忽略)
        
/// </summary>
        
/// <param name="dirPath">被压缩的文件夹夹路径</param>
        
/// <param name="zipFilePath">生成压缩文件的路径,为空则默认与被压缩文件夹同一级目录,名称为:文件夹名+.zip</param>
        
/// <param name="err">出错信息</param>
        
/// <returns>是否压缩成功</returns>
        public bool ZipFile(string dirPath, string zipFilePath, out string err)
        {
            err = "";
            if (dirPath == string.Empty)
            {
                err = "要压缩的文件夹不能为空!";
                return false;
            }
            if (!Directory.Exists(dirPath))
            {
                err = "要压缩的文件夹不存在!";
                return false;
            }
            //压缩文件名为空时使用文件夹名+.zip
            if (zipFilePath == string.Empty)
            {
                if (dirPath.EndsWith("\\"))
                {
                    dirPath = dirPath.Substring(0, dirPath.Length - 1);
                }
                zipFilePath = dirPath + ".zip";
            }

            try
            {
                string[] filenames = Directory.GetFiles(dirPath);
                using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
                {
                    s.SetLevel(9);
                    byte[] buffer = new byte[4096];
                    foreach (string file in filenames)
                    {
                        ZipEntry entry = new ZipEntry(Path.GetFileName(file));
                        entry.DateTime = DateTime.Now;
                        s.PutNextEntry(entry);
                        using (FileStream fs = File.OpenRead(file))
                        {
                            int sourceBytes;
                            do
                            {
                                sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                s.Write(buffer, 0, sourceBytes);
                            } while (sourceBytes > 0);
                        }
                    }
                    s.Finish();
                    s.Close();
                }
            }
            catch (Exception ex)
            {
                err = ex.Message;
                return false;
            }
            return true;
        }

        /// <summary>
        
/// 功能:解压zip格式的文件。
        
/// </summary>
        
/// <param name="zipFilePath">压缩文件路径</param>
        
/// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>
        
/// <param name="err">出错信息</param>
        
/// <returns>解压是否成功</returns>
        public bool UnZipFile(string zipFilePath, string unZipDir, out string err)
        {
            err = "";
            if (zipFilePath == string.Empty)
            {
                err = "压缩文件不能为空!";
                return false;
            }
            if (!File.Exists(zipFilePath))
            {
                err = "压缩文件不存在!";
                return false;
            }
            //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹
            if (unZipDir == string.Empty)
                unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
            if (!unZipDir.EndsWith("\\"))
                unZipDir += "\\";
            if (!Directory.Exists(unZipDir))
                Directory.CreateDirectory(unZipDir);

            try
            {
                using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
                {

                    ZipEntry theEntry;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        string directoryName = Path.GetDirectoryName(theEntry.Name);
                        string fileName = Path.GetFileName(theEntry.Name);
                        if (directoryName.Length > 0)
                        {
                            Directory.CreateDirectory(unZipDir + directoryName);
                        }
                        if (!directoryName.EndsWith("\\"))
                            directoryName += "\\";
                        if (fileName != String.Empty)
                        {
                            using (FileStream streamWriter = File.Create(unZipDir + 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;
                                    }
                                }
                            }
                        }
                    }//while
                }
            }
            catch (Exception ex)
            {
                err = ex.Message;
                return false;
            }
            return true;
        }//解压结束
        #endregion

需要添加对SharpZipLib的引用:
using ICSharpCode.SharpZipLib.Zip;

 

抱歉!评论已关闭.