asp.net中實現檔案壓縮及解壓代碼

來源:互聯網
上載者:User
 代碼如下 複製代碼

using System;
 using System.IO;
 using ICSharpCode.SharpZipLib.Checksums;
 using ICSharpCode.SharpZipLib.Zip;
 using ICSharpCode.SharpZipLib.GZip;
 
 namespace ZipFile
 {
     /// <summary>
     /// 壓縮檔
     /// </summary>
     public class ZipHelp
     {
         public string ZipName { get; set; }
         /// <summary>
         /// 壓縮檔夾
         /// </summary>
         /// <param name="zipSourcePath">需要壓縮的檔案夾路徑(全路徑)</param>
         /// <param name="zipToFilePath">壓縮後儲存的路徑且必須帶尾碼名如:D:\aa.zip(如果為空白字元則預設儲存到同級檔案夾名稱為源檔案名稱)</param>
         public void ZipFileMain(string zipSourcePath,string zipToFilePath)
         {
             string[] filenames = Directory.GetFiles(zipSourcePath);
             ZipName = zipSourcePath.Substring(zipSourcePath.LastIndexOf("\")+1);
             //定義壓縮更目錄對象
             Crc32 crc = new Crc32();
             ZipOutputStream s = new ZipOutputStream(File.Create(zipToFilePath.Equals("")? zipSourcePath+".zip":zipToFilePath));
 
             s.SetLevel(6); // 設定壓縮層級
             //遞迴壓縮檔夾下的所有檔案和字檔案夾
             AddDirToDir(crc, s,zipSourcePath);
 
             s.Finish();
             s.Close();
         }
         /// <summary>
         /// 壓縮單個檔案
         /// </summary>
         /// <param name="zipSourcePath">需要壓縮的檔案路徑(全路徑)</param>
         /// <param name="zipToFilePath">壓縮後儲存的檔案路徑(如果是Null 字元則預設壓縮到同目錄下檔案名稱為源檔案名稱)</param>
         public void ZipByFile(string zipSourcePath,string zipToFilePath)
         {
             //定義壓縮更目錄對象
             Crc32 crc = new Crc32();
             string dirName = zipSourcePath.Substring(zipSourcePath.LastIndexOf("\") + 1, zipSourcePath.LastIndexOf(".") - (zipSourcePath.LastIndexOf("\") + 1)) + ".zip";
             ZipOutputStream s = new ZipOutputStream(File.Create(zipToFilePath.Equals("")? zipSourcePath.Substring(0,zipSourcePath.LastIndexOf("\"))+"\"+ dirName:zipToFilePath));
             s.SetLevel(6); // 設定壓縮層級
             AddFileToDir(crc,s,zipSourcePath,0);
             s.Finish();
             s.Close();
         }
         /// <summary>
         /// 壓縮單個檔案到指定壓縮檔夾下(內部調用)
         /// </summary>
         /// <param name="crc"></param>
         /// <param name="s"></param>
         /// <param name="file">檔案路徑</param>
         public void AddFileToDir(Crc32 crc,ZipOutputStream s,string file,int dotype)
         {
             FileStream fs = File.OpenRead(file);
             byte[] buffer = new byte[fs.Length];
             fs.Read(buffer, 0, buffer.Length);
             string filename="";
             if (dotype == 0)
                 filename = file.Substring(file.LastIndexOf("\") + 1);
             else
                 filename = file.Substring(file.IndexOf(ZipName));
             ZipEntry entry = new ZipEntry(filename);
             entry.DateTime = DateTime.Now;
             entry.Size = fs.Length;
             fs.Close();
             crc.Reset();
             crc.Update(buffer);
             entry.Crc = crc.Value;
             s.PutNextEntry(entry);
             s.Write(buffer, 0, buffer.Length);
         }
         /// <summary>
         /// 遞迴檔案夾層級(內部調用)
         /// </summary>
         /// <param name="crc"></param>
         /// <param name="s"></param>
         /// <param name="file"></param>
         public void AddDirToDir(Crc32 crc, ZipOutputStream s, string file)
         {
             //添加此檔案夾下的檔案
             string[] files = Directory.GetFiles(file);
             foreach (string i in files)
             {
                 AddFileToDir(crc,s,i,1);
             }
             //查詢此檔案夾下的子檔案夾
             string[] dirs=Directory.GetDirectories(file);
             foreach (string i in dirs)
             {
                 AddDirToDir(crc,s,i);
             }
         }
     }
 }


-------------------------------------------接下來是解壓的類------------------------------------------------------

 代碼如下 複製代碼

using System;
 using System.Text;
 using System.Collections;
 using System.IO;
 using System.Diagnostics;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.Data;
 
 using ICSharpCode.SharpZipLib.BZip2;
 using ICSharpCode.SharpZipLib.Zip;
 using ICSharpCode.SharpZipLib.Zip.Compression;
 using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
 using ICSharpCode.SharpZipLib.GZip;
 
 namespace ZipFile
 {
     /// <summary>
     /// 解壓檔案
     /// </summary>
     public class UnZipFile
     {
         /// <summary>
         /// 解壓檔案方法
         /// </summary>
         /// <param name="UnSourceZip">源檔案</param>
         /// <param name="UnZipToPath">解壓到目錄路徑(如果為空白字元則是解壓到目前的目錄)</param>
         public void UnZip(string UnSourceZip,string UnZipToPath)
         {
             ZipInputStream s = new ZipInputStream(File.OpenRead(UnSourceZip));
 
             ZipEntry theEntry;
             while ((theEntry = s.GetNextEntry()) != null)
             {
 
                 string fileName = Path.GetFileName(theEntry.Name);
 
                 //產生解壓目錄
                 if (!UnZipToPath.Equals(""))
                     Directory.CreateDirectory(UnZipToPath);
                 else
                     UnZipToPath = UnSourceZip.Substring(0,UnSourceZip.LastIndexOf("\")>0?UnSourceZip.LastIndexOf("\"):0);
                 if (fileName != String.Empty)
                 {
                     //建立檔案夾
                     int startIndex = 0;
                     while(theEntry.Name.IndexOf("/",startIndex)>0)
                     {
                         //計算檔案夾路徑
                         string dirpath=theEntry.Name.Substring(0, theEntry.Name.IndexOf("/", startIndex));
                         //添加檔案夾
                         Directory.CreateDirectory(UnZipToPath.Equals("")? dirpath: UnZipToPath + "//" + dirpath);
                         startIndex = theEntry.Name.IndexOf("/", startIndex)+1;
                     }
                     //解壓檔案到指定的目錄
                     FileStream streamWriter = File.Create(UnZipToPath.Equals("")? theEntry.Name: UnZipToPath + "//" + 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();
         }
     }
 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.