SharpZipLib使用递归压缩文件夹
using System;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
namespace feixinBackUp
{
/// <summary>
/// 压缩解压类
/// </summary>
public class Myzip
{
ZipOutputStream zos=null;
string strBaseDir="";
public void dlZipDir(string[] args)
{
zos = new ZipOutputStream(File.Create(args[1]));
zos.Password ="";//设置压缩文件密码
strBaseDir = args[0]+ "\\";
addZipEntry(strBaseDir);
zos.Finish();
zos.Close();
}
void addZipEntry(string PathStr)
{
DirectoryInfo di=new DirectoryInfo(PathStr);
foreach(DirectoryInfo item in di.GetDirectories())
{
addZipEntry(item.FullName);
}
foreach(FileInfo item in di.GetFiles())
{
FileStream fs = File.OpenRead(item.FullName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer,0,buffer.Length);
string strEntryName=item.FullName.Replace(strBaseDir,"");
ZipEntry entry = new ZipEntry(strEntryName);
entry.Size = fs.Length;
zos.PutNextEntry(entry);
zos.Write(buffer,0,buffer.Length);
fs.Close();
}
}
}
}
综合网络上的,自己小小修改了下using ICSharpCode.SharpZipLib.Zip;
using System.IO;
namespace feixinBackUp
{
/// <summary>
/// 压缩解压类
/// </summary>
public class Myzip
{
ZipOutputStream zos=null;
string strBaseDir="";
public void dlZipDir(string[] args)
{
zos = new ZipOutputStream(File.Create(args[1]));
zos.Password ="";//设置压缩文件密码
strBaseDir = args[0]+ "\\";
addZipEntry(strBaseDir);
zos.Finish();
zos.Close();
}
void addZipEntry(string PathStr)
{
DirectoryInfo di=new DirectoryInfo(PathStr);
foreach(DirectoryInfo item in di.GetDirectories())
{
addZipEntry(item.FullName);
}
foreach(FileInfo item in di.GetFiles())
{
FileStream fs = File.OpenRead(item.FullName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer,0,buffer.Length);
string strEntryName=item.FullName.Replace(strBaseDir,"");
ZipEntry entry = new ZipEntry(strEntryName);
entry.Size = fs.Length;
zos.PutNextEntry(entry);
zos.Write(buffer,0,buffer.Length);
fs.Close();
}
}
}
}
直接使用,发现问题请留言告诉我下...