/// <summary> /// 压缩单个文件 /// </summary> /// <param>要压缩的文件</param> /// <param>压缩后的文件</param> /// <param>压缩等级</param> /// <param>每次写入大小</param> public static void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize) { if (string.IsNullOrEmpty(fileToZip)) { throw new ArgumentNullException(fileToZip); } if (string.IsNullOrEmpty(zipedFile)) { throw new ArgumentNullException(zipedFile); } if (!File.Exists(fileToZip)) { throw new FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!"); } try { using (var zipFile = File.Create(zipedFile)) { using (var zipStream = new ZipOutputStream(zipFile)) { using (var streamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read)) { var fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\", StringComparison.Ordinal) + 1); var zipEntry = new ZipEntry(fileName); zipStream.PutNextEntry(zipEntry); zipStream.SetLevel(compressionLevel); var buffer = new byte[blockSize]; try { int sizeRead; do { sizeRead = streamToZip.Read(buffer, 0, buffer.Length); zipStream.Write(buffer, 0, sizeRead); } while (sizeRead > 0); } catch (Exception ex) { throw new Exception(ex.Message); } streamToZip.Close(); } zipStream.Finish(); zipStream.Close(); } zipFile.Close(); } } catch (IOException ioex) { throw new IOException(ioex.Message); } catch (Exception ex) { throw new Exception(ex.Message); } }
2. 压缩单个文件:
/// <summary> /// 压缩单个文件 /// </summary> /// <param>要进行压缩的文件名</param> /// <param>压缩后生成的压缩文件名</param> public static void ZipFile(string fileToZip, string zipedFile) { if (string.IsNullOrEmpty(fileToZip)) { throw new ArgumentException(fileToZip); } if (string.IsNullOrEmpty(zipedFile)) { throw new ArgumentException(zipedFile); } if (!File.Exists(fileToZip)) { throw new FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!"); } try { using (var fs = File.OpenRead(fileToZip)) { var buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); using (var zipFile = File.Create(zipedFile)) { using (var zipStream = new ZipOutputStream(zipFile)) { var fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\", StringComparison.Ordinal) + 1); var zipEntry = new ZipEntry(fileName); zipStream.PutNextEntry(zipEntry); zipStream.SetLevel(5); zipStream.Write(buffer, 0, buffer.Length); zipStream.Finish(); zipStream.Close(); } } } } catch (IOException ioex) { throw new IOException(ioex.Message); } catch (Exception ex) { throw new Exception(ex.Message); } }
3.压缩多层目录:
/// <summary> /// 压缩多层目录 /// </summary> /// <param>目录</param> /// <param>压缩文件</param> public static void ZipFileDirectory(string strDirectory, string zipedFile) { if (string.IsNullOrEmpty(strDirectory)) { throw new ArgumentException(strDirectory); } if (string.IsNullOrEmpty(zipedFile)) { throw new ArgumentException(zipedFile); } using (var zipFile = File.Create(zipedFile)) { using (var s = new ZipOutputStream(zipFile)) { ZipSetp(strDirectory, s, ""); } } }
4.递归遍历目录:
/// <summary> /// 递归遍历目录 /// </summary> /// <param>目录</param> /// <param>ZipOutputStream对象</param> /// <param>父路径</param> private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath) { if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar) { strDirectory += Path.DirectorySeparatorChar; } var crc = new Crc32(); var filenames = Directory.GetFileSystemEntries(strDirectory); try { // 遍历所有的文件和目录 foreach (var file in filenames) { // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件 if (Directory.Exists(file)) { var pPath = parentPath; pPath += file.Substring(file.LastIndexOf("\\", StringComparison.Ordinal) + 1); pPath += "\\"; ZipSetp(file, s, pPath); } // 否则直接压缩文件 else { //打开压缩文件 using (var fs = File.OpenRead(file)) { var buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); var fileName = parentPath + file.Substring(file.LastIndexOf("\\", StringComparison.Ordinal) + 1); var entry = new ZipEntry(fileName) { DateTime = DateTime.Now, Size = fs.Length }; fs.Close(); crc.Reset(); crc.Update(buffer); entry.Crc = crc.Value; s.PutNextEntry(entry); s.Write(buffer, 0, buffer.Length); } } } } catch (IOException ioex) { throw new IOException(ioex.Message); } catch (Exception ex) { throw new Exception(ex.Message); } }
5.解压缩一个 zip 文件: