1 #region 将本地压缩文件转换为Base64编码 2 /// <summary> 3 /// 将本地压缩文件转换为Base64编码 4 /// </summary> 5 /// <returns></returns> 6 public string ReturnBase64Code() 7 { 8 //从服务器获取到的文件路径 9 string ys_filePath = rootDirectoryPath + @"\hx.zip"; 10 //将压缩文件转换为二进制 11 using (FileStream fs = new FileStream(ys_filePath, FileMode.Open)) 12 { 13 byte[] bytes = new byte[fs.Length]; 14 int count = Convert.ToInt32(fs.Length); 15 fs.Read(bytes, 0, count); 16 fs.Close(); 17 //将二进制转为为base64 18 return Convert.ToBase64String(bytes); 19 } 20 } 21 #endregion
1 #region 直接删除指定目录下的所有文件及文件夹(保留目录) 2 /// <summary> 3 /// 直接删除指定目录下的所有文件及文件夹(保留目录) 4 /// </summary> 5 /// <param name="strPath">文件夹路径</param> 6 /// <returns>执行结果</returns> 7 public bool DeleteDir(string strPath) 8 { 9 try 10 { 11 strPath = @strPath.Trim().ToString();// 清除空格 12 if (System.IO.Directory.Exists(strPath)) // 判断文件夹是否存在 13 { 14 // 获得文件夹数组 15 string[] strDirs = System.IO.Directory.GetDirectories(strPath); // 获得文件数组 16 string[] strFiles = System.IO.Directory.GetFiles(strPath); // 遍历所有子文件夹 17 foreach (string strFile in strFiles) 18 { 19 // 删除文件夹 20 System.IO.File.Delete(strFile); 21 } 22 // 遍历所有文件 23 foreach (string strdir in strDirs) 24 { 25 // 删除文件 26 System.IO.Directory.Delete(strdir, true); 27 } 28 } 29 return true; 30 } 31 catch (Exception Exp) // 异常处理 32 { 33 System.Diagnostics.Debug.Write(Exp.Message.ToString()); // 异常信息 34 return false; 35 } 36 } 37 #endregion
1 #region 定时下载广告文件 2 public void theout(object source, System.Timers.ElapsedEventArgs e) 3 { 4 string str = ReturnBase64Code();//将二进制转为为base64 5 6 byte[] outputb = Convert.FromBase64String(str);//将base64编码转换为二进制 7 8 //将二进制转换为压缩文件 9 string path = rootDirectoryPath + @"\new_hx.zip";//压缩文件的地址 10 File.WriteAllBytes(path, outputb); 11 12 //删除广告目录下已经存在的文件 13 if (Directory.Exists(filePath)) 14 { 15 DeleteDir(filePath); 16 } 17 //解压文件 18 bool result = bonkerZip.UnZipFile(path, filePath); // DeCompressionZip 19 if (!result) 20 { 21 ErrorLog.WriteErrorLog(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "广告文件解压失败,失败原因:"+bonkerZip.errorMsg); 22 } 23 webBrowser1.Navigate(filePath+@"\index.html"); 24 } 25 #endregion
1 public class BonkerZip 2 { 3 /// <summary> 4 /// 存放待压缩的文件的绝对路径 5 /// </summary> 6 private List<string> AbsolutePaths { set; get; } 7 public string errorMsg { set; get; } 8 9 public BonkerZip() 10 { 11 errorMsg = ""; 12 AbsolutePaths = new List<string>(); 13 } 14 /// <summary> 15 /// 添加压缩文件 16 /// </summary> 17 /// <param name="_fileAbsolutePath">文件的绝对路径</param> 18 public void AddFile(string _fileAbsolutePath) 19 { 20 AbsolutePaths.Add(_fileAbsolutePath); 21 } 22 /// <summary> 23 /// 压缩文件或者文件夹 24 /// </summary> 25 /// <param name="_depositPath">压缩后文件的存放路径 如C:\\windows\abc.zip</param> 26 /// <returns></returns> 27 public bool CompressionZip(string _depositPath) 28 { 29 bool result = true; 30 FileStream fs = null; 31 try 32 { 33 ZipOutputStream ComStream = new ZipOutputStream(File.Create(_depositPath)); 34 ComStream.SetLevel(9); //压缩等级 35 foreach (string path in AbsolutePaths) 36 { 37 //如果是目录 38 if (Directory.Exists(path)) 39 { 40 ZipFloder(path, ComStream, path); 41 } 42 else if (File.Exists(path))//如果是文件 43 { 44 fs = File.OpenRead(path); 45 byte[] bts = new byte[fs.Length]; 46 fs.Read(bts, 0, bts.Length); 47 ZipEntry ze = new ZipEntry(new FileInfo(path).Name); 48 ComStream.PutNextEntry(ze); //为压缩文件流提供一个容器 49 ComStream.Write(bts, 0, bts.Length); //写入字节 50 } 51 } 52 ComStream.Finish(); // 结束压缩 53 ComStream.Close(); 54 } 55 catch (Exception ex) 56 { 57 if (fs != null) 58 { 59 fs.Close(); 60 } 61 errorMsg = ex.Message; 62 result = false; 63 } 64 return result; 65 } 66 //压缩文件夹 67 private void ZipFloder(string _OfloderPath, ZipOutputStream zos, string _floderPath) 68 { 69 foreach (FileSystemInfo item in new DirectoryInfo(_floderPath).GetFileSystemInfos()) 70 { 71 if (Directory.Exists(item.FullName)) 72 { 73 ZipFloder(_OfloderPath, zos, item.FullName); 74 } 75 else if (File.Exists(item.FullName))//如果是文件 76 { 77 DirectoryInfo ODir = new DirectoryInfo(_OfloderPath); 78 string fullName2 = new FileInfo(item.FullName).FullName; 79 string path = ODir.Name + fullName2.Substring(ODir.FullName.Length, fullName2.Length - ODir.FullName.Length);//获取相对目录 80 FileStream fs = File.OpenRead(fullName2); 81 byte[] bts = new byte[fs.Length]; 82 fs.Read(bts, 0, bts.Length); 83 ZipEntry ze = new ZipEntry(path); 84 zos.PutNextEntry(ze); //为压缩文件流提供一个容器 85 zos.Write(bts, 0, bts.Length); //写入字节 86 } 87 } 88 } 89 /// <summary> 90 /// 解压 91 /// </summary> 92 /// <param name="_depositPath">压缩文件路径</param> 93 /// <param name="_floderPath">解压的路径</param> 94 /// <returns></returns> 95 public bool DeCompressionZip(string _depositPath, string _floderPath) 96 { 97 bool result = true; 98 FileStream fs=null; 99 try 100 { 101 using (ZipInputStream InpStream = new ZipInputStream(File.OpenRead(_depositPath))) 102 { 103 ZipEntry ze = InpStream.GetNextEntry();//获取压缩文件中的每一个文件 104 Directory.CreateDirectory(_floderPath);//创建解压文件夹 105 while (ze != null)//如果解压完ze则是null 106 { 107 if (ze.IsFile)//压缩zipINputStream里面存的都是文件。带文件夹的文件名字是文件夹\\文件名 108 { 109 string[] strs = ze.Name.Split('\\');//如果文件名中包含’\\‘则表明有文件夹 110 if (strs.Length > 1) 111 { 112 //两层循环用于一层一层创建文件夹 113 for (int i = 0; i < strs.Length - 1; i++) 114 { 115 string floderPath = _floderPath; 116 for (int j = 0; j < i; j++) 117 { 118 floderPath = floderPath + "\\" + strs[j]; 119 } 120 floderPath = floderPath + "\\" + strs[i]; 121 Directory.CreateDirectory(floderPath); 122 } 123 } 124 fs = new FileStream(_floderPath + "\\" + ze.Name, FileMode.OpenOrCreate, FileAccess.Write);//创建文件 125 //循环读取文件到文件流中 126 while (true) 127 { 128 byte[] bts = new byte[1024]; 129 int i = InpStream.Read(bts, 0, bts.Length); 130 if (i > 0) 131 { 132 fs.Write(bts, 0, i); 133 } 134 else 135 { 136 fs.Flush(); 137 fs.Close(); 138 break; 139 } 140 } 141 } 142 ze = InpStream.GetNextEntry(); 143 } 144 } 145 } 146 catch (Exception ex) 147 { 148 if (fs != null) 149 { 150 fs.Close(); 151 } 152 errorMsg = ex.Message; 153 result = false; 154 } 155 finally 156 { 157 if (fs != null) 158 { 159 fs.Close(); 160 fs.Dispose(); 161 } 162 } 163 return result; 164 } 165 166 167 /// <summary> 168 /// 功能:解压zip格式的文件。 169 /// </summary> 170 /// <param name="zipFilePath">压缩文件路径</param> 171 /// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param> 172 /// <param name="err">出错信息</param> 173 /// <returns>解压是否成功</returns> 174 public bool UnZipFile(string zipFilePath, string unZipDir)// , out string err 175 { 176 // err = ""; 177 if (zipFilePath == string.Empty) 178 { 179 //err = "压缩文件不能为空!"; 180 return false; 181 } 182 if (!File.Exists(zipFilePath)) 183 { 184 //err = "压缩文件不存在!"; 185 return false; 186 } 187 //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹 188 if (unZipDir == string.Empty) 189 unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath)); 190 if (!unZipDir.EndsWith("\\")) 191 unZipDir += "\\"; 192 if (!Directory.Exists(unZipDir)) 193 Directory.CreateDirectory(unZipDir); 194 195 try 196 { 197 using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath.ToLower()))) 198 { 199 200 ZipEntry theEntry; 201 while ((theEntry = s.GetNextEntry()) != null) 202 { 203 string directoryName = Path.GetDirectoryName(theEntry.Name); 204 string fileName = Path.GetFileName(theEntry.Name); 205 if (directoryName.Length > 0) 206 { 207 Directory.CreateDirectory(unZipDir + directoryName); 208 } 209 if (!directoryName.EndsWith("\\")) 210 directoryName += "\\"; 211 if (fileName != String.Empty) 212 { 213 using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name)) 214 { 215 216 int size = 2048; 217 byte[] data = new byte[2048]; 218 while (true) 219 { 220 size = s.Read(data, 0, data.Length); 221 if (size > 0) 222 { 223 streamWriter.Write(data, 0, size); 224 } 225 else 226 { 227 break; 228 } 229 } 230 } 231 } 232 }//while 233 } 234 } 235 catch (Exception ex) 236 { 237 //err = ex.Message; 238 return false; 239 } 240 return true; 241 }//解压结束 242 243 }