C#操作壓縮包_第1頁(yè)
C#操作壓縮包_第2頁(yè)
C#操作壓縮包_第3頁(yè)
C#操作壓縮包_第4頁(yè)
C#操作壓縮包_第5頁(yè)
已閱讀5頁(yè),還剩7頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、A 2.0 C#實(shí)現(xiàn)壓縮/解壓功能 (示例代碼下載 收藏(一. 實(shí)現(xiàn)功能對(duì)文件及目錄的壓縮及解壓功能(二. 運(yùn)行圖片示例(三.代碼1. 壓縮類1/*/ 2/ 壓縮類3/ 4 public class ZipClass 5 6 public static void ZipFile( string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize 7 8 / 如果文件沒(méi)有找到,則報(bào)錯(cuò) 9 if (!System.IO.File.Exists(FileToZip 10 11 throw new System.IO.Fil

2、eNotFoundException(" 指定要壓縮的文件 : " + FileToZip + " 不存在 !" 12 13 14 System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read; 15 System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile; 16 ZipOutputStream ZipSt

3、ream = new ZipOutputStream(ZipFile; 17 ZipEntry ZipEntry = new ZipEntry("ZippedFile" 18 ZipStream.PutNextEntry(ZipEntry; 19 ZipStream.SetLevel(CompressionLevel; 20 byte buffer = new byte BlockSize; 21 System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length; 22 ZipStream.Write(buffer,

4、 0, size; 23 try 24 25 while 26 27 int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length; 28 ZipStream.Write(buffer, 0, sizeRead; 29 size += sizeRead; 30 31 32 catch (System.Exception ex 33 34 throw ex; 35 36 ZipStream.Finish(; 37 ZipStream.Close(; 38 StreamToZip.Close(; 39 40 41 /*/ / 42 / 壓縮目錄4

5、3 / 44 / 數(shù)組 ( 數(shù)組 0: 要壓縮的目錄 ; 數(shù)組 1: 壓縮的文件名 45 public static void ZipFileDictory( string args 46 47 string filenames = Directory.GetFiles(args0; 48 49 Crc32 crc = new Crc32(; 50 ZipOutputStream s = new ZipOutputStream(File.Create(args1; 51 s.SetLevel(6; 52 foreach ( string file in filenames 53 54 / 打開

6、壓縮文件 55 FileStream fs = File.OpenRead(file; 56 57 byte buffer = new byte fs.Length; 58 fs.Read(buffer, 0, buffer.Length; 59 ZipEntry entry = new ZipEntry(file; 60 61 entry.DateTime = DateTime.Now; 62 63 entry.Size = fs.Length; 64 fs.Close(; 65 66 crc.Reset(; 67 crc.Update(buffer; 68 69 entry.Crc = c

7、rc.Value; 70 71 s.PutNextEntry(entry; 72 73 s.Write(buffer, 0, buffer.Length; 74 75 76 77 s.Finish(; 78 s.Close(; 79 80 81 /*/ / 82 / 壓縮文件83 / 84 / 要進(jìn)行壓縮的文件名 85 / 壓縮后生成的壓縮文件名 86 public static void ZipFile( string FileToZip, string ZipedFile 87 88 / 如果文件沒(méi)有找到,則報(bào)錯(cuò) 89 if (!File.Exists(FileToZip 90 91 th

8、row new System.IO.FileNotFoundException(" 指定要壓縮的文件 : " + FileToZip + " 不存在 !" 92 93 FileStream fs = File.OpenRead(FileToZip; 94 byte buffer = new byte fs.Length; 95 fs.Read(buffer, 0, buffer.Length; 96 fs.Close(; 97 98 FileStream ZipFile = File.Create(ZipedFile; 99 ZipOutputStrea

9、m ZipStream = new ZipOutputStream(ZipFile; 100 ZipEntry ZipEntry = new ZipEntry("ZippedFile" 101 ZipStream.PutNextEntry(ZipEntry; 102 ZipStream.SetLevel(6; 103 104 ZipStream.Write(buffer, 0, buffer.Length; 105 ZipStream.Finish(; 106 ZipStream.Close(; 107 108 109 110 /*/ / 111/ 解壓類112/ 113

10、public class UnZipClass 114 115 /*/ / 116 / 解壓功能(解壓壓縮文件到指定目錄117 / 118 / 待解壓的文件 119 / 指定解壓目標(biāo)目錄 120 public static void UnZip( string args 121 122 ZipInputStream s = new ZipInputStream(File.OpenRead(args0.Trim(; 123 ZipEntry theEntry; 124 string directoryName = Path.GetDirectoryName(args1.Trim(; 125 12

11、6 if (!Directory.Exists(args1.Trim( 127 128 Directory.CreateDirectory(directoryName; 129 130 while (theEntry = s.GetNextEntry( != null 131 132 ; 133 string fileName = Path.GetFileName(theEntry.Name; 134 135 if 136 137 FileStream streamWriter = File.Create(args1.Trim( + fileName; 138 139 int size = 2

12、048; 140 byte data = new byte 2048; 141 while ( true 142 143 size = s.Read(data, 0, data.Length; 144 if (size > 0 145 146 streamWriter.Write(data, 0, size; 147 148 else 149 150 break ; 151 152 153 154 streamWriter.Close(; 155 156 157 s.Close(; 158 159 2.前臺(tái)頁(yè)面代碼1 2 3 4   5 Height="44px&qu

13、ot; Text=" 壓縮文件 / 文件夾示例 " Width="366px"> 6 7 8 9 16 24 25 26 34 42 43 44 45 10 壓縮目錄 (from/to:" Width="153px"> 11 12 13  14 壓縮目錄 " />15 17 解壓目錄 (from/to:" Width="154px">18 19 20 21  22 解壓目錄 " OnClick="btUnZipDictor

14、y_Click" />23 27 壓縮文件 (from/to:" Width="153px">28 29 30 31  32 壓縮文件 " OnClick="btZipFile_Click" />33 35 解壓文件 (from/to:" Width="154px">36 37 38 39  40 解壓文件 " OnClick="btUnZipFile_Click" />41 46 47 48 49 50 51 52

15、 53 3.后臺(tái)頁(yè)面代碼1public partial class _Default : System.Web.UI.Page 23 protected void Page_Load(object sender, EventArgs e4 56 7 protected void btZipDictory_Click(object sender, EventArgs e8 9 string FilePathS = new string2;10 FilePathS0 = TextBox1.Text.Trim(; /待壓縮的文件目錄11 FilePathS1 = TextBox2.Text.Trim

16、(; /生成的目標(biāo)文件 12 ZipClass.ZipFileDictory(FilePathS;13 14 protected void btUnZipDictory_Click(object sender, EventArgs e15 16 string FilePathS = new string2;17 FilePathS0 = TextBox3.Text.Trim(; /待解壓的文件18 FilePathS1 = TextBox4.Text.Trim(; /解壓目標(biāo)存放目錄19 UnZipClass.UnZip(FilePathS;20 21 protected void btZipFile_Click(object sender, EventArgs e22 23 string FilePathS = new string2;24 FilePathS0 = TextBox5.Text.Trim(; /待壓縮的文件25 FilePathS1 = TextBox6.Text.Trim(; /生成的壓縮文件名26 ZipClass

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論