public string BackProcessExecute(string fileName) { string outFile = TempNameGenerator.GenerateTempNameFromFile(fileName); PasswordDeriveBytes password = new PasswordDeriveBytes(KeyBlock, saltValueBytes, hashAlgorithm, passwordIterations); byte[] keyBytes = password.GetBytes(keySize / 8); using (RijndaelManaged symmetricKey = new RijndaelManaged()) { symmetricKey.Mode = CipherMode.CBC; ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, IV); using (FileStream fsread = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { using (CryptoStream cryptoStream = new CryptoStream(fsread, decryptor, CryptoStreamMode.Read)) { using (BinaryWriter fsDecrypted = new BinaryWriter(new FileStream(outFile, FileMode.Create, FileAccess.Write))) { byte[] file_data = new byte[fsread.Length]; int decryptedByteCount = cryptoStream.Read(file_data, 0, file_data.Length); fsDecrypted.Write(file_data, 0, decryptedByteCount); fsDecrypted.Flush(); fsDecrypted.Close(); } cryptoStream.Close(); } } } return(outFile); }
public string ProcessExecute(string fileName) { string outFile = TempNameGenerator.GenerateTempNameFromFile(fileName); PasswordDeriveBytes password = new PasswordDeriveBytes(KeyBlock, saltValueBytes, hashAlgorithm, passwordIterations); byte[] keyBytes = password.GetBytes(keySize / 8); using (RijndaelManaged symmetricKey = new RijndaelManaged()) { symmetricKey.Mode = CipherMode.CBC; ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, IV); using (FileStream fsInput = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { using (FileStream fsEncrypted = new FileStream(outFile, FileMode.Create, FileAccess.Write)) { using (CryptoStream cryptoStream = new CryptoStream(fsEncrypted, encryptor, CryptoStreamMode.Write)) { byte[] bytearrayinput = new byte[fsInput.Length]; fsInput.Read(bytearrayinput, 0, bytearrayinput.Length); cryptoStream.Write(bytearrayinput, 0, bytearrayinput.Length); cryptoStream.FlushFinalBlock(); cryptoStream.Close(); } fsEncrypted.Close(); } fsInput.Close(); } } return(outFile); }
public string ProcessExecute(string fileName) { string outFile = TempNameGenerator.GenerateTempNameFromFile(fileName); using (FileStream outFileStream = new FileStream(outFile, FileMode.Create, FileAccess.Write)) { using (ZOutputStream outZStream = new ZOutputStream(outFileStream, zlibConst.Z_BEST_COMPRESSION)) { using (FileStream inFileStream = new System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read)) { transfer.Transfer(inFileStream, outZStream); } } } return(outFile); }
public string BackProcessExecute(string fileName) { int data = 0; int stopByte = -1; string out_file = TempNameGenerator.GenerateTempNameFromFile(fileName); using (FileStream outFileStream = new FileStream(out_file, FileMode.Create)) { using (ZInputStream inZStream = new ZInputStream(File.Open(fileName, FileMode.Open, FileAccess.Read))) { while (stopByte != (data = inZStream.Read())) { byte _dataByte = (byte)data; outFileStream.WriteByte(_dataByte); } inZStream.Close(); } outFileStream.Close(); } return(out_file); }
public void Compress(CompressorOption option) { bool ignore_all = false; PrimeArchiverType type = PrimeArchiverType.Nothing; processors.Clear(); processors.Add(new CompressFile()); if (!String.IsNullOrEmpty(option.Password)) { type |= PrimeArchiverType.Password; EncryptFile ef = new EncryptFile(); ef.SetKey(option.Password); processors.Add(ef); } header = new Header.Header(); HeaderItemPath hip = new HeaderItemPath(); try { string temp_archive = TempNameGenerator.GenerateTempNameFromFile(option.Output); using (FileStream archiveStream = new FileStream(temp_archive, FileMode.Create, FileAccess.Write)) { IncludesPathCreate(option); for (int i = 0; i < header.Items.Count; i++) { HeaderItemPath hip_file = new HeaderItemPath(); try { Process(header.Items[i].AbsolutePath); if (header.Items[i].Length != 0) { hip_file.UpdateCurrentPath(header.Items[i].AbsolutePath); foreach (IProcessFile processor in processors) { hip_file.UpdateCurrentPath(processor.ProcessExecute(hip_file.GetCurrentPath())); } using (FileStream fr = new FileStream(hip_file.GetCurrentPath(), FileMode.Open, FileAccess.Read)) { header.Items[i].SetLentgh(fr.Length); transfer.Transfer(fr, archiveStream); } } } catch (Exception ex) { if (ignore_all) { continue; } OperationErrorAction action = OperationErrorAction.Abort; switch (action) { case OperationErrorAction.Abort: throw ex; case OperationErrorAction.Ignore: continue; case OperationErrorAction.IgnoreAll: ignore_all = true; continue; case OperationErrorAction.Replay: i--; break; } } finally { hip_file.ClearTemporeryPathes(false); } } } string header_path = TempNameGenerator.GenerateTempNameFromFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "header.dat")); hip.UpdateCurrentPath(header_path); using (FileStream fs = new FileStream(hip.GetCurrentPath(), FileMode.Create, FileAccess.Write)) { new StreamTransfer().Transfer(new MemoryStream(header.ToArray()), fs); } foreach (IProcessFile processor in processors) { hip.UpdateCurrentPath(processor.ProcessExecute(hip.GetCurrentPath())); } using (FileStream endArchiveStream = new FileStream(option.Output, FileMode.Create, FileAccess.Write)) { using (FileStream fr = new FileStream(hip.GetCurrentPath(), FileMode.Open, FileAccess.Read)) { endArchiveStream.WriteByte((byte)type); int after_processors_header_length = (int)fr.Length; endArchiveStream.Write(BitConverter.GetBytes(after_processors_header_length), 0, sizeof(int)); transfer.Transfer(fr, endArchiveStream); } using (FileStream fr = new FileStream(temp_archive, FileMode.Open, FileAccess.Read)) { transfer.Transfer(fr, endArchiveStream); } Operations.DeleteFile(temp_archive); } } catch (Exception ex) { Process(ex.Message); } finally { hip.ClearTemporeryPathes(true); processors.Clear(); } }
public void Decompress(string source, string output, string password) { CompressorOption option = new CompressorOption(); processors.Clear(); FileStream fs = null; BinaryReader br = null; Header.Header unpack_header = new Header.Header(); try { using (fs = new FileStream(source, FileMode.Open, FileAccess.Read)) { using (br = new BinaryReader(fs)) { PrimeArchiverType type = (PrimeArchiverType)br.ReadByte(); if ((type & PrimeArchiverType.Password) == PrimeArchiverType.Password) { EncryptFile ef = new EncryptFile(); ef.SetKey(password); processors.Add(ef); } processors.Add(new CompressFile()); byte[] header_length_arr = br.ReadBytes(sizeof(int)); byte[] header_arr = br.ReadBytes(BitConverter.ToInt32(header_length_arr, 0)); string header_path = TempNameGenerator.GenerateTempNameFromFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "header.dat")); HeaderItemPath hip_header = new HeaderItemPath(); hip_header.UpdateCurrentPath(header_path); using (FileStream fw = new FileStream(header_path, FileMode.Create, FileAccess.Write)) { transfer.Transfer(new MemoryStream(header_arr), fw); } foreach (IProcessFile processor in processors) { hip_header.UpdateCurrentPath(processor.BackProcessExecute(hip_header.GetCurrentPath())); } using (FileStream fr = new FileStream(hip_header.GetCurrentPath(), FileMode.Open, FileAccess.Read)) { byte[] header_body = new byte[fr.Length]; int count = fr.Read(header_body, 0, (int)fr.Length); unpack_header.Parse(header_body); } hip_header.ClearTemporeryPathes(true); foreach (HeaderItem item in unpack_header.Items) { if (item.Length == 0) { string full_path = Path.Combine(output, item.RelativePath); Process(full_path); Operations.CreateDirectory(full_path); } else { string full_path = Path.Combine(output, item.RelativePath); Process(full_path); byte[] file_body = br.ReadBytes((int)item.Length); HeaderItemPath hip_file = new HeaderItemPath(); hip_file.UpdateCurrentPath(TempNameGenerator.GenerateTempNameFromFile(full_path)); using (FileStream fw = new FileStream(hip_file.GetCurrentPath(), FileMode.Create, FileAccess.Write)) { transfer.Transfer(new MemoryStream(file_body), fw); } foreach (IProcessFile processor in processors) { hip_file.UpdateCurrentPath(processor.BackProcessExecute(hip_file.GetCurrentPath())); } Operations.MoveFile(hip_file.GetCurrentPath(), full_path); hip_file.RemoveLast(); hip_file.ClearTemporeryPathes(true); } } } } } catch (Exception ex) { Process(ex.Message); } finally { if (br != null) { br.Close(); } if (fs != null) { fs.Close(); } } }