压缩解压
public class ZipHerlper { #region 压缩zip public static bool CreateZipDirectory(string dirPath, string zipFilePath) { bool success = false; FileInfo zipFile = new FileInfo(zipFilePath); if (zipFile.Exists) { zipFile.Delete(); } ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipFilePath)); try { if (dirPath[dirPath.Length - 1] != System.IO.Path.DirectorySeparatorChar) dirPath += System.IO.Path.DirectorySeparatorChar; zipStream.SetLevel(9);// 压缩级别 0-9 byte[] buffer = new byte[4096]; //缓冲区大小 CreateZipFiles(dirPath, zipStream, dirPath, buffer); success = true; } catch (Exception ex) { success = false; } finally { zipStream.Finish(); zipStream.Close(); zipFile = new FileInfo(zipFilePath); if (!success && zipFile.Exists) { zipFile.Delete(); } } return success; } public static void CreateZipFiles(string sourceFilePath, ZipOutputStream zipStream, string staticFile, byte[] buffer) { string[] filesArray = Directory.GetFileSystemEntries(sourceFilePath); foreach (string file in filesArray) { if (!file.EndsWith(".pdb") && !file.Contains(".vshost.exe")) { if (Directory.Exists(file)) { CreateZipFiles(file, zipStream, staticFile, buffer); } else { ZipEntry entry = new ZipEntry(file.Substring(staticFile.LastIndexOf("\\") + 1)); entry.DateTime = DateTime.Now; zipStream.PutNextEntry(entry); using (FileStream fs = File.OpenRead(file)) { int sourceBytes; do { sourceBytes = fs.Read(buffer, 0, buffer.Length); zipStream.Write(buffer, 0, sourceBytes); } while (sourceBytes > 0); } } } } } #endregion /// <summary> /// 解压 /// </summary> /// <param name="zip"></param> /// <param name="path"></param> public static void UnZip(string zip, string path) { ZipInputStream s = new ZipInputStream(File.OpenRead(zip)); ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(path); string fileName = Path.GetFileName(theEntry.Name); //生成解压目录 if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } if (theEntry.Name.Contains("\\") || theEntry.Name.Contains("/")) { string subPath = Path.GetDirectoryName(path + theEntry.Name); if (!Directory.Exists(subPath)) { Directory.CreateDirectory(subPath); } } if (fileName != String.Empty) { //解压文件到指定的目录 FileStream streamWriter = File.Create(path + 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(); } }
private void button1_Click(object sender, EventArgs e) { FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "请选择文件路径"; if (dialog.ShowDialog() == DialogResult.OK) { string foldPath = dialog.SelectedPath; ZipHerlper.CreateZipDirectory(foldPath, foldPath+".Zip"); MessageBox.Show("压缩完成"); } } private void button2_Click(object sender, EventArgs e) { OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Multiselect = true; fileDialog.Title = "请选择文件"; fileDialog.Filter = "所有文件(*.*)|*.*"; if (fileDialog.ShowDialog() == DialogResult.OK) { string file = fileDialog.FileName; string ss = file.Substring(0, file.LastIndexOf('.')); ZipHerlper.UnZip(file, file.Substring(0,file.LastIndexOf('.'))+"\\"); MessageBox.Show("解压完成"); } }
ICSharpCode.SharpZipLib.dll需要引用