Skip to content

Instantly share code, notes, and snippets.

@yemrekeskin
Last active December 21, 2015 19:49
Show Gist options
  • Save yemrekeskin/6357317 to your computer and use it in GitHub Desktop.
Save yemrekeskin/6357317 to your computer and use it in GitHub Desktop.
FileProcessor ; Encode , Zip (with password) --> Unzip , Decode
public interface IFileProcessor
{
void Process(string targetFile, string zippedFile);
void UndoProcess(string zippedFile, string targetFile, string fileName);
}
public class FileProcessor
:IFileProcessor
{
private string dummyFilePath = FileProcessorConstants.DummyFilePath;
private string encodedContent = String.Empty;
private string originContent = String.Empty;
public virtual void Process(string targetFile, string zippedFile)
{
// actual file = targetFile path
// encoded -> zipped aith password = zippedFile path
if (!File.Exists(targetFile))
throw new ApplicationException("");
try
{
originContent = FileHelper.GetStringByFile(targetFile); // ACTUAL FILE --> STRING
encodedContent = SecureHelper.Encode(originContent); // STRING --> ENCODED STRING
FileHelper.SetFileByString(dummyFilePath, encodedContent); // ENCODED STRING --> CUSTOM FILE FORMAT (with custom extention)
ZipHelper.Zip(dummyFilePath, zippedFile); // CUSTOM FILE FORMAT --> ZIP FILE
}
catch (Exception)
{
throw new ApplicationException("");
}
finally
{
encodedContent = String.Empty;
originContent = String.Empty;
}
}
public virtual void UndoProcess(string zippedFile, string targetFile, string fileName)
{
// filename : test.xlsx - with file extention
if (!File.Exists(zippedFile))
throw new ApplicationException("");
try
{
ZipHelper.UnZip(zippedFile, dummyFilePath); // ZIP FILE --> CUSTOM FILE FORMAT
encodedContent = FileHelper.GetStringByFile(dummyFilePath); // CUSTOM FILE FORMAT (with custom extention) --> ENCODED STRING
originContent = SecureHelper.Decode(encodedContent); // ENCODED --> STRING
FileHelper.SetFileByString(targetFile + fileName, originContent); // STRING --> ACTUAL FILE
}
catch (Exception)
{
throw new ApplicationException("");
}
finally
{
encodedContent = String.Empty;
originContent = String.Empty;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment