//--------------------复制文件
//复制文件中有多个文件夹情况之下
private static bool CopyDirectory(string downLoadPath, string WinClientPath)
{
try
{
DirectoryInfo dir = new DirectoryInfo(downLoadPath);
FileSystemInfo[] fileAll = dir.GetFileSystemInfos(); //获取目录下(不包含子目录)的文件和子目录
//获取目录下所有的文件
FileSystemInfo[] fileinfo = dir.GetFiles();
//将查询当前目录的所有文件复制
foreach (FileSystemInfo i in fileinfo)
{
File.Copy(i.FullName, WinClientPath + "//" + i.Name, true); //不是文件夹即复制文件,true表示可以覆盖同名文件
}
//将当前查询的所有文件夹创建
foreach (FileSystemInfo i in fileAll)
{
if (i is DirectoryInfo) //判断是否文件夹
{
if (!Directory.Exists(WinClientPath + "\\" + i.Name))
{
Directory.CreateDirectory(WinClientPath + "\\" + i.Name); //目标目录下不存在此文件夹即创建子文件夹
}
//文件夹创建后循环copyDiretory对子目录进行分析
CopyDirectory(i.FullName, WinClientPath + "\\" + i.Name);
}
}
//复制完成后删除download文件夹
dir.Delete(true);
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
//--------------------解压文件------
//解压文件中有多个文件夹情况之下
private static bool Unzip(string address, string filezip)
{
try
{
string directoryName = Path.GetFullPath(address);
if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); }
//打开读取文件
using (FileStream fileStreamIn = new FileStream(filezip, FileMode.Open, FileAccess.Read))
{
using (ZipInputStream zipInStream = new ZipInputStream(fileStreamIn))
{
ZipEntry entry = zipInStream.GetNextEntry();
do
{
//判断是否时文件夹,如果是则生成文件夹
if (entry.Name.LastIndexOf("/") == entry.Name.Length - 1)
{
Directory.CreateDirectory(directoryName + @"\\" + entry.Name);
}
else
{
//不是则先判断在该文件下的文件夹是否都存在,不存在则生成文件夹
if (entry.Name.LastIndexOf("/") > 0)
{
if (!Directory.Exists(directoryName + @"\\" + entry.Name.Substring(0, entry.Name.LastIndexOf("/"))))
{ Directory.CreateDirectory(directoryName + @"\\" + entry.Name.Substring(0, entry.Name.LastIndexOf("/"))); }
}
//文件进行解压
using (FileStream fileStreamOut = new FileStream(directoryName + @"\\" + entry.Name, FileMode.Create, FileAccess.Write))
{
int size = 2048;
byte[] buffer = new byte[2048];
do
{
//读取文件大小,并解压到对应地址
size = zipInStream.Read(buffer, 0, buffer.Length);
fileStreamOut.Write(buffer, 0, size);
} while (size > 0);
}
}
}
while ((entry = zipInStream.GetNextEntry()) != null);
zipInStream.Close();
zipInStream.Dispose();
}
}
//删除zip文件
File.Delete(filezip);
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
//--------------------复制文件
网友评论