Thứ Tư, 9 tháng 11, 2016

[Base64Converter] Convert between file to Base64 file

Help
  Support convert between File and Base64 File
  Support GZip compress file data before convert

Use
  You can select multiple file and drag to execute convert file
  The file convert result output at same folder

Drag drop file to execute convert


Example File To Base64 File
   Base64Converter.exe File1.txt File2.txt
   Output result: File1.txt.base64 and File2.txt.base64

Example Base64 File To File
   Base64Converter.exe File1.txt.base64 File2.txt.base64
   Output result: File1_1.txt and File2_1.txt

Download Link: Base64Converter.rar
New Version: Base64Converter_V11

Code convert file to Base64 file
using System;
using System.IO;
 
namespace Base64Converter
{
    public class ConvertToBase64
    {
        private readonly string _file;
 
        private ConvertToBase64(string file)
        {
            _file = file;
        }
 
        private void Parse()
        {
            try
            {
                var bytes = File.ReadAllBytes(_file);
                var base64String = Convert.ToBase64String(bytes);
                var base64Path = Utils.GetFileName(_file + ".base64");
                File.WriteAllText(base64Path, _file + Environment.NewLine);
                File.AppendAllText(base64Path, base64String);
                Logger.Write("Base64 File: {0}", base64Path);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message, ex);
            }
        }
        public static void Parse(string file)
        {
            var converter = new ConvertToBase64(file);
            converter.Parse();
        }
    }
}

Code convert Base64 file to origin file
using System;
using System.IO;
 
namespace Base64Converter
{
    public class ConvertToFile
    {
        private readonly string _file;
 
        private ConvertToFile(string file)
        {
            _file = file;
        }
 
        private void Parse()
        {
            try
            {
                var allLines = File.ReadAllLines(_file);
                var filePath = _file.Substring(0, _file.Length - 7);
                var base64String = string.Empty;
                switch (allLines.Length)
                {
                    case 1:
                        base64String = allLines[0];
                        break;
                    case 2:
                        filePath = Utils.GetFileName(allLines[0]);
                        base64String = allLines[1];
                        break;
                    default:
                        base64String = string.Join(string.Empty, allLines);
                        break;
                }
 
                var bytes = Convert.FromBase64String(base64String);
                File.WriteAllBytes(filePath, bytes);
                Logger.Write("File: {0}", filePath);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message, ex);
            }
        }
 
        public static void Parse(string file)
        {
            var converter = new ConvertToFile(file);
            converter.Parse();
        }
    }
}