C#圧縮、解凍

22324 ワード

1 /// 2 /// FNameArry : , strZipName 3 /// 4 /// 5 /// 6 public void ZipFile(string[] FNameArry string strZipName) 7 { 8 9 10 if (strZipName.Length != 0) // 11 { 12 ZipOutputStream u = new ZipOutputStream(File.Create(FileDir + strZipName)); // “ZipOutputStream” 13 for (int i = 0; i < FNameArry.Length; i++) 14 { 15 if (FNameArry[i] != "") // 16 { 17 this.AddZipEntry(FNameArry[i] u out u); // 18 } 19 } 20 u.Finish(); // 21 u.Close(); 22 } 23 } 24 25 26 27 28 29 30 // :p ; u ZipOutputStream; out j “ZipEntry” “ZipOutputStream” 31 public void AddZipEntry(string p ZipOutputStream u out ZipOutputStream j) 32 { 33 string s = FileDir + p; 34 35 36 if (Directory.Exists(s)) // 37 { 38 DirectoryInfo di = new DirectoryInfo(s); 39 40 41 //*********** *********** 42 43 44 if (di.GetDirectories().Length <= 0) // 45 { 46 ZipEntry z = new ZipEntry(p + "/"); // “/” 47 u.PutNextEntry(z); 48 } 49 50 51 //*************** *************** 52 53 54 55 56 foreach (DirectoryInfo tem in di.GetDirectories()) // 57 { 58 ZipEntry z = new ZipEntry(this.ShortDir(tem.FullName) + "/"); // “/” 59 u.PutNextEntry(z); // 60 s = this.ShortDir(tem.FullName); 61 this.AddZipEntry(s u out u); // 62 } 63 foreach (FileInfo temp in di.GetFiles()) // 64 { 65 s = this.ShortDir(temp.FullName); 66 this.AddZipEntry(s u out u); // 67 } 68 } 69 else if (File.Exists(s)) // 70 { 71 u.SetLevel(9); // 72 FileStream f = File.OpenRead(s); 73 byte[] b = new byte[f.Length]; 74 f.Read(b 0 b.Length); // 75 ZipEntry z = new ZipEntry(this.ShortDir(s)); 76 u.PutNextEntry(z); // 77 u.Write(b 0 b.Length); // 78 f.Close(); 79 } 80 j = u; // “ZipOutputStream” 81 } 82 83 84 85 86 87 88 89 90 /// 91 /// 92 /// 93 /// 94 public void UnZipFile(string[] FNameArry) // 95 { 96 int i2 = 0; // 97 for (int j = 0; j < FNameArry.Length; j++) 98 { 99 if (FNameArry[j] != "") 100 { 101 string un_time = System.DateTime.Now.ToShortDateString() + "-" + System.DateTime.Now.Hour.ToString() + "-" + System.DateTime.Now.Minute.ToString() + "-" + (System.DateTime.Now.Second + i2).ToString(); 102 string un_dir = FileDir + "Unzip-" + un_time; 103 Directory.CreateDirectory(un_dir); // 104 ZipInputStream f = new ZipInputStream(File.OpenRead(FileDir + FNameArry[j])); // , “ZipInputStream” 105 106 107 A: ZipEntry zp = f.GetNextEntry(); // 。 ( ): “ZipEntry” , 。 , 、 0 、“Crc” 00000000 “ ”。 , 。 108 109 110 while (zp != null) 111 { 112 string FNameArry2; 113 if (zp.Name.IndexOf("/") >= 0) // 114 { 115 int tmp1 = zp.Name.LastIndexOf("/"); 116 FNameArry2 = zp.Name.Substring(0 tmp1); 117 Directory.CreateDirectory(un_dir + "/" + FNameArry2 + "/"); // , --- (A) (B) 118 } 119 if (!zp.IsDirectory && zp.Crc != 00000000L) // “ZipEntry” “ ” 120 { 121 int i = 2048; 122 byte[] b = new byte[i]; // 2048 123 FileStream s = File.Create(un_dir + "/" + zp.Name); //(B)- 124 while (true) // , “ZipEntry” 125 { 126 i = f.Read(b 0 b.Length); // “ZipEntry” 127 if (i > 0) 128 { 129 s.Write(b 0 i); // 130 } 131 else 132 { 133 break; // 0 , 134 } 135 } 136 s.Close(); 137 } 138 goto A; // “ZipEntry” 139 } 140 f.Close(); 141 i2++; 142 } 143 } 144 }