基於C# 生成Zip緊縮包代碼。本站提示廣大學習愛好者:(基於C# 生成Zip緊縮包代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是基於C# 生成Zip緊縮包代碼正文
緊縮包制造也是許多項目中須要用到的功效。好比有年夜量的文件(假定有10000個)須要上傳,1個1個的上傳仿佛不太靠譜(靠,那得傳到甚麼時刻啊?),這時候我們可以制造一個緊縮包zip,直接傳這個文件到辦事器端,然後在辦事器目次解壓,釋放外面的文件。
這裡我們選用ICSharpCode.SharpZipLib這個類庫來完成我們的需求。
ICSharpCode.SharpZipLib供給了兩個緊縮算法的完成,分離是BZIP2(緊縮時光長,緊縮率高)和GZIP(緊縮效力高,緊縮率低)。
先界說一個列舉,用於法式標示是哪一個緊縮算法。
/// <summary> /// 緊縮列舉 /// </summary> public enum ZipEnum { //緊縮時光長,緊縮率高 BZIP, //緊縮效力高,緊縮率低 GZIP }
單個文件的緊縮:
#region 制造緊縮包(單個文件緊縮) /// <summary> /// 制造緊縮包(單個文件緊縮) /// </summary> /// <param name="sourceFileName">原文件</param> /// <param name="zipFileName">緊縮文件</param> /// <param name="zipEnum">緊縮算法列舉</param> /// <returns>緊縮勝利標記</returns> public static bool ZipFile(string srcFileName, string zipFileName, ZipEnum zipEnum) { bool flag = true; try { switch (zipEnum) { case ZipEnum.BZIP: FileStream inStream = File.OpenRead(srcFileName); FileStream outStream = File.Open(zipFileName, FileMode.Create); //參數true表現緊縮完成後,inStream和outStream銜接都釋放 BZip.Compress(inStream, outStream, true, ); inStream.Close(); outStream.Close(); break; case ZipEnum.GZIP: FileStream srcFile = File.OpenRead(srcFileName); GZipOutputStream zipFile = newGZipOutputStream(File.Open(zipFileName, FileMode.Create)); byte[] fileData = new byte[srcFile.Length]; srcFile.Read(fileData, , (int)srcFile.Length); zipFile.Write(fileData, , fileData.Length); srcFile.Close(); zipFile.Close(); break; default: break; } } catch { flag = false; } return flag; }#endregion
單個文件的解緊縮:
#region 解緊縮包(單個文件解緊縮) /// <summary> /// 解緊縮包(單個文件解緊縮) /// </summary> /// <param name="zipFileName">緊縮文件</param> /// <param name="unzipFileName">解緊縮文件</param> /// <param name="zipEnum">緊縮算法列舉</param> /// <returns>緊縮勝利標記</returns> public static bool UnZipFile(string zipFileName, stringunzipFileName, ZipEnum zipEnum) { bool flag = true; try { switch (zipEnum) { case ZipEnum.BZIP: FileStream inStream = File.OpenRead(zipFileName); FileStream outStream = File.Open(unzipFileName, FileMode.Create); BZip.Decompress(inStream, outStream, true); break; case ZipEnum.GZIP: GZipInputStream zipFile = newGZipInputStream(File.OpenRead(zipFileName)); FileStream destFile = File.Open(unzipFileName, FileMode.Create); int bufferSize = * ; byte[] fileData = new byte[bufferSize]; while (bufferSize > ) { bufferSize = zipFile.Read(fileData, , bufferSize); zipFile.Write(fileData, , bufferSize); } destFile.Close(); zipFile.Close(); break; default: break; } } catch { flag = false; } return flag; } #endregion
下面的兩個辦法在援用了dll後,可以直接應用。
看到這裡,信任讀者有疑問了,假如我想讓多個文件緊縮到1個zip包裡呢?乃至可以給文件加密?給zip包加正文?
好吧,我這裡持續貼兩個辦法,都經由測試,可用。
制造緊縮包:
#region 制造緊縮包(多個文件緊縮到一個緊縮包,支撐加密、正文) /// <summary> /// 制造緊縮包(多個文件緊縮到一個緊縮包,支撐加密、正文) /// </summary> /// <param name="topDirectoryName">緊縮文件目次</param> /// <param name="zipedFileName">緊縮包文件名</param> /// <param name="compresssionLevel">緊縮級別 -</param> /// <param name="password">暗碼</param> /// <param name="comment">正文</param> public static void ZipFiles(string topDirectoryName, string zipedFileName, intcompresssionLevel, string password, string comment) { using (ZipOutputStream zos = newZipOutputStream(File.Open(zipedFileName, FileMode.OpenOrCreate))) { if (compresssionLevel != ) { zos.SetLevel(compresssionLevel);//設置緊縮級別 } if (!string.IsNullOrEmpty(password)) { zos.Password = password;//設置zip包加密暗碼 } if (!string.IsNullOrEmpty(comment)) { zos.SetComment(comment);//設置zip包的正文 } //輪回設置目次下一切的*.jpg文件(支撐子目次搜刮) foreach (string file inDirectory.GetFiles(topDirectoryName, "*.jpg", SearchOption.AllDirectories)) { if (File.Exists(file)) { FileInfo item = new FileInfo(file); FileStream fs = File.OpenRead(item.FullName); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, , buffer.Length); ZipEntry entry = new ZipEntry(item.Name); zos.PutNextEntry(entry); zos.Write(buffer, , buffer.Length); } } } } #endregion
解緊縮包:
#region 解緊縮包(將緊縮包解壓到指定目次) /// <summary> /// 解緊縮包(將緊縮包解壓到指定目次) /// </summary> /// <param name="zipedFileName">緊縮包稱號</param> /// <param name="unZipDirectory">解緊縮目次</param> /// <param name="password">暗碼</param> public static void UnZipFiles(string zipedFileName, string unZipDirectory, stringpassword) { using (ZipInputStream zis = newZipInputStream(File.Open(zipedFileName, FileMode.OpenOrCreate))) { if (!string.IsNullOrEmpty(password)) { zis.Password = password;//有加密文件的,可以設置暗碼解壓 } ZipEntry zipEntry; while ((zipEntry = zis.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(unZipDirectory); string pathName = Path.GetDirectoryName(zipEntry.Name); string fileName = Path.GetFileName(zipEntry.Name); pathName = pathName.WordStr(".", "$"); directoryName += "\\" + pathName; if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } if (!string.IsNullOrEmpty(fileName)) { FileStream fs = File.Create(Path.Combine(directoryName, fileName)); int size = ; byte[] bytes = new byte[]; while (true) { size = zis.Read(bytes, , bytes.Length); if (size > ) { fs.Write(bytes, , size); } else { break; } } fs.Close(); } } } } #endregion
挪用時我們可以這麼寫:
ZipFileUtil.ZipFiles(@"E:\\test\\", "E:\\test.zip", 1, "admin", "this is admin's comment.");//制造緊縮包 ZipFileUtil.UnZipFiles("E:\\test.zip", "E:\\guwei4037\\", "admin");//解緊縮包
以上內容是基於C# 生成Zip緊縮包代碼的相干引見,願望對年夜家有所贊助!