轉自:http://blog.csdn.net/yanlele424/article/details/6895986html
這段時間一直在作一個網站,其中遇到了一個問題,就是在服務器端壓縮多個服務器端的文件,而後在供客戶下載。說白了就是用戶上傳了多個文件,而後別的用戶能夠點擊批量下載這些文件。我要作的就是實現把這些文件壓縮以後供用戶下載。 我首先想到的是.Net提供的GZipStream類,翻了一下書才發現GZipStream沒有提供加壓多個文件的方法,須要本身定義,這樣解壓也只有使用本身的程序才能夠。這不是我想要的效果,放棄這個方案。因爲以前沒有接觸過這方面的技術,只有在網上找,結果找到多種解決的方案,大部分的方法都要使用外部的類庫。這裏列出來一個最經常使用的,以備之後查找方便: SharpZipLib (參見文章http://www.cnblogs.com/tuyile006/archive/2008/04/25/1170894.html)java
使用ICSharpCode.SharpZipLib.dll; 下載地址 http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspxweb
下面是對#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]));
-
-
-
- FileStream fs = File.Create(Path.GetFileNameWithoutExtension(args[1]));
-
-
- int size = 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"));
-
-
- FileStream fs = File.OpenRead(args[0]);
- /生成一個文件流,它用來打開要壓縮的文件
-
- 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得來全不費功夫啊
),貼出來找到的代碼,你們共同窗習。app
- using System;
- using System.Collections;
- using java.util;
- using java.util.zip;
-
- namespace CsZip
- {
- public delegate Enumeration EnumerationMethod();
-
-
-
-
- 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;
- }
-
-
- 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;
- }
-
-
- public IEnumerator GetEnumerator()
- {
- return new EnumerationWrapper(m_Method);
- }
- }
-
- public delegate bool FilterEntryMethod(ZipEntry e);
-
-
-
-
- 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();
- }
- }
- }
- }
- }
-
-
-
-
-
-
- public static ZipFile CreateEmptyZipFile(string fileName)
- {
- new ZipOutputStream(new java.io.FileOutputStream(fileName)).close();
- return new ZipFile(fileName);
- }
-
-
-
-
-
-
-
-
- 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);
-
- 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();
-
-
- 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.aspxoop
使用體驗:能夠照着例子實現簡單的加壓解壓,能夠加壓一個文件夾中的全部文件,但沒有提供加壓子文件夾的說明。 目前網上的一些代碼有的沒法加壓空文件夾,有的加壓了用rar解不開,這是一點須要改進的。 但若是隻須要加壓文件夾第一級子目錄中的「文件」(不包括文件夾和子目錄)的狀況,使用這個庫是很方便的。並且是正常zip格式。 比.Net提供的GZipStream類強在它能夠按照標準zip格式加壓多個文件,而GZipStream沒有提供加壓多個文件的方法,須要本身定義, 這樣解壓也只有使用本身的程序才能夠,通用性方面不如SharpZipLib。學習
- #region 加壓解壓方法
-
-
-
-
-
-
-
- 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;
- }
-
- 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;
- }
-
-
-
-
-
-
-
-
- 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;
- }
- }
- }
- }
- }
- }
- }
- catch (Exception ex)
- {
- err = ex.Message;
- return false;
- }
- return true;
- }
- #endregion
須要添加對SharpZipLib的引用:網站
using ICSharpCode.SharpZipLib.Zip;