About this document
This document ICSharpCode.SharpZipLib.dll package, commonly used compression and decompression method based on already encompassed, are accumulated through project combat
1. Basic Introduction
Since the project will need to use a variety of compressed file compression download, reducing network bandwidth, so compression is a very common feature for compression Microsoft itself also provides some libraries
- Microsoft's own compression class ZipArchive class, can only be used for NET FrameWork4.5
- Call compression software compression action command execution, the computer itself will need to install a software compression
- Using third-party compression dll file, generally used most (ICSharpCode.SharpZipLib.dll), download dll ICSharpCode.SharpZipLib.zip
2. The actual project
- Compress a single file, you need to specify the compression level
- Compressing a single folder, you need to specify the compression level
- Compress multiple files or multiple folders
- Compressed packet is encrypted with less [,] the actual situation there
- Direct unpack, without a password
- A password is required decompression
2.1 compress a single file
Write two methods, you can specify the compression level, so your compressed packet size is not the same
2.2 compressing a single folder
public void ZipDir(string dirToZip, string zipedFileName, int compressionLevel = 9)
2.3 plurality of compressed files or folders
public bool ZipManyFilesOrDictorys(IEnumerable<string> folderOrFileList, string zipedFile, string password)
2.4 pairs of the compressed packet is encrypted
public bool ZipManyFilesOrDictorys(IEnumerable<string> folderOrFileList, string zipedFile, string password)
2.5 Direct unpack, without a password
public void UnZip(string zipFilePath, string unZipDir)
3. presentation charts
3.ZipHelper Download
//-------------------------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2016 , ZTO , Ltd .
//------------------------------------------------------------------------------------- using System; using System.Collections; using System.Collections.Generic; using System.IO; namespace ZTO.PicTest.Utilities { using ICSharpCode.SharpZipLib.Checksums; using ICSharpCode.SharpZipLib.Zip; /// <summary> /// Zip压缩帮助类 /// /// 修改纪录 /// /// 2015-09-16 版本:1.0 YangHengLian 创建主键,注意命名空间的排序。 /// 2016-5-7 YangHengLian增加了可以支持多个文件或者多个文件夹打包成一个zip文件 /// /// 版本:1.0 /// /// <author> /// <name>YangHengLian</name> /// <date>2015-09-16</date> /// </author> /// </summary> public class ZipHelper { /// <summary> /// 压缩文件夹 /// </summary> /// <param name="dirToZip"></param> /// <param name="zipedFileName"></param> /// <param name="compressionLevel">压缩率0(无压缩)9(压缩率最高)</param> public void ZipDir(string dirToZip, string zipedFileName, int compressionLevel = 9) { if (Path.GetExtension(zipedFileName) != ".zip") { zipedFileName = zipedFileName + ".zip"; } using (var zipoutputstream = new ZipOutputStream(File.Create(zipedFileName))) { zipoutputstream.SetLevel(compressionLevel); Crc32 crc = new Crc32(); Hashtable fileList = GetAllFies(dirToZip); foreach (DictionaryEntry item in fileList) { FileStream fs = new FileStream(item.Key.ToString(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); // ZipEntry entry = new ZipEntry(item.Key.ToString().Substring(dirToZip.Length + 1)); ZipEntry entry = new ZipEntry(Path.GetFileName(item.Key.ToString())) { DateTime = (DateTime) item.Value, Size = fs.Length }; fs.Close(); crc.Reset(); crc.Update(buffer); entry.Crc = crc.Value; zipoutputstream.PutNextEntry(entry); zipoutputstream.Write(buffer, 0, buffer.Length); } } } /// <summary> /// 获取所有文件 /// </summary> /// <returns></returns> public Hashtable GetAllFies(string dir) { Hashtable filesList = new Hashtable(); DirectoryInfo fileDire = new DirectoryInfo(dir); if (!fileDire.Exists) { throw new FileNotFoundException("目录:" + fileDire.FullName + "没有找到!"); } GetAllDirFiles(fileDire, filesList); GetAllDirsFiles(fileDire.GetDirectories(), filesList); return filesList; } /// <summary> /// 获取一个文件夹下的所有文件夹里的文件 /// </summary> /// <param name="dirs"></param> /// <param name="filesList"></param> public void GetAllDirsFiles(IEnumerable<DirectoryInfo> dirs, Hashtable filesList) { foreach (DirectoryInfo dir in dirs) { foreach (FileInfo file in dir.GetFiles("*.*")) { filesList.Add(file.FullName, file.LastWriteTime); } GetAllDirsFiles(dir.GetDirectories(), filesList); } } /// <summary> /// 获取一个文件夹下的文件 /// </summary> /// <param name="dir">目录名称</param> /// <param name="filesList">文件列表HastTable</param> public static void GetAllDirFiles(DirectoryInfo dir, Hashtable filesList) { foreach (FileInfo file in dir.GetFiles("*.*")) { filesList.Add(file.FullName, file.LastWriteTime); } } /// <summary> /// 功能:解压zip格式的文件。 /// </summary> /// <param name="zipFilePath">压缩文件路径</param> /// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param> /// <returns>解压是否成功</returns> public void UnZip(string zipFilePath, string unZipDir) { if (zipFilePath == string.Empty) { throw new Exception("压缩文件不能为空!"); } if (!File.Exists(zipFilePath)) { throw new FileNotFoundException("压缩文件不存在!"); } //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹 if (unZipDir == string.Empty) unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath)); if (!unZipDir.EndsWith("/")) unZipDir += "/"; if (!Directory.Exists(unZipDir)) Directory.CreateDirectory(unZipDir); using (var s = new ZipInputStream(File.OpenRead(zipFilePath))) { ZipEntry theEntry;