C#压缩与解压缩GZip通用类

  注意,事先需要引入ICSharpCode.SharpZipLib.dll,该dll在网上到处都有,下载即可
1
using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.IO; 5 using ICSharpCode.SharpZipLib.Zip; 6 using System.Threading; 7 8 namespace lpUpdate 9 { 10 class GZip 11 { 12 /// <summary> 13 /// 打包文件 14 /// </summary> 15 /// <param name="filenames">要打包的文件列表 </param> 16 /// <param name="GzipFileName">目标文件名 </param> 17 /// <param name="CompressionLevel">压缩品质级别(0~9) </param> 18 /// <param name="SleepTimer">休眠时间(单位毫秒) </param> 19 public static void DoZip(List<FileInfo> filenames, string GzipFileName, int CompressionLevel, int SleepTimer) 20 { 21 22 ZipOutputStream s = new ZipOutputStream(File.Create(GzipFileName)); 23 try 24 { 25 s.SetLevel(CompressionLevel); //0 - store only to 9 - means best compression 26 27 foreach (FileInfo file in filenames) 28 { 29 FileStream fs = null; 30 try 31 { 32 fs = file.Open(FileMode.Open, FileAccess.ReadWrite); 33 } 34 catch 35 { continue; } 36 37 byte[] buffer = new byte[fs.Length]; 38 fs.Read(buffer, 0, buffer.Length); 39 40 ZipEntry entry = new ZipEntry(Path.GetFileName(file.Name)); 41 entry.DateTime = (file.CreationTime > file.LastWriteTime ? file.LastWriteTime : file.CreationTime); 42 43 s.PutNextEntry(entry); 44 45 s.Write(buffer, 0, buffer.Length); 46 47 48 fs.Close(); 49 file.Delete(); 50 51 if (SleepTimer > 10) 52 Thread.Sleep(SleepTimer); 53 } 54 } 55 finally 56 { 57 s.Finish(); 58 s.Close(); 59 } 60 } 61 /// <summary> 62 /// 打包文件 63 /// </summary> 64 /// <param name="filenames">要打包的文件列表 </param> 65 /// <param name="GzipFileName">目标文件名 </param> 66 /// <param name="CompressionLevel">压缩品质级别(0~9) </param> 67 /// <param name="SleepTimer">休眠时间(单位毫秒) </param> 68 public static void DoZip(List<string> filenames, string GzipFileName, int CompressionLevel, int SleepTimer) 69 { 70 ZipOutputStream s = new ZipOutputStream(File.Create(GzipFileName)); 71 try 72 { 73 s.SetLevel(CompressionLevel); //0 - store only to 9 - means best compression 74 75 foreach (string filename in filenames) 76 { 77 FileInfo file = new FileInfo(filename); 78 FileStream fs = null; 79 try 80 { 81 fs = file.Open(FileMode.Open, FileAccess.ReadWrite); 82 } 83 catch 84 { continue; } 85 86 byte[] buffer = new byte[fs.Length]; 87 fs.Read(buffer, 0, buffer.Length); 88 89 ZipEntry entry = new ZipEntry(Path.GetFileName(file.Name)); 90 entry.DateTime = (file.CreationTime > file.LastWriteTime ? file.LastWriteTime : file.CreationTime); 91 92 s.PutNextEntry(entry); 93 94 s.Write(buffer, 0, buffer.Length); 95 96 97 fs.Close(); 98 file.Delete(); 99 100 if (SleepTimer > 10) 101 Thread.Sleep(SleepTimer); 102 } 103 } 104 finally 105 { 106 s.Finish(); 107 s.Close(); 108 } 109 } 110 /// <summary> 111 /// 解压缩文件 112 /// </summary> 113 /// <param name="GzipFile">压缩包文件名 </param> 114 /// <param name="dPath">解压路径 </param> 115 public static void UnZip(string GzipFile, string dPath) 116 { 117 ZipInputStream s = new ZipInputStream(File.OpenRead(GzipFile)); 118 119 string directoryName = Path.GetDirectoryName(dPath + "\\") + "\\"; 120 121 if (!Directory.Exists(directoryName)) Directory.CreateDirectory(directoryName);//生成解压目录 122 string CurrentDirectory = directoryName; 123 124 byte[] data = new byte[2048]; 125 int size = 2048; 126 127 ZipEntry theEntry = null; 128 FileStream streamWriter = null; 129 while ((theEntry = s.GetNextEntry()) != null) 130 { 131 if (theEntry.IsDirectory) 132 {// 该结点是目录 133 if (!Directory.Exists(CurrentDirectory + theEntry.Name)) Directory.CreateDirectory(CurrentDirectory + theEntry.Name); 134 } 135 else 136 { 137 if (theEntry.Name != String.Empty) 138 { 139 //解压文件到指定的目录 140 streamWriter = File.Create(CurrentDirectory + theEntry.Name); 141 while (true) 142 { 143 size = s.Read(data, 0, data.Length); 144 if (size <= 0) break; 145 146 streamWriter.Write(data, 0, size); 147 } 148 streamWriter.Close(); 149 } 150 } 151 } 152 s.Close(); 153 } 154 } 155 }

posted on 2012-05-16 10:13  comcyd  阅读(539)  评论(0)    收藏  举报

导航