private async Task DecodeFolder(IEnumerable <string> encfolder, string rootpath = null) { foreach (var folder in encfolder) { if (!Directory.Exists(folder)) { Log("folder not found : {0}", folder); continue; } var decparent = rootpath; if (decparent == null) { decparent = Directory.GetParent(folder).FullName; } var encfoldername = (folder.EndsWith("\\")) ? Path.GetDirectoryName(folder) : folder; var i = encfoldername.LastIndexOf('\\'); if (i >= 0) { encfoldername = encfoldername.Substring(i + 1); } var decfoldername = CryptCarotDAV.DecryptFilename(encfoldername); if (decfoldername == null) { Log("foldername decode error : {0}", folder); continue; } var decfolder = Path.Combine(decparent, decfoldername); if (Directory.Exists(decfolder)) { Log("Exists : {0}", decfolder); continue; } try { Directory.CreateDirectory(decfolder); } catch (Exception ex) { Log("CreateDirectory failed : {0} {1}", decfolder, ex.Message); continue; } Log("OK : {0}->{1}", folder, decfolder); // subitems var subfiles = Directory.GetFiles(folder); var subdirs = Directory.GetDirectories(folder); await DecodeFile(subfiles, decfolder); await DecodeFolder(subdirs, decfolder); } }
private async Task DecodeFile(IEnumerable <string> encfiles, string rootpath = null) { foreach (var file in encfiles) { if (!File.Exists(file)) { Log("file not found : {0}", file); continue; } var decfilepath = rootpath; if (decfilepath == null) { decfilepath = Path.GetDirectoryName(file); } var encfilename = Path.GetFileName(file); var decfilename = CryptCarotDAV.DecryptFilename(encfilename); if (decfilename == null) { Log("filename decode error : {0}", file); continue; } var decfile = Path.Combine(decfilepath, decfilename); if (File.Exists(decfile)) { Log("Exists : {0}", decfile); continue; } using (var efile = File.OpenRead(file)) using (var dfile = File.OpenWrite(decfile)) using (var cfile = new CryptCarotDAV.CryptCarotDAV_DecryptStream(efile)) { try { await cfile.CopyToAsync(dfile, 81920, cts.Token); } catch (Exception ex) { Log("Decode Error : {0}->{1} {2}", file, decfile, ex.Message); continue; } } Log("OK : {0}->{1}", file, decfile); } }