public bool Decompress(string fileToDecompress, string targetFolder, SecureString minga = null, string outputFile = null) { string output = string.Empty; try { if (!File.Exists(fileToDecompress)) { throw new FileNotFoundException("File to check" + fileToDecompress + " not found"); } if (!Directory.Exists(targetFolder)) { throw new DirectoryNotFoundException("Folder " + targetFolder + " not found"); } Logger.Log("Decompressing {0} file under {1}", fileToDecompress, targetFolder, LogLevel.Debug); var args = string.Format(@"x {0} ""{1}"" -o""{2}"" -y -r", (minga == null ? string.Empty : string.Format(" -p{0}", minga.SecureStringToString())), fileToDecompress, targetFolder); SevenZipReturnCode returnCode = run(args, out output); if (!string.IsNullOrEmpty(outputFile)) { Filesystem.CreateTextFile(outputFile, output); } if (SevenZipReturnCode.NoError != returnCode && SevenZipReturnCode.Warning != returnCode) { throw new SevenZipHandlerException("Decompression error: " + SevenZipReturnCodeExt.ToString(returnCode)); } Logger.Log("File decompressed", LogLevel.Debug); return(true); } catch (FileSystemItemException) { throw; } catch (SevenZipHandlerException) { throw; } catch (Exception ex) { throw new SevenZipHandlerException(ex.Message) { StackTrace = ex.StackTrace }; } }
public static string ToString(SevenZipReturnCode sevenZipReturnCode) { switch (sevenZipReturnCode) { case SevenZipReturnCode.NoError: return("No error"); case SevenZipReturnCode.Warning: return("Warning"); case SevenZipReturnCode.Fatal: return("Fatal error"); case SevenZipReturnCode.CommandLineError: return("Command line error"); case SevenZipReturnCode.NotEnoughMemoryForOperation: return("Not enough memory for operation"); case SevenZipReturnCode.UserAbort: return("User stopped the process"); } throw new SevenZipHandlerException("Unsupported 7Z return code: " + sevenZipReturnCode); }
public bool Compress(FileSystemInfo[] itemsToCompress, string fileToGenerate, string[] itemsToSkip = null, SecureString minga = null, string outputFile = null) { string output = string.Empty; try { if (itemsToCompress == null) { throw new FileSystemItemNullException(); } if (itemsToCompress.Length == 0) { throw new FileSystemItemNoItem(); } string itemsToCompressStr = string.Empty; itemsToCompress.ToList().ForEach(i => { if (!i.Exists) { throw new FileSystemItemNotFoundException(i); } itemsToCompressStr += @"""" + i.FullName + @""" "; }); string itemsToSkipStr = itemsToSkip == null || itemsToSkip.Length == 0 ? string.Empty : "-x!"; if (!string.IsNullOrEmpty(itemsToSkipStr)) { itemsToSkip.ToList().ForEach(i => { itemsToSkipStr += @"""" + i + @""" "; }); } if (File.Exists(fileToGenerate)) { Logger.Log("Deleting {0}", fileToGenerate, LogLevel.Debug); File.SetAttributes(fileToGenerate, FileAttributes.Normal); File.Delete(fileToGenerate); } Logger.Log("Compressing {0} into {1}", itemsToCompressStr, fileToGenerate, LogLevel.Debug); var args = string.Format(@"a -mhe{0} ""{1}"" {2} {3}", (minga == null ? string.Empty : string.Format(" -p{0}", minga.SecureStringToString())), fileToGenerate, itemsToCompressStr, itemsToSkipStr); SevenZipReturnCode returnCode = run(args, out output); if (!string.IsNullOrEmpty(outputFile)) { Filesystem.CreateTextFile(outputFile, output); } if (SevenZipReturnCode.NoError != returnCode && SevenZipReturnCode.Warning != returnCode) { throw new SevenZipHandlerException("Compression error: " + SevenZipReturnCodeExt.ToString(returnCode)); } if (!File.Exists(fileToGenerate)) { throw new FileNotFoundException("File to generate " + fileToGenerate + " not created"); } Logger.Log("File {0} created", fileToGenerate, LogLevel.Debug); return(true); } catch (FileSystemItemException) { throw; } catch (SevenZipHandlerException) { throw; } catch (Exception ex) { throw new SevenZipHandlerException(ex.Message) { StackTrace = ex.StackTrace }; } }
public static bool TryParse(string value, out SevenZipReturnCode result) { result = SevenZipReturnCode.UnknownCode; return(Enum.TryParse <SevenZipReturnCode>(value, out result)); }
public bool CreateHash(string[] foldersToHash, out string hashValue, string outputFile = null) { hashValue = string.Empty; string output = string.Empty; try { if (foldersToHash == null || foldersToHash.Length == 0) { throw new SevenZipHandlerException("No folder(s) to hash"); } StringBuilder foldersToHashStr = new StringBuilder(); foldersToHash.ToList().ForEach(f => { if (!Directory.Exists(f)) { throw new DirectoryNotFoundException("Folder to hash " + f + " not found"); } foldersToHashStr.AppendFormat(@"""" + f + @"\*.*"" "); }); Logger.Log("Generating hash from {0} folder(s) contents", foldersToHashStr.ToString(), LogLevel.Debug); var args = string.Format(@"h -scrcsha256 -r {0}", foldersToHashStr); var commandOutput = string.Empty; SevenZipReturnCode returnCode = run(args, out commandOutput); if (SevenZipReturnCode.NoError != returnCode) { throw new SevenZipHandlerException("Hash generation error: " + SevenZipReturnCodeExt.ToString(returnCode)); } Regex regularExpression = new Regex("SHA256 for data and names:[ ]+([A-Z0-9]{64,64}).*", RegexOptions.IgnoreCase); foreach (string line in commandOutput.Split(Environment.NewLine.ToArray())) { var match = regularExpression.Match(line); if (match.Success && match.Groups.Count > 0) { hashValue = match.Groups[0].Value; break; } } if (!string.IsNullOrEmpty(outputFile)) { Filesystem.CreateTextFile(outputFile, hashValue); } Logger.Log("Hash generated", LogLevel.Debug); return(true); } catch (SevenZipHandlerException) { throw; } catch (Exception ex) { throw new SevenZipHandlerException(ex.Message) { StackTrace = ex.StackTrace }; } }
public bool AddFiles(string inputFile, FileSystemInfo[] filesToAdd, SecureString minga = null, string outputFile = null) { string output = string.Empty; try { if (!File.Exists(inputFile)) { throw new FileNotFoundException("File " + inputFile + " not found"); } if (filesToAdd == null) { throw new FileSystemItemNullException(); } if (filesToAdd.Length == 0) { throw new FileSystemItemNoItem(); } string itemsToAddStr = string.Empty; filesToAdd.ToList().ForEach(i => { if (!i.Exists) { throw new FileSystemItemNotFoundException(i); } itemsToAddStr += @"""" + i.FullName + @""" "; }); Logger.Log("Adding {0} into {1}", itemsToAddStr, inputFile, LogLevel.Debug); var args = string.Format(@"a -mhe{0} -t7z ""{1}"" {2} {3}", (minga == null ? string.Empty : string.Format(" -p{0}", minga.SecureStringToString())), inputFile, filesToAdd); SevenZipReturnCode returnCode = run(args, out output); if (!string.IsNullOrEmpty(outputFile)) { Filesystem.CreateTextFile(outputFile, output); } if (SevenZipReturnCode.NoError != returnCode && SevenZipReturnCode.Warning != returnCode) { throw new SevenZipHandlerException("Update error: " + SevenZipReturnCodeExt.ToString(returnCode)); } Logger.Log("File {0} updated", inputFile, LogLevel.Debug); return(true); } catch (FileSystemItemException) { throw; } catch (SevenZipHandlerException) { throw; } catch (Exception ex) { throw new SevenZipHandlerException(ex.Message) { StackTrace = ex.StackTrace }; } }