/// <summary> /// 多个文件压缩为一个文件 *.zip /// </summary> /// <param name="files"></param> /// <param name="outputFullPathAndFile"></param> /// <param name="password"></param> public static void ZipFilesList(List <string> files, string outputFullPathAndFile, string password) { FileStream ostream; byte[] obuffer; string outPath = outputFullPathAndFile; ZipOutputStream oZipStream = new ZipOutputStream(File.Create(outPath)); // create zip stream if (password != null && password != String.Empty) { oZipStream.Password = password; } oZipStream.SetLevel(9); // maximum compression ZipEntry oZipEntry; foreach (string Fil in files) // for each file, generate a zipentry { ///将文件名称解码为字符串 oZipEntry = new ZipEntry(HttpUtility.UrlDecode(X2File.GetFileName(Fil))); oZipStream.PutNextEntry(oZipEntry); if (!Fil.EndsWith(@"/")) // if a file ends with '/' its a directory { ostream = File.OpenRead(Fil); obuffer = new byte[ostream.Length]; ostream.Read(obuffer, 0, obuffer.Length); oZipStream.Write(obuffer, 0, obuffer.Length); ostream.Close(); } } oZipStream.Finish(); oZipStream.Close(); }
/// <summary> /// 修改文件的只读属性 /// </summary> /// <param name="filePath"></param> /// <returns></returns> public static bool ModifyFileAttribuate(string filePath) { if (X2File.IsExist(filePath)) { FileInfo fi = new FileInfo(filePath); ModifyFileAttribuate(fi); } return(true); }
/// <summary> /// 修改文件夹的只读属性 /// </summary> /// <param name="filePath"></param> /// <returns></returns> public static bool ModifyDirectoryAttribuate(DirectoryInfo dir) { if (dir.Exists) { ////递归修改属性 DirectoryInfo[] subDirs = dir.GetDirectories(); foreach (var item in subDirs) { ModifyDirectoryAttribuate(item); } FileInfo[] subFiles = dir.GetFiles(); foreach (var item in subFiles) { X2File.ModifyFileAttribuate(item); } if (dir.Attributes.ToString().Contains("ReadOnly")) { dir.Attributes = FileAttributes.Normal; } } return(true); }
/// <summary> /// 差异文件夹的复制 /// </summary> /// <param name="newDirPath">复制到的新文件夹</param> /// <param name="srcDirPath">原始的目录</param> /// <param name="aimDirPath">目标对比的目录</param> /// <returns>成功与否</returns> public static void CopyDifferenceFiles(string newDirPath, string srcDirPath, string aimDirPath) { if (IsExist(newDirPath)) { CreatDir(newDirPath); } ///源目标不存在,则直接复制 if (string.IsNullOrEmpty(srcDirPath)) { CopyDir(aimDirPath, newDirPath, CopyType.AllFileAndDirectory); return; } DirectoryInfo srcInfo = new DirectoryInfo(srcDirPath); DirectoryInfo aimInfo = new DirectoryInfo(aimDirPath); ///文件对比 foreach (var aimItem in aimInfo.GetFiles()) { bool isSameFile = false; foreach (var srcItem in srcInfo.GetFiles()) { if (X2File.CompareFile(srcItem.FullName, aimItem.FullName)) { isSameFile = true; break; } } ///新增或者不同的文件 if (!isSameFile) { File.Copy(aimItem.FullName, newDirPath + @"\" + aimItem.Name); } } ///目录对比 foreach (var aimItem in aimInfo.GetDirectories()) { ///空目录直接排出 if (aimItem.GetFileSystemInfos().Length == 0) { continue; } bool isSameFile = false; foreach (var srcItem in srcInfo.GetDirectories()) { ///相同目录递归对比 if (srcItem.Name == aimItem.Name) { CopyDifferenceFiles(newDirPath + @"\" + aimItem.Name, srcItem.FullName, aimItem.FullName); isSameFile = true; break; } } ///新增的目录 if (!isSameFile) { CopyDir(aimItem.FullName, newDirPath + @"\" + aimItem.Name, CopyType.AllFileAndDirectory); } } }