Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)
 
(一). 实现功能
    对文件及目录的压缩及解压功能
(二). 运行图片示例
Asp.net 2.0 C#实现压缩/解压功能

  (三).代码

   1. 压缩类

  1Asp.net 2.0 C#实现压缩/解压功能Asp.net 2.0 C#实现压缩/解压功能/**//// <summary>
  2Asp.net 2.0 C#实现压缩/解压功能/// 压缩类
  3Asp.net 2.0 C#实现压缩/解压功能/// </summary>

  4Asp.net 2.0 C#实现压缩/解压功能public class ZipClass
  5Asp.net 2.0 C#实现压缩/解压功能Asp.net 2.0 C#实现压缩/解压功能Asp.net 2.0 C#实现压缩/解压功能{   
  6Asp.net 2.0 C#实现压缩/解压功能    public static void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)
  7Asp.net 2.0 C#实现压缩/解压功能Asp.net 2.0 C#实现压缩/解压功能    Asp.net 2.0 C#实现压缩/解压功能{
  8Asp.net 2.0 C#实现压缩/解压功能        //如果文件没有找到,则报错
  9Asp.net 2.0 C#实现压缩/解压功能        if (!System.IO.File.Exists(FileToZip))
 10Asp.net 2.0 C#实现压缩/解压功能Asp.net 2.0 C#实现压缩/解压功能        Asp.net 2.0 C#实现压缩/解压功能{
 11Asp.net 2.0 C#实现压缩/解压功能            throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!");
 12Asp.net 2.0 C#实现压缩/解压功能        }

 13Asp.net 2.0 C#实现压缩/解压功能
 14Asp.net 2.0 C#实现压缩/解压功能        System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);
 15Asp.net 2.0 C#实现压缩/解压功能        System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
 16Asp.net 2.0 C#实现压缩/解压功能        ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
 17Asp.net 2.0 C#实现压缩/解压功能        ZipEntry ZipEntry = new ZipEntry("ZippedFile");
 18Asp.net 2.0 C#实现压缩/解压功能        ZipStream.PutNextEntry(ZipEntry);
 19Asp.net 2.0 C#实现压缩/解压功能        ZipStream.SetLevel(CompressionLevel);
 20Asp.net 2.0 C#实现压缩/解压功能        byte[] buffer = new byte[BlockSize];
 21Asp.net 2.0 C#实现压缩/解压功能        System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
 22Asp.net 2.0 C#实现压缩/解压功能        ZipStream.Write(buffer, 0, size);
 23Asp.net 2.0 C#实现压缩/解压功能        try
 24Asp.net 2.0 C#实现压缩/解压功能Asp.net 2.0 C#实现压缩/解压功能        Asp.net 2.0 C#实现压缩/解压功能{
 25Asp.net 2.0 C#实现压缩/解压功能            while (size < StreamToZip.Length)
 26Asp.net 2.0 C#实现压缩/解压功能Asp.net 2.0 C#实现压缩/解压功能            Asp.net 2.0 C#实现压缩/解压功能{
 27Asp.net 2.0 C#实现压缩/解压功能                int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
 28Asp.net 2.0 C#实现压缩/解压功能                ZipStream.Write(buffer, 0, sizeRead);
 29Asp.net 2.0 C#实现压缩/解压功能                size += sizeRead;
 30Asp.net 2.0 C#实现压缩/解压功能            }

 31Asp.net 2.0 C#实现压缩/解压功能        }

 32Asp.net 2.0 C#实现压缩/解压功能        catch (System.Exception ex)
 33Asp.net 2.0 C#实现压缩/解压功能Asp.net 2.0 C#实现压缩/解压功能        Asp.net 2.0 C#实现压缩/解压功能{
 34Asp.net 2.0 C#实现压缩/解压功能            throw ex;
 35Asp.net 2.0 C#实现压缩/解压功能        }

 36Asp.net 2.0 C#实现压缩/解压功能        ZipStream.Finish();
 37Asp.net 2.0 C#实现压缩/解压功能        ZipStream.Close();
 38Asp.net 2.0 C#实现压缩/解压功能        StreamToZip.Close();
 39Asp.net 2.0 C#实现压缩/解压功能    }

 40Asp.net 2.0 C#实现压缩/解压功能
 41Asp.net 2.0 C#实现压缩/解压功能Asp.net 2.0 C#实现压缩/解压功能    /**//// <summary>
 42Asp.net 2.0 C#实现压缩/解压功能    /// 压缩目录
 43Asp.net 2.0 C#实现压缩/解压功能    /// </summary>
 44Asp.net 2.0 C#实现压缩/解压功能    /// <param name="args">数组(数组[0]: 要压缩的目录; 数组[1]: 压缩的文件名)</param>

 45Asp.net 2.0 C#实现压缩/解压功能    public static void ZipFileDictory(string[] args)
 46Asp.net 2.0 C#实现压缩/解压功能Asp.net 2.0 C#实现压缩/解压功能    Asp.net 2.0 C#实现压缩/解压功能{
 47Asp.net 2.0 C#实现压缩/解压功能        string[] filenames = Directory.GetFiles(args[0]);
 48Asp.net 2.0 C#实现压缩/解压功能
 49Asp.net 2.0 C#实现压缩/解压功能        Crc32 crc = new Crc32();
 50Asp.net 2.0 C#实现压缩/解压功能        ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));            
 51Asp.net 2.0 C#实现压缩/解压功能        s.SetLevel(6); 
 52Asp.net 2.0 C#实现压缩/解压功能        foreach (string file in filenames)
 53Asp.net 2.0 C#实现压缩/解压功能Asp.net 2.0 C#实现压缩/解压功能        Asp.net 2.0 C#实现压缩/解压功能{
 54Asp.net 2.0 C#实现压缩/解压功能            //打开压缩文件
 55Asp.net 2.0 C#实现压缩/解压功能            FileStream fs = File.OpenRead(file);
 56Asp.net 2.0 C#实现压缩/解压功能
 57Asp.net 2.0 C#实现压缩/解压功能            byte[] buffer = new byte[fs.Length];
 58Asp.net 2.0 C#实现压缩/解压功能            fs.Read(buffer, 0, buffer.Length);
 59Asp.net 2.0 C#实现压缩/解压功能            ZipEntry entry = new ZipEntry(file);
 60Asp.net 2.0 C#实现压缩/解压功能
 61Asp.net 2.0 C#实现压缩/解压功能            entry.DateTime = DateTime.Now;
 62Asp.net 2.0 C#实现压缩/解压功能            
 63Asp.net 2.0 C#实现压缩/解压功能            entry.Size = fs.Length;
 64Asp.net 2.0 C#实现压缩/解压功能            fs.Close();
 65Asp.net 2.0 C#实现压缩/解压功能
 66Asp.net 2.0 C#实现压缩/解压功能            crc.Reset();
 67Asp.net 2.0 C#实现压缩/解压功能            crc.Update(buffer);
 68Asp.net 2.0 C#实现压缩/解压功能
 69Asp.net 2.0 C#实现压缩/解压功能            entry.Crc = crc.Value;
 70Asp.net 2.0 C#实现压缩/解压功能
 71Asp.net 2.0 C#实现压缩/解压功能            s.PutNextEntry(entry);
 72Asp.net 2.0 C#实现压缩/解压功能
 73Asp.net 2.0 C#实现压缩/解压功能            s.Write(buffer, 0, buffer.Length);
 74Asp.net 2.0 C#实现压缩/解压功能
 75Asp.net 2.0 C#实现压缩/解压功能        }

 76Asp.net 2.0 C#实现压缩/解压功能
 77Asp.net 2.0 C#实现压缩/解压功能        s.Finish();
 78Asp.net 2.0 C#实现压缩/解压功能        s.Close();
 79Asp.net 2.0 C#实现压缩/解压功能    }

 80Asp.net 2.0 C#实现压缩/解压功能
 81Asp.net 2.0 C#实现压缩/解压功能Asp.net 2.0 C#实现压缩/解压功能    /**//// <summary>
 82Asp.net 2.0 C#实现压缩/解压功能    /// 压缩文件
 83Asp.net 2.0 C#实现压缩/解压功能    /// </summary>
 84Asp.net 2.0 C#实现压缩/解压功能    /// <param name="FileToZip">要进行压缩的文件名</param>
 85Asp.net 2.0 C#实现压缩/解压功能    /// <param name="ZipedFile">压缩后生成的压缩文件名</param>

 86Asp.net 2.0 C#实现压缩/解压功能    public static void ZipFile(string FileToZip, string ZipedFile)
 87Asp.net 2.0 C#实现压缩/解压功能Asp.net 2.0 C#实现压缩/解压功能    Asp.net 2.0 C#实现压缩/解压功能{
 88Asp.net 2.0 C#实现压缩/解压功能        //如果文件没有找到,则报错
 89Asp.net 2.0 C#实现压缩/解压功能        if (!File.Exists(FileToZip))
 90Asp.net 2.0 C#实现压缩/解压功能Asp.net 2.0 C#实现压缩/解压功能        Asp.net 2.0 C#实现压缩/解压功能{
 91Asp.net 2.0 C#实现压缩/解压功能            throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!");
 92Asp.net 2.0 C#实现压缩/解压功能        }
            
 93Asp.net 2.0 C#实现压缩/解压功能        FileStream fs = File.OpenRead(FileToZip);
 94Asp.net 2.0 C#实现压缩/解压功能        byte[] buffer = new byte[fs.Length];
 95Asp.net 2.0 C#实现压缩/解压功能        fs.Read(buffer, 0, buffer.Length);
 96Asp.net 2.0 C#实现压缩/解压功能        fs.Close();
 97Asp.net 2.0 C#实现压缩/解压功能
 98Asp.net 2.0 C#实现压缩/解压功能        FileStream ZipFile = File.Create(ZipedFile);
 99Asp.net 2.0 C#实现压缩/解压功能        ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
100Asp.net 2.0 C#实现压缩/解压功能        ZipEntry ZipEntry = new ZipEntry("ZippedFile");
101Asp.net 2.0 C#实现压缩/解压功能        ZipStream.PutNextEntry(ZipEntry);
102Asp.net 2.0 C#实现压缩/解压功能        ZipStream.SetLevel(6);
103Asp.net 2.0 C#实现压缩/解压功能        
104Asp.net 2.0 C#实现压缩/解压功能        ZipStream.Write(buffer, 0, buffer.Length);            
105Asp.net 2.0 C#实现压缩/解压功能        ZipStream.Finish();
106Asp.net 2.0 C#实现压缩/解压功能        ZipStream.Close();
107Asp.net 2.0 C#实现压缩/解压功能    }

108Asp.net 2.0 C#实现压缩/解压功能}

109Asp.net 2.0 C#实现压缩/解压功能
110Asp.net 2.0 C#实现压缩/解压功能Asp.net 2.0 C#实现压缩/解压功能/**//// <summary>
111Asp.net 2.0 C#实现压缩/解压功能///  解压类
112Asp.net 2.0 C#实现压缩/解压功能/// </summary>

113Asp.net 2.0 C#实现压缩/解压功能public class UnZipClass
114Asp.net 2.0 C#实现压缩/解压功能Asp.net 2.0 C#实现压缩/解压功能Asp.net 2.0 C#实现压缩/解压功能{
115Asp.net 2.0 C#实现压缩/解压功能Asp.net 2.0 C#实现压缩/解压功能    /**//// <summary>
116Asp.net 2.0 C#实现压缩/解压功能    /// 解压功能(解压压缩文件到指定目录)
117Asp.net 2.0 C#实现压缩/解压功能  &nb

相关文章:

  • 2021-11-02
  • 2021-08-05
  • 2022-01-14
  • 2021-12-02
  • 2022-12-23
猜你喜欢
  • 2021-07-07
  • 2021-12-28
  • 2021-11-06
  • 2021-06-26
相关资源
相似解决方案