文件 在线压缩 技术
生活随笔
收集整理的這篇文章主要介紹了
文件 在线压缩 技术
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
客戶有時會有這樣的需求:把服務(wù)器上的某個文件和文件夾下載到本地,我們的解決方法是使用在線壓縮技術(shù)。就是把文件先在服務(wù)器上壓縮,創(chuàng)建一個.ZIP,然后下載到本地。
網(wǎng)上提供了原代碼,但是有些不足之處。第一:中文文件名的文件壓縮之后,壓縮包里的中文文件名都是亂碼。不過我已經(jīng)改了源文件,并且編譯了一個新的dll,大家直接引用這個dll就可以了。有需要的給我要,我的郵件ligc@langchao.com.第二:只能往壓縮包里方一個文件,而我寫了一個可以放文件夾的函數(shù)AddFolder,給大家?guī)砹藰O大的方便。
如有不足之處,請大家多多指出。
??2?//File?Reference
??3?using?System.IO;
??4?//Zip?Component?Reference
??5?using?ICSharpCode.SharpZipLib.Checksums;
??6?using?ICSharpCode.SharpZipLib.Zip;
??7?using?ICSharpCode.SharpZipLib.Zip.Compression;
??8?using?ICSharpCode.SharpZipLib.Zip.Compression.Streams;
??9?
?10?//test
?11?using?System.Web;
?12?
?13?namespace?ZipUnzip
?14?{
?15??///?<summary>
?16??///?ZipUnzip?主要用于在線壓縮和解壓縮文件
?17??///?</summary>
?18??public?class?ZipUnzip
?19??{
?20???#region?定義私有變量
?21????private?Crc32?crc;
?22????private?ZipOutputStream?s;
?23????private?string?errorString;
?24???#endregion
?25?
?26???public?ZipUnzip()
?27???{
?28???}
?29?
?30???///?<summary>
?31???///?開始壓縮并設(shè)置壓縮文件(.zip)路徑
?32???///?</summary>
?33???///?<param?name="fileName">fileName</param>
?34???public?void?BeginZip(?string?fileName?)
?35???{
?36????crc?=?new?Crc32();
?37????s?=?new?ZipOutputStream(?File.Create(?fileName?)?);
?38???}
?39?
?40???///?<summary>
?41???///?結(jié)束壓縮
?42???///?</summary>
?43???public?void?FinishZip()
?44???{
?45????s.Finish();
?46????s.Close();
?47???}
?48???
?49???///?<summary>
?50???///?添加文件到壓縮文件
?51???///?</summary>
?52???///?<param?name="fileName"></param>
?53???///?<param?name="zipName"></param>
?54???///?<returns>錯誤原因</returns>
?55???public?string?AddZip(?string?fileName,?string?zipName?)
?56???{
?57????try
?58????{
?59?????FileStream?fs?=?File.OpenRead(?fileName?);
?60?????byte[]?buffer?=?new?byte[fs.Length];
?61?????fs.Read(?buffer,?0,?buffer.Length?);
?62?????fileName?=?Path.GetFileName(?fileName?);
?63?????long?fileLength?=?fs.Length;
?64?????fs.Close();
?65?????
?66?????ZipEntry?entry?=?new?ZipEntry(?zipName?);
?67?????entry.DateTime?=?DateTime.Now;
?68?????entry.Size?=?fileLength;
?69?
?70?????crc.Reset();
?71?????crc.Update(?buffer?);
?72?????entry.Crc?=?crc.Value;
?73?????s.PutNextEntry(?entry?);
?74?????s.Write(?buffer,?0,?buffer.Length?);
?75?
?76?????return?string.Empty;
?77????}
?78????catch(?Exception?addEx?)
?79????{
?80?????errorString?=?addEx.Message;
?81?????this.FinishZip();
?82?????return?addEx.Message;
?83????}
?84????}
?85?
?86???public?void?AddFolder(?string?folderName,?string?zipName?)
?87???{
?88????if(?Directory.Exists(?folderName?)?)
?89????{
?90?????foreach(?string?str?in?Directory.GetFileSystemEntries(?folderName?)?)
?91??????{
?92???????if(?File.Exists(?str?)?)
?93????????AddZip(?str,?zipName?+?"\\"?+?str.Substring(?str.LastIndexOf(?"\\"?)?+?1?)?);
?94???????else
?95????????AddFolder(?str,?zipName?+?"\\"?+?str.Substring(?str.LastIndexOf(?"\\"?)?+?1?)?);
?96??????}
?97????}
?98???}
?99???
100???///?<summary>
101???///?獲得錯誤原因
102???///?</summary>
103???///?<returns></returns>
104???public?string?GetErrorString()
105???{
106????return?errorString;
107???}
108??}
109?}
110?
111?Class?Finish
112?
113?以下為調(diào)用方法
114?using?System;
115?using?System.Collections;
116?using?System.ComponentModel;
117?using?System.Data;
118?using?System.Drawing;
119?using?System.Web;
120?using?System.Web.SessionState;
121?using?System.Web.UI;
122?using?System.Web.UI.WebControls;
123?using?System.Web.UI.HtmlControls;
124?
125?using?System.IO;
126?
127?using?ICSharpCode.SharpZipLib.Checksums;
128?using?ICSharpCode.SharpZipLib.Zip;
129?using?ICSharpCode.SharpZipLib.Zip.Compression;
130?using?ICSharpCode.SharpZipLib.Zip.Compression.Streams;
131?
132?namespace?ZipUnzip
133?{
134??///?<summary>
135??///?WebForm1?的摘要說明。
136??///?</summary>
137??public?class?WebForm1?:?System.Web.UI.Page
138??{
139???protected?System.Web.UI.WebControls.TextBox?TextBox1;
140???protected?System.Web.UI.WebControls.TextBox?TextBox2;
141???protected?System.Web.UI.WebControls.Button?Button2;
142??
143???private?void?Page_Load(object?sender,?System.EventArgs?e)
144???{
145????//?在此處放置用戶代碼以初始化頁面
146???}
147?
148???#region?Web?窗體設(shè)計器生成的代碼
149???override?protected?void?OnInit(EventArgs?e)
150???{
151????//
152????//?CODEGEN:?該調(diào)用是?ASP.NET?Web?窗體設(shè)計器所必需的。
153????//
154????InitializeComponent();
155????base.OnInit(e);
156???}
157???
158???///?<summary>
159???///?設(shè)計器支持所需的方法?-?不要使用代碼編輯器修改
160???///?此方法的內(nèi)容。
161???///?</summary>
162???private?void?InitializeComponent()
163???{????
164????this.Button2.Click?+=?new?System.EventHandler(this.Button2_Click);
165????this.Load?+=?new?System.EventHandler(this.Page_Load);
166?
167???}
168???#endregion
169?
170???private?void?Button1_Click(object?sender,?System.EventArgs?e)
171???{
172?
173???}
174?
175???private?void?Button2_Click(object?sender,?System.EventArgs?e)
176???{
177????ZipUnzip?zuz?=?new?ZipUnzip();
178????zuz.BeginZip(?Server.MapPath(?"."?)?+?"\\"?+?"Temp.zip"?);
179????zuz.AddFolder(?Server.MapPath(?"."?)?+?"\\"?+?"C301-K",?"C301-M"?);
180????zuz.FinishZip();
181???}
182?
183?
184??}
185?}
186?
187?
由于時間倉促,所以沒有時間整理,日后一定理順程序。
轉(zhuǎn)載于:https://www.cnblogs.com/liguancong/archive/2005/10/29/264344.html
總結(jié)
以上是生活随笔為你收集整理的文件 在线压缩 技术的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DataGrid 完全攻略之二(把数据导
- 下一篇: 丢失__EVENTTARGET _dop