close
Create a Zip with full control over contents
新增一個類別庫專案: ZipTool
ZipTool專案
加入參考
Zip壓縮類別
1: public class Zip
2: {3: /// <summary>
4: /// 創建壓縮檔案
5: /// </summary>
6: /// <param name="outPathname">縮檔案完整路徑</param>
7: /// <param name="password">密碼</param>
8: /// <param name="folderName">壓縮目錄</param>
9: public void CreateZipFile(string outPathname, string password, string folderName)
10: { 11: FileStream fsOut = File.Create(outPathname);12: ZipOutputStream zipStream = new ZipOutputStream(fsOut);
13: 14: zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
15: 16: zipStream.Password = password; // optional. Null is the same as not setting. Required if using AES.
17: 18: // This setting will strip the leading part of the folder path in the entries, to
19: // make the entries relative to the starting folder.
20: // To include the full path for each entry up to the drive root, assign folderOffset = 0.
21: int folderOffset = folderName.Length + (folderName.EndsWith("\\") ? 0 : 1);
22: 23: CompressFolder(folderName, zipStream, folderOffset); 24: 25: zipStream.IsStreamOwner = true; // Makes the Close also Close the underlying stream 26: zipStream.Close(); 27: } 28: 29: /// <summary> 30: /// 壓縮資料夾包含子資料夾 31: /// </summary>32: /// <param name="path"></param>
33: /// <param name="zipStream"></param>
34: /// <param name="folderOffset"></param>
35: private void CompressFolder(string path, ZipOutputStream zipStream, int folderOffset) 36: { 37: 38: string[] files = Directory.GetFiles(path); 39: 40: foreach (string filename in files) 41: { 42: 43: FileInfo fi = new FileInfo(filename); 44: 45: string entryName = filename.Substring(folderOffset); // Makes the name in zip based on the folder 46: entryName = ZipEntry.CleanName(entryName); // Removes drive from name and fixes slash direction 47: ZipEntry newEntry = new ZipEntry(entryName); 48: newEntry.DateTime = fi.LastWriteTime; // Note the zip format stores 2 second granularity 49: 50: // Specifying the AESKeySize triggers AES encryption. Allowable values are 0 (off), 128 or 256. 51: // A password on the ZipOutputStream is required if using AES. 52: // newEntry.AESKeySize = 256; 53: 54: // To permit the zip to be unpacked by built-in extractor in WinXP and Server2003, WinZip 8, Java, and other older code, 55: // you need to do one of the following: Specify UseZip64.Off, or set the Size. 56: // If the file may be bigger than 4GB, or you do not need WinXP built-in compatibility, you do not need either, 57: // but the zip will be in Zip64 format which not all utilities can understand. 58: // zipStream.UseZip64 = UseZip64.Off; 59: newEntry.Size = fi.Length; 60: 61: zipStream.PutNextEntry(newEntry); 62: 63: // Zip the file in buffered chunks64: // the "using" will close the stream even if an exception occurs
65: byte[] buffer = new byte[4096];
66: using (FileStream streamReader = File.OpenRead(filename))
67: { 68: StreamUtils.Copy(streamReader, zipStream, buffer); 69: } 70: zipStream.CloseEntry(); 71: } 72: string[] folders = Directory.GetDirectories(path); 73: foreach (string folder in folders) 74: { 75: CompressFolder(folder, zipStream, folderOffset); 76: } 77: } 78: }Zip壓縮工廠: 負責創建一個Zip instance
1: /// <summary>
2: /// 壓縮工廠
3: /// </summary>
4: public class ZipFactory
5: {6: public static Zip GetInstance()
7: {8: return new Zip();
9: } 10: }新增一個客戶端應用程式: ZipToolWinApp
新增Windows Forms App專案: ZipToolWinApp
加入參考: ZipTool
介面如下:
壓縮特定資料夾(inFolder),壓縮檔名(fileName),輸出至資料夾(outFolder),同時加上密碼(passwd),壓縮等級level:1~9 最高(9)
1: private void button1_Click(object sender, EventArgs e)
2: {3: string fileName = fileNameTBox.Text;
4: string outFolder = outFolderTBox.Text;
5: string outFilePath = string.Format(@"{0}\{1}", outFolder, fileName);
6: string inFolder = inFolderTBox.Text;
7: string passwd = passwdTBox.Text;
8: int level = Convert.ToInt16(levelNud.Value);
9: ZipFactory.GetInstance().CreateZipFile(outFilePath, passwd, inFolder, level); 10: }
Download: source code
參考資料
全站熱搜







留言列表
