private void Compress_Click(object sender, RoutedEventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog(); if (saveFileDialog.ShowDialog() == true) { var dirrectoreyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); DirectoryInfo directoryInfo = new DirectoryInfo(dirrectoreyPath); //Assuming Test is your Folder @"D:\Test" FileInfo[] files = directoryInfo.GetFiles("*.dic"); //Getting Text files string lastCommonDictionaryPath = ""; foreach (FileInfo file in files) { if (file.Name.Contains(commonDictionaryPath)) { lastCommonDictionaryPath = file.Name; } } if (!string.IsNullOrEmpty(lastCommonDictionaryPath)) { var commonDic = File.ReadAllText(lastCommonDictionaryPath); archiveDictionary.InitializeCommonDictionary(commonDic); } var result = archiveDictionary.Compress(inputFileForCompress); File.WriteAllBytes(saveFileDialog.FileName, result.EncodeBytes.ToArray()); File.WriteAllText($"{saveFileDialog.FileName} (words = {result.DictionaryWordCount.ToString()}).dic", result.Dictionary); File.WriteAllText($"{commonDictionaryPath} (words = {result.CommonDictionaryWordCount.ToString()}).dic", result.CommonDictionary); Close(); } }
/// <summary> /// Compress a folder or file into a zip file /// </summary> /// <param name="source">File path of folder or file</param> /// <param name="destination">Folder path of output with zip name</param> /// <returns></returns> public static Task CompressAsync(string source, string destinationWithZipName, CancellationToken cancellationToken, bool deleteAfterArchiving = false) { return(Task.Run(() => { try { cancellationToken.ThrowIfCancellationRequested(); Archiver.Compress(source, destinationWithZipName); if (deleteAfterArchiving) { if (Directory.Exists(source)) { Directory.Delete(source, true); } else if (File.Exists(source)) { File.Delete(source); } } } catch (OperationCanceledException wasCanceled) { throw wasCanceled; } catch (ObjectDisposedException wasAreadyCanceled) { throw wasAreadyCanceled; } })); }
/// <summary> /// Контейнер приложения /// </summary> /// <param name="args"> /// Первый параметр - режим работы архиватора: /// c или compress - сжатие / /// d или decompress - деархивация; /// Второй параметр - исходный файл; /// Третий параметр - конечный файл /// </param> static void Main(string[] args) { #if DEBUG string mode = "decompress"; string open_path = @"C:\Users\A7tti\source\repos\MultithreadingArchiver\MultithreadingArchiver\bin\Release\movie.mkv.gz"; string save_path = @"C:\Users\A7tti\source\repos\MultithreadingArchiver\MultithreadingArchiver\bin\Release\"; #else if (args.Length < 3) { Console.WriteLine("To compress or decompress please enter args:\n[compress | decompress] [source file path] [destination directory path]"); return; } string mode = args[0]; string open_path = args[1]; string save_path = args[2]; #endif // Инициализация if (mode == "c" || mode == "compress") { Archiver.Compress(open_path, save_path); } else if (mode == "d" || mode == "decompress") { Archiver.Decompress(open_path, save_path); } else { Console.WriteLine("[compress | decompress]"); } // Мануальный выход из программы, если перехвачен ctrl+c Environment.Exit(0); }
public static void Main(string[] args) { if (args.Length < 3) { Usage(); } else { var command = args[0].ToLower(); if (!command.Equals("compress") && !command.Equals("decompress")) { Usage(); } else { var fromFile = args[1]; var toFile = args[2]; var archiver = new Archiver(new GZipWorker.GzipArchiver()); var result = false; if (command.Equals("compress")) { result = archiver.Compress(fromFile, toFile); } else if (command.Equals("decompress")) { result = archiver.Decompress(fromFile, toFile); } Environment.Exit(result ? 0 : 1); } } }
/// <summary> /// Compress a folder or file into a zip file /// </summary> /// <param name="source">File path of folder or file</param> /// <param name="destination">Folder path of output with zip name</param> /// <returns></returns> public static void Compress(string source, string destinationWithZipName, bool overwrite) { if (overwrite && File.Exists(destinationWithZipName)) { SafeFileManagement.DeleteFile(destinationWithZipName); } Archiver.Compress(source, destinationWithZipName, overwrite); }
void Build() { var pluginName = PluginName.Replace(" ", ""); string parentDir = ""; if (OutputDirectory == "") { parentDir = EditorUtility.OpenFolderPanel("Select Output Directory", UnityEngine.Application.dataPath.Replace("/Assets", ""), "Build"); } else { parentDir = EditorUtility.OpenFolderPanel("Select Output Directory", Path.GetDirectoryName(OutputDirectory), new DirectoryInfo(OutputDirectory).Name); } // exit if dialog cancelled if (parentDir == "") { return; } // create temporary output directory var outputDirPath = parentDir + "/" + pluginName; var outputDir = new DirectoryInfo(outputDirPath); outputDir.Create(); var createDirectoryTimeout = 0; while (!outputDir.Exists && createDirectoryTimeout < 100) { System.Threading.Thread.Sleep(50); createDirectoryTimeout++; } if (createDirectoryTimeout >= 100) { UnityEngine.Debug.LogError("Failed to create plugin directory."); return; } var pluginDll = outputDirPath + "/" + pluginName + ".dll"; var compilerArgs = "-t:library -out:" + pluginDll; // reference UnityEngine libs, mono libs, and package dlls used in current project var editorPath = Path.GetDirectoryName(EditorApplication.applicationPath); #if UNITY_EDITOR_OSX var unityLibsPath = editorPath + "/Unity.app/Contents/Managed/UnityEngine/"; var monoLibsPath = editorPath + "/Unity.app/Contents/MonoBleedingEdge/lib/mono/unity/"; #else var unityLibsPath = editorPath + "/Data/Managed/UnityEngine/"; var monoLibsPath = editorPath + "/Data/MonoBleedingEdge/lib/mono/unity/"; #endif var packageLibPath = UnityEngine.Application.dataPath.Replace("Assets", "/Library/ScriptAssemblies/"); compilerArgs += " -lib:\"" + unityLibsPath + "\",\"" + packageLibPath + "\""; foreach (var unityLib in Directory.GetFiles(unityLibsPath)) { if (unityLib.Contains(".dll")) { compilerArgs += " -r:\"" + Path.GetFileName(unityLib) + "\""; } } foreach (var packageLib in Directory.GetFiles(packageLibPath)) { if (packageLib.Contains(".dll") && !packageLib.Contains("Editor")) { compilerArgs += " -r:\"" + Path.GetFileName(packageLib) + "\""; } } // add project source files compilerArgs += " -recurse:" + UnityEngine.Application.dataPath + "/" + SourceDirectory + "/*.cs "; // get mono compiler from Unity install var compilerPath = Path.GetDirectoryName(EditorApplication.applicationPath); #if UNITY_EDITOR_OSX compilerPath += "/Unity.app/Contents/MonoBleedingEdge/bin/mcs"; #elif UNITY_EDITOR_WIN compilerPath += "/Data/MonoBleedingEdge/bin/mcs.bat"; #else compilerPath += "/Data/MonoBleedingEdge/bin/mcs"; #endif var compilerStartInfo = new ProcessStartInfo(); compilerStartInfo.FileName = compilerPath; compilerStartInfo.Arguments = compilerArgs; compilerStartInfo.RedirectStandardError = true; compilerStartInfo.UseShellExecute = false; var compilerProcess = new Process(); compilerProcess.StartInfo = compilerStartInfo; // capture errors var standardError = new System.Text.StringBuilder(); compilerProcess.ErrorDataReceived += (s, args) => standardError.AppendLine(args.Data); compilerProcess.Start(); compilerProcess.BeginErrorReadLine(); compilerProcess.WaitForExit(); // compiler tends to generate a lot of empty whitespace // at standard error var errorString = Regex.Replace(standardError.ToString(), @"^\r?\n?$", "", RegexOptions.Multiline); if (errorString.ToLower().Contains("error")) { UnityEngine.Debug.LogError("Plugin compilation failed with:\n" + errorString); return; } UnityEngine.Debug.Log("Plugin .dll compilation succeeded."); // Replace custom components with a proxy. // This proxy reloads the actual components at runtime // from the dll var proxiedPrefabs = new List <string>(); foreach (var assetBundleName in AssetDatabase.GetAllAssetBundleNames()) { foreach (var assetPath in AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName)) { if (assetPath.Contains(".prefab")) { var prefabContents = PrefabUtility.LoadPrefabContents(assetPath); foreach (var component in prefabContents.GetComponents <Component>()) { var componentType = component.GetType(); // if the component is located within the current project's scripting // assembly, swap it for the proxy that loads from the dll if (componentType.Assembly.FullName.Contains("Assembly-CSharp")) { var componentProxy = prefabContents.AddComponent <ComponentProxy>(); componentProxy.TypeName = componentType.FullName; componentProxy.PluginAssembly = pluginName; MonoBehaviour.DestroyImmediate(component); proxiedPrefabs.Add(assetPath); } } PrefabUtility.SaveAsPrefabAsset(prefabContents, assetPath); } } } // build asset bundles BuildPipeline.BuildAssetBundles(outputDirPath, BuildAssetBundleOptions.None, (BuildTarget)System.Enum.Parse(typeof(BuildTarget), TargetPlatform)); // remove extra files built from asset bundle export foreach (var filePath in Directory.GetFiles(outputDirPath)) { var fileName = Path.GetFileName(filePath); if (fileName.Contains(".manifest")) { File.Delete(filePath); } else if (fileName == pluginName) { File.Delete(filePath); } } UnityEngine.Debug.Log("Asset bundles exported."); // change proxies back to original components now bundle is exported foreach (var prefabPath in proxiedPrefabs) { var prefabContents = PrefabUtility.LoadPrefabContents(prefabPath); foreach (var proxy in prefabContents.GetComponents <ComponentProxy>()) { var componentType = System.Type.GetType(proxy.TypeName + ", Assembly-CSharp"); prefabContents.AddComponent(componentType); Component.DestroyImmediate(proxy); } PrefabUtility.SaveAsPrefabAsset(prefabContents, prefabPath); } // compress all files into a URack plugin archive var pluginArchive = parentDir + "/" + pluginName + "-" + PluginVersion + GetPlatformSuffix(TargetPlatform) + ".zip"; if (File.Exists(pluginArchive)) { File.Delete(pluginArchive); } Archiver.Compress(outputDirPath, pluginArchive); UnityEngine.Debug.Log("Plugin files packed."); // remove temp directory now it's archived Directory.Delete(outputDirPath, true); EditorUtility.RevealInFinder(pluginArchive); UnityEngine.Debug.Log("Plugin exported successfully."); // store plugin export settings for next export PlayerPrefs.SetString("URackExporter_PluginName_" + ProjectName, PluginName); PlayerPrefs.SetString("URackExporter_PluginVersion_" + ProjectName, PluginVersion); PlayerPrefs.SetString("URackExporter_SourceDirectory_" + ProjectName, SourceDirectory); PlayerPrefs.SetString("URackExporter_OutputDirectory_" + ProjectName, parentDir); PlayerPrefs.SetString("URackExporter_TargetPlatform_" + ProjectName, TargetPlatform); }