解压缩
////// 解压缩文件 /// /// 解压到目录 /// 文件字节流 public static void UnZip(string dirPath, byte[] bytes, ProgressBar pb, ProgressBar pbTotal) { pb.Minimum = 0; pb.Value = 0; pb.Maximum = ZipCount(bytes); ZipInputStream s = new ZipInputStream(new MemoryStream(bytes)); ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { pb.Value++; pbTotal.Value++; string fileName = Path.GetFileName(theEntry.Name); //生成解压目录 if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } if (string.IsNullOrEmpty(fileName)) { string dir = System.IO.Path.Combine(dirPath, theEntry.Name); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } } else { //解压文件到指定的目录 FileStream streamWriter = File.Create(System.IO.Path.Combine(dirPath, theEntry.Name)); int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } streamWriter.Close(); } } s.Close(); }
////// 计算文件数量 /// /// ///public static int ZipCount(byte[] bytes) { int count = 0; ZipInputStream s = new ZipInputStream(new MemoryStream(bytes)); ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { count++; } s.Close(); return count; }
posted on 2013-04-17 22:24 阅读( ...) 评论( ...)