代码如下图:
/// <summary>
/// 压缩、解压缩文件类
/// </summary>
public class ZipHelper
{
/// <summary>
/// 压缩
/// </summary>
/// <param name="sourcepath">要压缩的文件或者文件夹(绝对路径)</param>
/// <param name="destpath">压缩后zip文件的路径(绝对路径)</param>
/// <param name="level">压缩级别0~9,数字越大压缩率越高,默认为5</param>
/// <param name="zipcontent">压缩文件夹时,只压缩文件夹内文件是否,如果为true,说明只是压缩文件夹内的文件,如果是false,则压缩出来的zip文件,包含该文件夹</param>
/// <returns></returns>
public static bool CompressionZip(string sourcepath, string destpath, int level = 5, bool zipcontent = true)
{
bool result = false;
FileStream fs = null;
try
{
sourcepath = sourcepath.Replace("/", "\\");
destpath = destpath.Replace("/", "\\");
ZipOutputStream outputstream = new ZipOutputStream(File.Create(destpath));//压缩
outputstream.SetLevel(level); //设置压缩的等级
if (Directory.Exists(sourcepath))
{
ZipFolder(sourcepath, outputstream, sourcepath, zipcontent);
}
else if (File.Exists(sourcepath))
{
fs = File.OpenRead(sourcepath);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string filename = new FileInfo(sourcepath).Name;
ZipEntry entry = new ZipEntry(filename);
outputstream.PutNextEntry(entry);//为压缩文件流提供一个容器
outputstream.Write(buffer, 0, buffer.Length);//写入字节
}
outputstream.Finish(); //结束压缩
outputstream.Close();
fs.Close();
result = true;
}
catch (Exception ex)
{
if (fs != null)
{
fs.Close();
}
}
return result;
}
/// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="sourcefolder"></param>
/// <param name="outputstream"></param>
/// <param name="topsourcefolder"></param>
/// <param name="zipcontent"></param>
static void ZipFolder(string sourcefolder, ZipOutputStream outputstream, string topsourcefolder, bool zipcontent = true)
{
FileSystemInfo[] fileInfos = new DirectoryInfo(sourcefolder).GetFileSystemInfos();
foreach (FileSystemInfo item in fileInfos)
{
if (Directory.Exists(item.FullName))
{
ZipFolder(item.FullName, outputstream, topsourcefolder, zipcontent);
}
else if (File.Exists(item.FullName))
{
DirectoryInfo dirInfo = new DirectoryInfo(topsourcefolder);
string fullname = new FileInfo(item.FullName).FullName;//获取该文件的全部路径
string name = fullname.Substring(dirInfo.FullName.Length, fullname.Length - dirInfo.FullName.Length);//获取压缩文件相对于跟文件夹的相对路径
string relativepath = string.Empty;
if (zipcontent)//是否只压缩文件的内容, true,压缩文件夹内容,false,压缩文件夹
{
relativepath = name;
}
else
{
relativepath = string.Format("{0}{1}", dirInfo.Name, name);
}
FileStream fs = File.OpenRead(fullname);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(relativepath);
outputstream.PutNextEntry(entry); //为压缩文件流提供一个容器
outputstream.Write(buffer, 0, buffer.Length);
}
}
}
/// <summary>
/// 解压缩
/// </summary>
/// <param name="zippath">待解压缩的zip文件路径</param>
/// <param name="destfolder">解压处理的文件存放路径</param>
/// <returns></returns>
public static bool DeCompressionZip(string zippath, string destfolder)
{
bool result = false;
FileStream fs = null;
try
{
ZipInputStream inputstream = new ZipInputStream(File.OpenRead(zippath));
ZipEntry entry = inputstream.GetNextEntry(); //获取压缩文件中的每一个文件
if (!Directory.Exists(destfolder))
{
Directory.CreateDirectory(destfolder);
}
while (entry != null)//如果解压完ze则是null
{
if (entry.IsFile) //压缩zipINputStream里面存的都是文件。带文件夹的文件名字是文件夹\\文件名
{
string[] pathArr = entry.Name.Split('\\');//如果文件名包含"\\",这说明包含文件夹
if (pathArr.Length > 1)
{
string path = entry.Name.Substring(0, entry.Name.LastIndexOf("\\") + 1); //创建文件夹
Directory.CreateDirectory(Path.Combine(String.Format("{0}\\{1}", destfolder, path)));
}
fs = new FileStream(String.Format("{0}\\{1}", destfolder, entry.Name), FileMode.OpenOrCreate, FileAccess.ReadWrite);
byte[] buffer = new byte[1024];
int read = inputstream.Read(buffer, 0, buffer.Length);
while (read > 0)
{
fs.Write(buffer, 0, read);
read = inputstream.Read(buffer, 0, buffer.Length);
}
fs.Flush();
fs.Close();
}
entry = inputstream.GetNextEntry();
}
}
catch (Exception ex)
{
if (fs != null)
{
fs.Close();
}
}
return result;
}
}