程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> .Net中使用壓縮和解壓縮(三)

.Net中使用壓縮和解壓縮(三)

編輯:.NET實例教程
/// <summary>
        /// 處理在解壓縮時,如果在目標目錄下已經存在相同的文件,提示用戶是否替換已存在文件
        /// </summary>
        /// <param name="filename">解壓後文件</param>
        /// <returns>是否替換已存在的文件,是將替換,否不替換</returns>
        private bool _ProcessOverwrite(string filename)
        {
            bool isOverWrite = false;
            ZipHelpEventArgs e = new ZipHelpEventArgs(filename, isOverWrite);
            OnZipHelperProcessOverWrite(e);
            return e.IsOverWrite;
        }
        /// <summary>
        ///  處理在壓縮時,如果在目標目錄下已經存在相同的文件,提示用戶是否替換已存在的壓縮文件
        /// </summary>
        /// <param name="e"></param>
        private void _ProcessOverwrite(ZipHelpEventArgs e)
        {
            OnZipHelperProcessOverWrite(e);
        }
        #endregion
        #region 靜態方法
        /// <summary>
        /// 獲取單個文件壓縮後大小
        /// </summary>
        /// <param name="filename">要壓縮的文件路徑</param>
        /// <param name="tempDir">臨時目錄</param>
        /// <returns>文件大小(以字節為單位)</returns>
        public static long GetFileCompressedSize(string fileName, string tempDir)
        {
            long rv = 0;
            //以當前時間點為文件名,以防止文件沖突
            string targetFile = Path.Combine(tempDir, _GetCurrDatetime() + ".zip");

            //創建臨時壓縮文件
            ZipOutputStream zipOut = new ZipOutputStream(File.Create(targetFile));
            try
            {
                FileInfo fi = new FileInfo(fileName);
                ZipEntry entry = new ZipEntry(fi.Name);
                FileStream sReader = File.OpenRead(fileName);
                byte[] buff = new byte[Convert.ToInt32(sReader.Length)];
                sReader.Read(buff, 0, (int)sReader.Length);
                entry.Size = sReader.Length;
                sReader.Close();
                entry.DateTime = fi.LastWriteTime;
                zipOut.PutNextEntry(entry);
                zipOut.Write(buff, 0, buff.Length);
          &nbsp;     zipOut.Finish();
                zipOut.Close();
                if (File.Exists(targetFile))
                {
                    FileInfo fi2 = new FileInfo(targetFile);
                    rv = fi2.Length;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //刪除臨時文件
                if (File.Exists(targetFile))
                {
                    File.Delete(targetFile);
                }
            }
            return rv;
        }
        /// <summary>
        /// 獲取某文件列表壓縮後大小
     &nbsp;  /// </summary>
        /// <param name="filename">要壓縮的文件路徑數組</param>
        /// <param name="tempDir">臨時目錄</param>
        /// <returns>文件大小(以字節為單位)</returns>
        public static long GetFileCompressedSize(string[] fileList, string tempDir)
        {
            long rv = 0;
            //以當前時間點為文件名,以防止文件沖突
            string targetFile = Path.Combine(tempDir, _GetCurrDatetime() + ".zip");
            //創建臨時壓縮文件
            ZipOutputStream zipOut = new ZipOutputStream(File.Create(targetFile));
            try
            {
                foreach (string fName in fileList)
                {
                    FileInfo fi = new FileInfo(fName);
                    ZipEntry entry = new ZipEntry(fi.Name);
                    FileStream sReader = File.OpenRead(fName);
                    byte[] buff = new byte[Convert.ToInt32(sReader.Length)];
                    sReader.Read(buff, 0,(int)sReader.Length);
                    entry.Size = sReader.Length;
                    sReader.Close();
                    entry.DateTime = fi.LastWriteTime;
                    zipOut.PutNextEntry(entry);
                    zipOut.Write(buff, 0, buff.Length);
                }
                zipOut.Finish();
                zipOut.Close();
                if (File.Exists(targetFile))
                {
                    FileInfo fi2 = new FileInfo(targetFile);
                    rv = fi2.Length;
                }
            }
            catch (Exception ex)
            {
                throw ex;
        &nbsp;   }
            finally
            {
 &nbsp;              //刪除臨時文件
                if (File.Exists(targetFile))
                {
                    File.Delete(targetFile);
                }
            }
            return rv;
        }
        /// <summary>
        /// 獲取壓縮文件裡的文件列表
        /// </summary>
        /// <remarks>忽略壓縮文件中的目錄,只返回文件。</remarks>
        /// <param name="filename">壓縮文件路徑</param>
        /// <param name="tempDir">臨時目錄</param>
        /// <returns>文件名數組</returns>       
        public static string[] GetFileList(string fileName, string tempDir)
        {
            FastZip fz = new FastZip();
            fz.RestoreDateTimeOnExtract = true;
            string[] rv;
            //在當前程序路徑/Temp 下創建臨時目錄
            string tempdir = Path.Combine(tempDir, "tempzip");
            try
            {
      &nbsp;         fz.ExtractZip(fileName, tempdir, "");
                rv = Directory.GetFiles(tempdir);
                //刪除臨時文件
                Directory.Delete(tempdir, true);
            }
            catch (Exception ex)
            {
                rv = null;
                throw ex;
            }
            return rv;
        }

        /// <summary>
        /// 抽取壓縮文件裡的某個文件
        /// </summary>
        /// <param name="ZipFileName">壓縮文件路徑</param>
        /// <param name="zipFileName">壓縮文件路徑</param>
        /// <param name="extraFileName">要抽取的文件名</param>
        /// <param name="targetDir">抽取文件保存目錄</param>
        /// <param name="tempDir">臨時目錄</param>
        public static bool GetFile(string zipFileName, string extraFileName, string targetDir, string tempDir)
        {
            bool rv = false;
            FastZip fz = new FastZip();            fz.RestoreDateTimeOnExtract = true;
            string targetFileName = "";
            string[] tempfiles;
            FileInfo fi;
            //在當前程序路徑/temp 下 創建臨時目錄           
            string tempdir = Path.Combine(tempDir, "tempzip");
try
            {
                fz.ExtractZip(zipFileName, tempdir, "");
                tempfiles = Directory.GetFiles(tempdir);
                foreach (string fname in tempfiles)
                {
                    fi = new FileInfo(fname);
                    if (fi.Name.Equals(extraFileName))
                    {
                        targetFileName = Path.Combine(targetDir, fi.Name);
                        File.Move(fi.FullName, targetFileName);
                        break;
                    }
                }
                //刪除臨時目錄
                Directory.Delete(tempdir, true);
                //檢查目標文件是否已經創建
                if (File.Exists(targetFileName))
                    rv = true;
                else
                    rv = false;
            }
            catch (Exception ex)
            {
                rv = false;
                throw ex;
            }
            return rv;
        }
        /// <summary>
        /// 獲取當前時間點,精確到毫秒
        /// </summary>
        /// <returns>時間點字符串</returns>
        private static string _GetCurrDatetime()
        {
            DateTime dt = System.DateTime.Now;
     &nbsp;      string rv = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Second.ToString() + dt.Millisecond.ToString();
            return rv;
        }
        #endregion
        #endregion
    }     
   
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved