C# 利用ICSharpCode.SharpZipLib 压缩和解压文件
public class SharpZipHelper { /// <summary> /// 压缩率:0-9 /// </summary> private int compressionLevel = 5; private byte[] buffer = new byte[2048]; /// <summary> /// 压缩文件 /// </summary> /// <param name="fileToZip">要压缩的文件路径</param> /// <param name="zipedFile">压缩后的文件路径</param> public void ZipFile(string fileToZip, string zipedFile) { if (!File.Exists(fileToZip)) { throw new FileNotFoundException("The specified file " + fileToZip + " could not be found."); } using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedFile))) { string fileName = Path.GetFileName(fileToZip); ZipEntry zipEntry = new ZipEntry(fileName); zipStream.PutNextEntry(zipEntry); zipStream.SetLevel(compressionLevel); using (FileStream streamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read)) { while (true) { int size = streamToZip.Read(buffer, 0, buffer.Length); if (size < 1) break; zipStream.Write(buffer, 0, size); } } } } /// <summary> /// 压缩文件夹 /// </summary> /// <param name="directoryToZip">要压缩的文件夹路径</param> /// <param name="zipedDirectory">压缩或的文件夹路径</param> public void ZipDerctory(string directoryToZip, string zipedFileName) { using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedFileName))) { ArrayList fileList = GetFileList(directoryToZip); int directoryNameLength = (Directory.GetParent(directoryToZip)).ToString().Length; zipStream.SetLevel(compressionLevel); ZipEntry zipEntry = null; FileStream fileStream = null; foreach (string fileName in fileList) { zipEntry = new ZipEntry(fileName.Remove(0, directoryNameLength)); zipStream.PutNextEntry(zipEntry); if (!fileName.EndsWith(@"/")) { fileStream = File.OpenRead(fileName); while (true) { int readCount = fileStream.Read(buffer, 0, buffer.Length); if (readCount < 1) break; zipStream.Write(buffer, 0, readCount); } } } } } /// <summary> /// 得到文件下的所有文件 /// </summary> /// <param name="directory">文件夹路径</param> /// <returns></returns> private ArrayList GetFileList(string directory) { ArrayList fileList = new ArrayList(); bool isEmpty = true; foreach (string file in Directory.GetFiles(directory)) { fileList.Add(file); isEmpty = false; } if (isEmpty) { if (Directory.GetDirectories(directory).Length == 0) { fileList.Add(directory + @"/"); } } foreach (string dirs in Directory.GetDirectories(directory)) { foreach (object obj in GetFileList(dirs)) { fileList.Add(obj); } } return fileList; } /// <summary> /// 解压缩文件 /// </summary> /// <param name="zipFilePath">压缩文件路径和文件名 eg:D:\aa.zip</param> /// <param name="unZipFilePatah">解压缩文件存放路径 eg:D:\aa\</param> public void UnZipFile(string zipFilePath, string unZipFilePatah) { //如果目录不存在则创建 if (!Directory.Exists(unZipFilePatah)) Directory.CreateDirectory(unZipFilePatah); using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipFilePath))) { ZipEntry zipEntry = null; while ((zipEntry=zipStream.GetNextEntry()) != null) { string fileName = Path.GetFileName(zipEntry.Name); if (!string.IsNullOrEmpty(fileName)) { if (zipEntry.CompressedSize == 0) break; using (FileStream stream = File.Create(unZipFilePatah + fileName)) { while (true) { int size = zipStream.Read(buffer, 0, buffer.Length); if (size > 0) { stream.Write(buffer, 0, size); } else { break; } } } } } } } /// <summary> /// 解压缩目录 /// </summary> /// <param name="zipDirectoryPath">压缩目录路径 eg:D:\aaa.zip</param> /// <param name="unZipDirecotyPath">解压缩目录路径 eg:D:\aaaa\</param> public void UnZipDirectory(string zipDirectoryPath, string unZipDirecotyPath) { //如果目录不存在则创建 if (!Directory.Exists(unZipDirecotyPath)) Directory.CreateDirectory(unZipDirecotyPath); using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipDirectoryPath))) { ZipEntry zipEntry = null; while ((zipEntry = zipStream.GetNextEntry())!= null) { string directoryName = Path.GetDirectoryName(zipEntry.Name); string fileName = Path.GetFileName(zipEntry.Name); directoryName = unZipDirecotyPath + directoryName; if (!string.IsNullOrEmpty(directoryName)) { Directory.CreateDirectory(directoryName); } if (!string.IsNullOrEmpty(fileName)) { if (zipEntry.CompressedSize == 0) break; if (zipEntry.IsDirectory) { directoryName = Path.GetDirectoryName(unZipDirecotyPath + zipEntry.Name); Directory.CreateDirectory(directoryName); } using (FileStream stream = File.Create(unZipDirecotyPath + zipEntry.Name)) { while (true) { int size = zipStream.Read(buffer, 0, buffer.Length); if (size > 0) { stream.Write(buffer, 0, size); } else { break; } } } } } } } }
转载来源:http://blog.csdn.net/venus0314/article/details/1280610