[C#] 微厦在线学习学院版 mooc慕课系统 asp.net源码 微信+APP 普及版 →→→→→进入此内容的聊天室

来自 , 2020-10-19, 写在 C#, 查看 103 次.
URL http://www.code666.cn/view/18ad9899
  1. using System;
  2. using System.Xml;
  3. using System.IO;
  4. using System.Collections;
  5. using System.Text;
  6. using System.Web;
  7. using ICSharpCode.SharpZipLib;
  8. using ICSharpCode.SharpZipLib.Zip.Compression;
  9. using ICSharpCode.SharpZipLib.GZip;
  10. using System.Collections.Generic;
  11. using ICSharpCode.SharpZipLib.Zip;
  12. using ICSharpCode.SharpZipLib.Checksums;
  13.  
  14. namespace Song.Extend
  15. {
  16.     public class Compress
  17.     {
  18.         /// <summary>
  19.         /// 压缩文件
  20.         /// </summary>
  21.         /// <param name="strFileFolder"></param>
  22.         /// <param name="strZip"></param>
  23.         public static void ZipFile(string strFileFolder, string strZip)
  24.         {
  25.             if (strFileFolder[strFileFolder.Length - 1] != Path.DirectorySeparatorChar)
  26.                 strFileFolder += Path.DirectorySeparatorChar;
  27.             ZipOutputStream s = new ZipOutputStream(System.IO.File.Create(strZip));
  28.             s.SetLevel(6); // 0 - store only to 9 - means best compression
  29.             zip(strFileFolder, strZip, s);
  30.             s.Finish();
  31.             s.Close();
  32.         }
  33.         private static void zip(string strFile, string StoreFile, ZipOutputStream s)
  34.         {
  35.             if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
  36.                 strFile += Path.DirectorySeparatorChar;
  37.             Crc32 crc = new Crc32();
  38.             string[] filenames = Directory.GetFileSystemEntries(strFile);
  39.             foreach (string file in filenames)
  40.             {
  41.                 //如果本目录内有正在被压缩的文件,跳过
  42.                 if (file == StoreFile) continue;
  43.                 if (Directory.Exists(file))
  44.                 {
  45.                     zip(file, StoreFile, s);
  46.  
  47.                 }
  48.                 else
  49.                 {
  50.                     //打开压缩文件,开始压缩
  51.                     FileStream fs = System.IO.File.OpenRead(file);
  52.                     byte[] buffer = new byte[fs.Length];
  53.                     fs.Read(buffer, 0, buffer.Length);
  54.                     string tempfile = file.Substring(StoreFile.LastIndexOf("\\") + 1);
  55.                     ZipEntry entry = new ZipEntry(tempfile);
  56.                     entry.DateTime = DateTime.Now;
  57.                     entry.Size = fs.Length;
  58.                     fs.Close();
  59.                     crc.Reset();
  60.                     crc.Update(buffer);
  61.                     entry.Crc = crc.Value;
  62.                     s.PutNextEntry(entry);
  63.                     s.Write(buffer, 0, buffer.Length);
  64.                 }
  65.             }
  66.         }
  67.  
  68.        
  69.  
  70.         /// <summary>  
  71.         /// 解压缩ZIP文件到指定文件夹  
  72.         /// </summary>  
  73.         /// <param name="zipfileName">ZIP文件</param>  
  74.         /// <param name="UnZipDir">解压文件夹</param>  
  75.         /// <param name="password">压缩文件密码</param>  
  76.         public static void UnZipFile(string zipfileName, string UnZipDir, string password)
  77.         {
  78.  
  79.             //zipfileName=@"\Storage Card\PDADataExchange\receive\zip\test.zip";
  80.             //UnZipDir= @"\Storage Card\PDADataExchange\receive\xml\";
  81.             //password="";
  82.  
  83.             ZipInputStream s = new ZipInputStream(System.IO.File.OpenRead(zipfileName));
  84.             if (password != null && password.Length > 0)
  85.                 s.Password = password;
  86.             try
  87.             {
  88.                 ZipEntry theEntry;
  89.                 while ((theEntry = s.GetNextEntry()) != null)
  90.                 {
  91.                     string directoryName = Path.GetDirectoryName(UnZipDir);
  92.                     string pathname = Path.GetDirectoryName(theEntry.Name);
  93.                     string fileName = Path.GetFileName(theEntry.Name);
  94.  
  95.                     //生成解压目录    
  96.                     pathname = pathname.Replace(":", "$");//处理压缩时带有盘符的问题  
  97.                     directoryName = directoryName + "\\" + pathname;
  98.                     Directory.CreateDirectory(directoryName);
  99.  
  100.                     if (fileName != String.Empty)
  101.                     {
  102.                         //解压文件到指定的目录  
  103.                         FileStream streamWriter = System.IO.File.Create(directoryName + "\\" + fileName);
  104.  
  105.                         int size = 2048;
  106.                         byte[] data = new byte[2048];
  107.                         while (true)
  108.                         {
  109.                             size = s.Read(data, 0, data.Length);
  110.                             if (size > 0)
  111.                             {
  112.                                 streamWriter.Write(data, 0, size);
  113.                             }
  114.                             else
  115.                             {
  116.                                 break;
  117.                             }
  118.                         }
  119.                         streamWriter.Close();
  120.                     }
  121.                 }
  122.                 s.Close();
  123.             }
  124.             catch (Exception eu)
  125.             {
  126.                 throw eu;
  127.             }
  128.             finally
  129.             {
  130.                 s.Close();
  131.             }
  132.  
  133.         }
  134.  
  135.     }
  136. }
  137.  

回复 "微厦在线学习学院版 mooc慕课系统 asp.net源码 微信+APP 普及版"

这儿你可以回复上面这条便签

captcha