public static List <LogInfo> Compress(EngineState s, CodeCommand cmd) { // Compress,<ArchiveType>,<SrcPath>,<DestArchive>,[CompressLevel],[UTF8|UTF16|UTF16BE|ANSI] List <LogInfo> logs = new List <LogInfo>(); Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_Compress)); CodeInfo_Compress info = cmd.Info as CodeInfo_Compress; ArchiveCompressFormat arcType = info.Format; string srcPath = StringEscaper.Preprocess(s, info.SrcPath); string destArchive = StringEscaper.Preprocess(s, info.DestArchive); ArchiveHelper.CompressLevel compLevel = ArchiveHelper.CompressLevel.Normal; if (info.CompressLevel != null) { compLevel = (ArchiveHelper.CompressLevel)info.CompressLevel; } Encoding encoding = info.Encoding; if (info.Encoding == null) { encoding = Encoding.UTF8; } // Path Security Check if (StringEscaper.PathSecurityCheck(destArchive, out string errorMsg) == false) { logs.Add(new LogInfo(LogState.Error, errorMsg)); return(logs); } if (Directory.Exists(destArchive)) { logs.Add(new LogInfo(LogState.Error, $"[{destArchive}] should be a file, not a directory")); return(logs); } else { if (File.Exists(destArchive)) { logs.Add(new LogInfo(LogState.Warning, $"File [{destArchive}] will be overwritten")); } } if (!Directory.Exists(srcPath) && !File.Exists(srcPath)) { logs.Add(new LogInfo(LogState.Error, $"Cannot find [{srcPath}]")); return(logs); } bool success; switch (arcType) { case ArchiveCompressFormat.Zip: success = ArchiveHelper.CompressManagedZip(srcPath, destArchive, compLevel, encoding); break; default: logs.Add(new LogInfo(LogState.Error, $"Compressing to [{arcType}] format is not supported")); return(logs); } if (success) { logs.Add(new LogInfo(LogState.Success, $"[{srcPath}] compressed to [{destArchive}]")); } else { logs.Add(new LogInfo(LogState.Error, $"Compressing [{srcPath}] failed")); } return(logs); }
public void Compress_FileTemplate(string arcType, string srcFilePath, string destArc, ArchiveHelper.CompressLevel level, string encodingStr = null) { // Compress,<ArchiveType>,<SrcPath>,<DestArchive>,[CompressLevel],[UTF8|UTF16|UTF16BE|ANSI] EngineState s = EngineTests.CreateEngineState(); string dirPath = StringEscaper.Preprocess(s, Path.Combine("%TestBench%", "CommandArchive")); string destDir = Path.Combine(dirPath, "Compress_Dest"); string compRootDir = Path.Combine(dirPath, "Compress_Comp"); string compDir = Path.Combine(compRootDir, destArc); string srcFullPath = Path.Combine(dirPath, srcFilePath); string destFullPath = Path.Combine(destDir, destArc); string srcFileName = Path.GetFileName(srcFilePath); try { Directory.CreateDirectory(destDir); Directory.CreateDirectory(compDir); string rawCode = $"Compress,{arcType},\"%TestBench%\\CommandArchive\\{srcFilePath}\",\"%TestBench%\\CommandArchive\\Compress_Dest\\{destArc}\""; if (encodingStr != null) { rawCode += "," + encodingStr; } EngineTests.Eval(s, rawCode, CodeType.Compress, ErrorCheck.Success, out CodeCommand cmd); Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_Compress)); CodeInfo_Compress info = cmd.Info as CodeInfo_Compress; ArchiveHelper.DecompressManaged(destFullPath, compDir, true, info.Encoding); using (FileStream srcStream = new FileStream(srcFullPath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (FileStream destStream = new FileStream(Path.Combine(compDir, srcFileName), FileMode.Open, FileAccess.Read, FileShare.Read)) { byte[] srcDigest = HashHelper.CalcHash(HashType.SHA256, srcStream); byte[] destDigest = HashHelper.CalcHash(HashType.SHA256, destStream); Assert.IsTrue(srcDigest.SequenceEqual(destDigest)); } } finally { Directory.Delete(destDir, true); Directory.Delete(compRootDir, true); } }