程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> 利用ICSharpCode壓縮打包文件

利用ICSharpCode壓縮打包文件

編輯:關於.NET

因為項目需要打包文件,就在同事的建議下用ICSharpCode寫了個打包函數.ICSharpCode的介紹就不說了.具體請到官方網站 http://www.icsharpcode.net/ 上了解.

首先引用ICSharpCode.SharpZipLib.dll,沒有在這裡下載:http://files.cnblogs.com/KenBlove/ICSharpCode.SharpZipLib.rar

代碼實現多文件,自定義文件,整目錄打包等功能.好了..奉上代碼:

壓縮打包代碼

/// <summary>
    /// Zips the specified zip path.
    /// </summary>
    /// <param name="strZipPath">The zip path.</param>
    /// <param name="strZipTopDirectoryPath">The zip top directory path.</param>
    /// <param name="intZipLevel">The zip level.</param>
    /// <param name="strPassword">The password.</param>
    /// <param name="filesOrDirectoriesPaths">The files or directories paths.</param>
    /// <returns></returns>
    private bool Zip(string strZipPath, string strZipTopDirectoryPath, int intZipLevel, string strPassword, string[] filesOrDirectoriesPaths)
    {
        try
        {
            List<string> AllFilesPath = new List<string>();
            if (filesOrDirectoriesPaths.Length > 0) // get all files path
            {
                for (int i = 0; i < filesOrDirectoriesPaths.Length; i++)
                {
                    if (File.Exists(filesOrDirectoriesPaths[i]))
                    {
                        AllFilesPath.Add(filesOrDirectoriesPaths[i]);
                    }
                    else if (Directory.Exists(filesOrDirectoriesPaths[i]))
                    {
                        GetDirectoryFiles(filesOrDirectoriesPaths[i], AllFilesPath);
                    }
                }
            }
            if (AllFilesPath.Count > 0)
            {
                ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(strZipPath));
                zipOutputStream.SetLevel(intZipLevel);
                zipOutputStream.Password = strPassword;
                for (int i = 0; i < AllFilesPath.Count; i++)
                {
                    string strFile = AllFilesPath[i].ToString();
                    try
                    {
                        if (strFile.Substring(strFile.Length - 1) == "\\") //folder
                        {
                            string strFileName = strFile.Replace(strZipTopDirectoryPath, "");
                            if (strFileName.StartsWith("\\"))
                            {
                                strFileName = strFileName.Substring(1);
                            }
                            ZipEntry entry = new ZipEntry(strFileName);
                            entry.DateTime = DateTime.Now;
                            zipOutputStream.PutNextEntry(entry);
                        }
                        else //file
                        {
                            FileStream fs = File.OpenRead(strFile);
                            byte[] buffer = new byte[fs.Length];
                            fs.Read(buffer, 0, buffer.Length);
                            string strFileName = strFile.Replace(strZipTopDirectoryPath, "");
                            if (strFileName.StartsWith("\\"))
                            {
                                strFileName = strFileName.Substring(1);
                            }
                            ZipEntry entry = new ZipEntry(strFileName);
                            entry.DateTime = DateTime.Now;
                            zipOutputStream.PutNextEntry(entry);
                            zipOutputStream.Write(buffer, 0, buffer.Length);
                            fs.Close();
                            fs.Dispose();
                        }
                    }
                    catch
                    {
                        continue;
                    }
                }
                zipOutputStream.Finish();
                zipOutputStream.Close();
                return true;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }
    }
    /// <summary>
    /// Gets the directory files.
    /// </summary>
    /// <param name="strParentDirectoryPath">The parent directory path.</param>
    /// <param name="AllFilesPath">All files path.</param>
    private void GetDirectoryFiles(string strParentDirectoryPath, List<string> AllFilesPath)
    {
        string[] files = Directory.GetFiles(strParentDirectoryPath);
        for (int i = 0; i < files.Length; i++)
        {
            AllFilesPath.Add(files[i]);
        }
        string[] directorys = Directory.GetDirectories(strParentDirectoryPath);
        for (int i = 0; i < directorys.Length; i++)
        {
            GetDirectoryFiles(directorys[i], AllFilesPath);
        }
        if (files.Length == 0 && directorys.Length == 0) //empty folder
        {
            AllFilesPath.Add(strParentDirectoryPath);
        }
    }

調用也很簡單:

Code

string strZipPath = @"C:\Documents and Settings\ken\Desktop\Task1\ZipTest.zip";
string strZipTopDirectoryPath = @"C:\Documents and Settings\ken\Desktop\Task1\";
int intZipLevel = 6;
string strPassword = "";
string[] filesOrDirectoriesPaths = new string[] { @"C:\Documents and Settings\ken\Desktop\Task1\zipdemo\11\", 
            @"C:\Documents and Settings\ken\Desktop\Task1\zipdemo\Bin\ICSharpCode.SharpZipLib.dll" };
Zip(strZipPath, strZipTopDirectoryPath, intZipLevel, strPassword, filesOrDirectoriesPaths);

就這樣.如果需要盡管拿走.

(代碼參考過網絡上的資源,如有冒犯,莫怪莫怪~)

由於上邊代碼對於大文件是一次讀入,所以遇到大文件的時候是占用資源比較緊張。所以改為分段讀取,每次讀取64K數據:

string fileName = file.Replace(zipTopDirectory, "");
if (fileName.StartsWith("\\"))
fileName = fileName.Substring(1);
ZipEntry entry = new ZipEntry(fileName);
entry.DateTime = DateTime.Now;
zipedStream.PutNextEntry(entry);
for (long j = 0; j < fs.Length; j += 65536)
{
int byteLength = 65536;
if ((fs.Length - j) < 65536)
{
byteLength = (int)(fs.Length - j);
}

byte[] buffer = new byte[byteLength];
fs.Read(buffer, 0, byteLength);
zipedStream.Write(buffer, 0, byteLength);
}
fs.Close();

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved