// @todo: use temp, random names for zip files public static void CopyDirectory(string Source, string Dest, ECopyOptions Options) { string SourceDirName = Path.GetFileName(Source); string DestDirName = Path.GetFileName(Dest); if (Options.HasFlag(ECopyOptions.IsUpload)) { if (!Directory.Exists(Source)) { return; } // Zip source directory string SourceZipPath = Path.Combine(Path.GetFullPath(Path.GetDirectoryName(Source)), SourceDirName + ".zip"); File.Delete(SourceZipPath); ZipFile Zip = new ZipFile(SourceZipPath); Zip.CompressionLevel = Ionic.Zlib.CompressionLevel.Level9; Zip.BufferSize = 0x10000; Zip.AddDirectory(Source, DestDirName); Zip.Save(); // Upload the zip file string DestWorkingDir = Path.GetDirectoryName(Dest).Replace("\\", "/"); string DestZipName = DestDirName + ".zip"; CopyFile(SourceZipPath, DestWorkingDir + "/" + DestZipName, true); if (!Options.HasFlag(ECopyOptions.DoNotReplace)) { Command(DestWorkingDir, "rm -rf \"" + DestDirName + "\"", "", null); } if (!Options.HasFlag(ECopyOptions.DoNotUnpack)) { // Unpack, if requested Command(DestWorkingDir, "unzip \"" + DestZipName + "\"", "", null); Command(DestWorkingDir, "rm \"" + DestZipName + "\"", "", null); } File.Delete(SourceZipPath); } else { // Zip source directory string SourceWorkingDir = Path.GetDirectoryName(Source).Replace("\\", "/"); string ZipCommand = "zip -0 -r -y -T " + SourceDirName + ".zip " + SourceDirName; Command(SourceWorkingDir, ZipCommand, "", null); // Download the zip file string SourceZipPath = Path.Combine(Path.GetDirectoryName(Source), SourceDirName + ".zip").Replace("\\", "/"); string DestZipPath = Path.Combine(Path.GetFullPath(Path.GetDirectoryName(Dest)), DestDirName + ".zip"); CopyFile(SourceZipPath, DestZipPath, false); if (!Options.HasFlag(ECopyOptions.DoNotReplace) && Directory.Exists(Dest)) { Directory.GetFiles(Dest, "*", SearchOption.AllDirectories).ToList().ForEach(Entry => { File.SetAttributes(Entry, FileAttributes.Normal); }); Directory.Delete(Dest, true); } if (!Options.HasFlag(ECopyOptions.DoNotUnpack)) { // Unpack, if requested using (ZipFile Zip = ZipFile.Read(DestZipPath)) { Zip.ToList().ForEach(Entry => { Entry.FileName = DestDirName + Entry.FileName.Substring(SourceDirName.Length); Entry.Extract(Path.GetDirectoryName(Dest), ExtractExistingFileAction.OverwriteSilently); }); } File.Delete(DestZipPath); } Command(SourceWorkingDir, "rm \"" + SourceDirName + ".zip\"", "", null); } }
// @todo: use temp, random names for zip files public static void CopyDirectory(string Source, string Dest, ECopyOptions Options) { string SourceDirName = Path.GetFileName(Source); string DestDirName = Path.GetFileName(Dest); if (Options.HasFlag(ECopyOptions.IsUpload)) { if (!Directory.Exists(Source)) { return; } // Zip source directory string SourceZipPath = Path.Combine(Path.GetFullPath(Path.GetDirectoryName(Source)), SourceDirName + ".zip"); File.Delete(SourceZipPath); ZipFile Zip = new ZipFile(SourceZipPath); Zip.CompressionLevel = Ionic.Zlib.CompressionLevel.Level9; Zip.BufferSize = 0x10000; Zip.AddDirectory(Source, DestDirName); Zip.Save(); // Upload the zip file string DestWorkingDir = Path.GetDirectoryName(Dest).Replace("\\", "/"); string DestZipName = DestDirName + ".zip"; CopyFile(SourceZipPath, DestWorkingDir + "/" + DestZipName, true); if (!Options.HasFlag(ECopyOptions.DoNotReplace)) { Command(DestWorkingDir, "rm -rf \"" + DestDirName + "\"", "", null); } if (!Options.HasFlag(ECopyOptions.DoNotUnpack)) { // Unpack, if requested Command(DestWorkingDir, "unzip \"" + DestZipName + "\"", "", null); Command(DestWorkingDir, "rm \"" + DestZipName + "\"", "", null); } File.Delete(SourceZipPath); } else { // Zip source directory string SourceWorkingDir = Path.GetDirectoryName(Source).Replace("\\", "/"); string ZipCommand = "zip -0 -r -y -T " + SourceDirName + ".zip " + SourceDirName; Command(SourceWorkingDir, ZipCommand, "", null); // Download the zip file string SourceZipPath = Path.Combine(Path.GetDirectoryName(Source), SourceDirName + ".zip").Replace("\\", "/"); string DestZipPath = Path.Combine(Path.GetFullPath(Path.GetDirectoryName(Dest)), DestDirName + ".zip"); CopyFile(SourceZipPath, DestZipPath, false); if (!Options.HasFlag(ECopyOptions.DoNotReplace) && Directory.Exists(Dest)) { Directory.GetFiles(Dest, "*", SearchOption.AllDirectories).ToList().ForEach(Entry => { File.SetAttributes(Entry, FileAttributes.Normal); }); Directory.Delete(Dest, true); } if (!Options.HasFlag(ECopyOptions.DoNotUnpack)) { // Unpack, if requested using (ZipFile Zip = ZipFile.Read(DestZipPath)) { Zip.ToList().ForEach(Entry => { Entry.FileName = DestDirName + Entry.FileName.Substring(SourceDirName.Length); Entry.Extract(Path.GetDirectoryName(Dest), ExtractExistingFileAction.OverwriteSilently); }); } File.Delete(DestZipPath); } Command(SourceWorkingDir, "rm \"" + SourceDirName + ".zip\"", "", null); } }