public void ConvertTudo() { string sourcePath = "..\\..\\filestest\\input"; string resultPath = "..\\..\\filestest\\output"; string[] files = Directory.GetFiles(sourcePath); string[] deleteFiles = Directory.GetFiles(resultPath, "*.txt"); deleteFiles.ToList().ForEach(f => File.Delete(f)); foreach (var item in files) { string[] filesSplit = item.Split('\\'); var a = File.OpenRead(item); string s = Base64Converter.ToBase64String(a); var t = FinderMimeType.GetMimeType(Convert.FromBase64String(s)); var obj = Base64Converter.FromBase64String(s, out IMimeType mime); File.WriteAllText(resultPath + "\\" + filesSplit[filesSplit.Length - 1].Replace('.', '_') + ".txt", s); if (obj.GetType() == typeof(MemoryStream)) { MemoryStream memory = (MemoryStream)obj; using (FileStream fileStream = new FileStream(resultPath + "\\" + filesSplit[filesSplit.Length - 1].Replace('.', '_') + $".{mime.Extension}", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 4096, FileOptions.None)) { fileStream.Write(memory.ToArray(), 0, memory.ToArray().Length); } } //string base64 = imageResult.ToBase64(); } }
/// <summary> /// Decodifica o base64 para o objeto desejado /// </summary> /// <param name="input">String base64</param> /// <param name="mime">Tipo de objeto resultante da Decodificação</param> /// <returns>Objeto decodificado</returns> public static object FromBase64Object(string input, out IMimeType mime) { if (string.IsNullOrEmpty(input)) { throw new ArgumentException("message", nameof(input)); } byte[] result = Convert.FromBase64String(input); if (FinderMimeType.IsString(result)) { mime = new MimeType("#string#", "", null); return(Encoding.UTF8.GetString(result)); } else { var _mime = FinderMimeType.GetMimeType(result); MemoryStream m = new MemoryStream(result); mime = _mime; return(m); } }