文件压缩与解压
1.文件壓縮事件
private void button1_Click(object sender, EventArgs e){string[] FileProperties = new string[2];FileProperties[0] = @"F:\sssd\";//待壓縮文件目錄FileProperties[1] = @"F:\sssd\\三國人物.zip"; //壓縮后的目標文件ZipClass Zc = new ZipClass();Zc.ZipFileMain(FileProperties);ZipClass.Zip(FileProperties[0], FileProperties[1]);}2.文件解壓事件
private void button2_Click(object sender, EventArgs e){string[] FileProperties = new string[2];FileProperties[0] = @"F:\sssd\\三國人物.zip";//待解壓的文件FileProperties[1] = @"F:\sssd\";//解壓后放置的目標目錄UnZipClass UnZc = new UnZipClass();UnZc.UnZip(FileProperties);} View Code3.ZipClass.cs(處理壓縮文件類)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using ICSharpCode.SharpZipLib.Zip; using System.IO; using ICSharpCode.SharpZipLib.Checksums; using System.Net.Mail; using System.Net;namespace 壓縮和解壓縮 {public class ZipClass{public void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize){//如果文件沒有找到,則報錯if (!System.IO.File.Exists(FileToZip)){throw new System.IO.FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd");}System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);ZipEntry ZipEntry = new ZipEntry("ZippedFile");ZipStream.PutNextEntry(ZipEntry);ZipStream.SetLevel(CompressionLevel);byte[] buffer = new byte[BlockSize];System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);ZipStream.Write(buffer, 0, size);try{while (size < StreamToZip.Length){int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);ZipStream.Write(buffer, 0, sizeRead);size += sizeRead;}}catch (System.Exception ex){throw ex;}ZipStream.Finish();ZipStream.Close();StreamToZip.Close();}public void ZipFileMain(string[] args){string[] filenames = Directory.GetFiles(args[0]);Crc32 crc = new Crc32();ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));s.SetLevel(6); // 0 - store only to 9 - means best compressionforeach (string file in filenames){//打開壓縮文件FileStream fs = File.OpenRead(file);byte[] buffer = new byte[fs.Length];fs.Read(buffer, 0, buffer.Length);ZipEntry entry = new ZipEntry(file);entry.DateTime = DateTime.Now;// set Size and the crc, because the information// about the size and crc should be stored in the header// if it is not set it is automatically written in the footer.// (in this case size == crc == -1 in the header)// Some ZIP programs have problems with zip files that don't store// the size and crc in the header.entry.Size = fs.Length;fs.Close();crc.Reset();crc.Update(buffer);entry.Crc = crc.Value;s.PutNextEntry(entry);s.Write(buffer, 0, buffer.Length);}s.Finish();s.Close();}/**//// <summary>/// 遞歸壓縮文件夾方法/// </summary>/// <param name="FolderToZip"></param>/// <param name="s"></param>/// <param name="ParentFolderName"></param>private static bool ZipFileDictory(string FolderToZip, ZipOutputStream s, string ParentFolderName){bool res = true;string[] folders, filenames;ZipEntry entry = null;FileStream fs = null;Crc32 crc = new Crc32();try{//創建當前文件夾entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/")); //加上 “/” 才會當成是文件夾創建 s.PutNextEntry(entry);s.Flush();//先壓縮文件,再遞歸壓縮文件夾 filenames = Directory.GetFiles(FolderToZip);foreach (string file in filenames){//打開壓縮文件fs = File.OpenRead(file);byte[] buffer = new byte[fs.Length];fs.Read(buffer, 0, buffer.Length);entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/" + Path.GetFileName(file)));entry.DateTime = DateTime.Now;entry.Size = fs.Length;fs.Close();crc.Reset();crc.Update(buffer);entry.Crc = crc.Value;s.PutNextEntry(entry);s.Write(buffer, 0, buffer.Length);}}catch{res = false;}finally{if (fs != null){fs.Close();fs = null;}if (entry != null){entry = null;}GC.Collect();GC.Collect();}folders = Directory.GetDirectories(FolderToZip);foreach (string folder in folders){if (!ZipFileDictory(folder, s, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip)))){return false;}}return res;}/**//// <summary>/// 壓縮目錄/// </summary>/// <param name="FolderToZip">待壓縮的文件夾,全路徑格式</param>/// <param name="ZipedFile">壓縮后的文件名,全路徑格式</param>/// <returns></returns>private static bool ZipFileDictory(string FolderToZip, string ZipedFile){bool res;if (!Directory.Exists(FolderToZip)){return false;}ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile));s.SetLevel(6);//s.Password = Password; res = ZipFileDictory(FolderToZip, s, "");s.Finish();s.Close();return res;}/**//// <summary>/// 壓縮文件/// </summary>/// <param name="FileToZip">要進行壓縮的文件名</param>/// <param name="ZipedFile">壓縮后生成的壓縮文件名</param>/// <returns></returns>private static bool ZipFile(string FileToZip, string ZipedFile){//如果文件沒有找到,則報錯if (!File.Exists(FileToZip)){throw new System.IO.FileNotFoundException("指定要壓縮的文件: " + FileToZip + " 不存在!");}//FileStream fs = null;FileStream ZipFile = null;ZipOutputStream ZipStream = null;ZipEntry ZipEntry = null;bool res = true;try{ZipFile = File.OpenRead(FileToZip);byte[] buffer = new byte[ZipFile.Length];ZipFile.Read(buffer, 0, buffer.Length);ZipFile.Close();ZipFile = File.Create(ZipedFile);ZipStream = new ZipOutputStream(ZipFile);// ZipStream.Password = Password;ZipEntry = new ZipEntry(Path.GetFileName(FileToZip));ZipStream.PutNextEntry(ZipEntry);ZipStream.SetLevel(6);ZipStream.Write(buffer, 0, buffer.Length);}catch{res = false;}finally{if (ZipEntry != null){ZipEntry = null;}if (ZipStream != null){ZipStream.Finish();ZipStream.Close();}if (ZipFile != null){ZipFile.Close();ZipFile = null;}GC.Collect();GC.Collect();}return res;}/**//// <summary>/// 壓縮文件 和 文件夾/// </summary>/// <param name="FileToZip">待壓縮的文件或文件夾,全路徑格式</param>/// <param name="ZipedFile">壓縮后生成的壓縮文件名,全路徑格式</param>/// <returns></returns>public static bool Zip(String FileToZip, String ZipedFile){if (Directory.Exists(FileToZip)){return ZipFileDictory(FileToZip, ZipedFile);}else if (File.Exists(FileToZip)){return ZipFile(FileToZip, ZipedFile);}else{return false;}}} } View Code4.UnZipClass.cs(處理解壓的文件類)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using ICSharpCode.SharpZipLib.Zip; using System.IO;namespace 壓縮和解壓縮 {public class UnZipClass{public void UnZip(string[] args){ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));ZipEntry theEntry;while ((theEntry = s.GetNextEntry()) != null){string directoryName = Path.GetDirectoryName(args[1]);string fileName = Path.GetFileName(theEntry.Name);//生成解壓目錄 Directory.CreateDirectory(directoryName);if (fileName != String.Empty){//解壓文件到指定的目錄FileStream streamWriter = File.Create(args[1] + fileName);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;}}streamWriter.Close();}}s.Close();}/**//// <summary>/// 解壓功能(解壓壓縮文件到指定目錄)/// </summary>/// <param name="FileToUpZip">待解壓的文件</param>/// <param name="ZipedFolder">指定解壓目標目錄</param>public static void UnZip(string FileToUpZip, string ZipedFolder){if (!File.Exists(FileToUpZip)){return;}if (!Directory.Exists(ZipedFolder)){Directory.CreateDirectory(ZipedFolder);}ZipInputStream s = null;ZipEntry theEntry = null;string fileName;FileStream streamWriter = null;try{s = new ZipInputStream(File.OpenRead(FileToUpZip));// s.Password = Password;while ((theEntry = s.GetNextEntry()) != null){if (theEntry.Name != String.Empty){fileName = Path.Combine(ZipedFolder, theEntry.Name);/**////判斷文件路徑是否是文件夾if (fileName.EndsWith("/") || fileName.EndsWith("\\")){Directory.CreateDirectory(fileName);continue;}streamWriter = File.Create(fileName);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;}}}}}finally{if (streamWriter != null){streamWriter.Close();streamWriter = null;}if (theEntry != null){theEntry = null;}if (s != null){s.Close();s = null;}GC.Collect();GC.Collect(1);}}} } View Code?
轉載于:https://www.cnblogs.com/zxbzl/p/3675322.html
總結
- 上一篇: Linux---Google Chrom
- 下一篇: linux各种压缩包使用方法