From a224a91e694995495142a03f593245e919bc55e4 Mon Sep 17 00:00:00 2001 From: Artem Kuznetsov Date: Wed, 25 May 2022 11:24:05 +0300 Subject: [PATCH] =?UTF-8?q?refactor:=20=D0=9F=D0=B5=D1=80=D0=B5=D0=B8?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D0=BE=D0=B2=D0=B0=D0=BD=20=D0=BA=D0=BE=D0=BC?= =?UTF-8?q?=D0=BF=D0=BE=D0=BD=D0=B5=D0=BD=D1=82;=20=D0=98=D0=B7=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D0=B5=D0=BD=D1=8B=20GUID;=20=D0=98=D0=B7=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D0=B5=D0=BD=20=D0=BD=D0=BE=D0=BC=D0=B5=D1=80=20?= =?UTF-8?q?=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D0=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/release.yml | 6 +- .gitignore | 4 +- build/build.bat | 6 +- build/package-loader.os | 2 +- build/packagedef | 2 +- src/NUnitTests/NUnitTests.csproj | 6 +- src/NUnitTests/Tests/external.os | 4 +- src/TestApp/TestApp.csproj | 6 +- src/oscript-component.sln | 29 -- src/oscript-deflate.sln | 29 ++ .../DeflatePacker.cs | 434 +++++++++--------- .../oscript-deflate.csproj} | 4 +- 12 files changed, 266 insertions(+), 266 deletions(-) delete mode 100644 src/oscript-component.sln create mode 100644 src/oscript-deflate.sln rename src/{oscript-component => oscript-deflate}/DeflatePacker.cs (97%) rename src/{oscript-component/oscript-component.csproj => oscript-deflate/oscript-deflate.csproj} (83%) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 2dcf200..c6f3d91 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -56,15 +56,15 @@ jobs: dotnet restore ./src dotnet msbuild ./src -property:Configuration=Release mkdir ./build/bin/ - cp -f ./src/oscript-component/bin/Release/net452/*.dll ./build/bin/ - cp -f ./src/oscript-component/bin/Release/net452/*.xml ./build/bin/ + cp -f ./src/oscript-deflate/bin/Release/net452/*.dll ./build/bin/ + cp -f ./src/oscript-deflate/bin/Release/net452/*.xml ./build/bin/ rm -f ./build/bin/OneScript*.* ./build/bin/ScriptEngine*.* # Генерация документации - name: Генерация документации run: | chmod +x ./build/OneScriptDocumenter/OneScriptDocumenter.exe - ./build/OneScriptDocumenter/OneScriptDocumenter.exe json ./build/bin/syntaxHelp.json ./src/oscript-component/bin/Release/net452/oscript-component.dll + ./build/OneScriptDocumenter/OneScriptDocumenter.exe json ./build/bin/syntaxHelp.json ./src/oscript-deflate/bin/Release/net452/oscript-deflate.dll # Сборка пакета OneScript - name: Сборка пакета diff --git a/.gitignore b/.gitignore index 4e2ab6d..8bee675 100644 --- a/.gitignore +++ b/.gitignore @@ -3,8 +3,8 @@ \.vscode/ src/NUnitTests/bin/ src/NUnitTests/obj/ -src/oscript-component/bin/ -src/oscript-component/obj/ +src/oscript-deflate/bin/ +src/oscript-deflate/obj/ src/TestApp/bin/ src/TestApp/obj/ test/ diff --git a/build/build.bat b/build/build.bat index f7dc3af..1290b3a 100644 --- a/build/build.bat +++ b/build/build.bat @@ -10,15 +10,15 @@ echo %~dp0 @rd /S /Q "%~dp0bin" @mkdir "%~dp0bin" -@xcopy %~dp0..\src\oscript-component\bin\Release\net452\*.dll %~dp0bin\ -@xcopy %~dp0..\src\oscript-component\bin\Release\net452\*.xml %~dp0bin\ +@xcopy %~dp0..\src\oscript-deflate\bin\Release\net452\*.dll %~dp0bin\ +@xcopy %~dp0..\src\oscript-deflate\bin\Release\net452\*.xml %~dp0bin\ @del /F /Q "%~dp0bin\OneScript*.*" "%~dp0bin\ScriptEngine*.*" "%~dp0bin\DotNetZip*.*" "%~dp0bin\Newtonsoft*.*" @"C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" curl -o "%~dp0OneScriptDocumenter.zip" https://github.com/dmpas/OneScriptDocumenter/releases/download/1.0.14/documenter.zip @"C:\Program Files\7-Zip\7z.exe" x -o%~dp0OneScriptDocumenter -y %~dp0OneScriptDocumenter.zip @del /F /Q "%~dp0OneScriptDocumenter*.*" -@%~dp0OneScriptDocumenter\OneScriptDocumenter.exe json %~dp0bin\syntaxHelp.json %~dp0..\src\oscript-component\bin\Release\net452\oscript-component.dll +@%~dp0OneScriptDocumenter\OneScriptDocumenter.exe json %~dp0bin\syntaxHelp.json %~dp0..\src\oscript-deflate\bin\Release\net452\oscript-deflate.dll @rd /S /Q "%~dp0OneScriptDocumenter" diff --git a/build/package-loader.os b/build/package-loader.os index 918c58c..bfc272a 100644 --- a/build/package-loader.os +++ b/build/package-loader.os @@ -3,7 +3,7 @@ СтандартнаяОбработка = Ложь; Отказ = Ложь; - ПодключитьВнешнююКомпоненту(ОбъединитьПути(КаталогБиблиотеки, "bin", "oscript-component.dll")); + ПодключитьВнешнююКомпоненту(ОбъединитьПути(КаталогБиблиотеки, "bin", "oscript-deflate.dll")); КонецПроцедуры diff --git a/build/packagedef b/build/packagedef index 45580c2..e5ad5ba 100644 --- a/build/packagedef +++ b/build/packagedef @@ -1,6 +1,6 @@ Описание .Имя("deflator") - .Версия("0.2.0") + .Версия("0.2.1") .Автор("Artem Kuznetsov") .АдресАвтора("ArKuznetsov@gmail.com") .Описание("Упаковщик deflate для oscript") diff --git a/src/NUnitTests/NUnitTests.csproj b/src/NUnitTests/NUnitTests.csproj index dc27130..7ce40d8 100644 --- a/src/NUnitTests/NUnitTests.csproj +++ b/src/NUnitTests/NUnitTests.csproj @@ -6,9 +6,9 @@ AnyCPU - - {EA173D06-CCD2-45D5-A263-DC866289517A} - oscript-component + + {6C4E3728-D8A7-4944-BD4A-F0B259F6712B} + oscript-deflate diff --git a/src/NUnitTests/Tests/external.os b/src/NUnitTests/Tests/external.os index 5665a2b..9370be1 100644 --- a/src/NUnitTests/Tests/external.os +++ b/src/NUnitTests/Tests/external.os @@ -35,8 +35,8 @@ Процедура ПередЗапускомТестов() - ПутьККомпоненте = ОбъединитьПути(ТекущийСценарий().Каталог, "src", "oscript-component", "bin"); - ПутьККомпоненте = ОбъединитьПути(ПутьККомпоненте, "Debug", "net452", "oscript-component.dll"); + ПутьККомпоненте = ОбъединитьПути(ТекущийСценарий().Каталог, "src", "oscript-deflate", "bin"); + ПутьККомпоненте = ОбъединитьПути(ПутьККомпоненте, "Debug", "net452", "oscript-deflate.dll"); Попытка ПодключитьВнешнююКомпоненту(ПутьККомпоненте); diff --git a/src/TestApp/TestApp.csproj b/src/TestApp/TestApp.csproj index a82cff7..ffc0294 100644 --- a/src/TestApp/TestApp.csproj +++ b/src/TestApp/TestApp.csproj @@ -9,9 +9,9 @@ - - {EA173D06-CCD2-45D5-A263-DC866289517A} - oscript-component + + {6C4E3728-D8A7-4944-BD4A-F0B259F6712B} + oscript-deflate \ No newline at end of file diff --git a/src/oscript-component.sln b/src/oscript-component.sln deleted file mode 100644 index cdb5222..0000000 --- a/src/oscript-component.sln +++ /dev/null @@ -1,29 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2012 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "oscript-component", "oscript-component\oscript-component.csproj", "{EA173D06-CCD2-45D5-A263-DC866289517A}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestApp", "TestApp\TestApp.csproj", "{EFFB5754-9D8D-4014-A5A4-6BFFCFBA0F80}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NUnitTests", "NUnitTests\NUnitTests.csproj", "{9A739C32-D551-43B0-920A-D9C53367BC38}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {EA173D06-CCD2-45D5-A263-DC866289517A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {EA173D06-CCD2-45D5-A263-DC866289517A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {EA173D06-CCD2-45D5-A263-DC866289517A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {EA173D06-CCD2-45D5-A263-DC866289517A}.Release|Any CPU.Build.0 = Release|Any CPU - {EFFB5754-9D8D-4014-A5A4-6BFFCFBA0F80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {EFFB5754-9D8D-4014-A5A4-6BFFCFBA0F80}.Debug|Any CPU.Build.0 = Debug|Any CPU - {EFFB5754-9D8D-4014-A5A4-6BFFCFBA0F80}.Release|Any CPU.ActiveCfg = Release|Any CPU - {EFFB5754-9D8D-4014-A5A4-6BFFCFBA0F80}.Release|Any CPU.Build.0 = Release|Any CPU - {9A739C32-D551-43B0-920A-D9C53367BC38}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {9A739C32-D551-43B0-920A-D9C53367BC38}.Debug|Any CPU.Build.0 = Debug|Any CPU - {9A739C32-D551-43B0-920A-D9C53367BC38}.Release|Any CPU.ActiveCfg = Release|Any CPU - {9A739C32-D551-43B0-920A-D9C53367BC38}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection -EndGlobal diff --git a/src/oscript-deflate.sln b/src/oscript-deflate.sln new file mode 100644 index 0000000..6b94eb6 --- /dev/null +++ b/src/oscript-deflate.sln @@ -0,0 +1,29 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{C9E6B10A-8892-47AD-B5BF-40C0581F559A}") = "oscript-deflate", "oscript-deflate\oscript-deflate.csproj", "{6C4E3728-D8A7-4944-BD4A-F0B259F6712B}" +EndProject +Project("{C9E6B10A-8892-47AD-B5BF-40C0581F559A}") = "TestApp", "TestApp\TestApp.csproj", "{CD178D0F-A42A-4E50-9627-FD7915806C36}" +EndProject +Project("{C9E6B10A-8892-47AD-B5BF-40C0581F559A}") = "NUnitTests", "NUnitTests\NUnitTests.csproj", "{D0F802A1-49BA-4045-B632-4230A4E6FFB6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6C4E3728-D8A7-4944-BD4A-F0B259F6712B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6C4E3728-D8A7-4944-BD4A-F0B259F6712B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6C4E3728-D8A7-4944-BD4A-F0B259F6712B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6C4E3728-D8A7-4944-BD4A-F0B259F6712B}.Release|Any CPU.Build.0 = Release|Any CPU + {CD178D0F-A42A-4E50-9627-FD7915806C36}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CD178D0F-A42A-4E50-9627-FD7915806C36}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CD178D0F-A42A-4E50-9627-FD7915806C36}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CD178D0F-A42A-4E50-9627-FD7915806C36}.Release|Any CPU.Build.0 = Release|Any CPU + {D0F802A1-49BA-4045-B632-4230A4E6FFB6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D0F802A1-49BA-4045-B632-4230A4E6FFB6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D0F802A1-49BA-4045-B632-4230A4E6FFB6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D0F802A1-49BA-4045-B632-4230A4E6FFB6}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/src/oscript-component/DeflatePacker.cs b/src/oscript-deflate/DeflatePacker.cs similarity index 97% rename from src/oscript-component/DeflatePacker.cs rename to src/oscript-deflate/DeflatePacker.cs index f8a0ae4..ad12c71 100644 --- a/src/oscript-component/DeflatePacker.cs +++ b/src/oscript-deflate/DeflatePacker.cs @@ -1,34 +1,34 @@ -/*---------------------------------------------------------- -Use of this source code is governed by an MIT-style -license that can be found in the LICENSE file or at -https://opensource.org/licenses/MIT. ----------------------------------------------------------- -// Codebase: https://github.com/ArKuznetsov/oscript-deflate/ -----------------------------------------------------------*/ - -using System.IO; -using System.IO.Compression; -using ScriptEngine.Machine; -using ScriptEngine.Machine.Contexts; -using ScriptEngine.HostedScript.Library.Binary; - -namespace oscriptcomponent -{ - /// - /// Предоставляет методы для упаковки / распаковки данных по алгоритму Deflate - /// - [ContextClass("УпаковщикDeflate", "DeflatePacker")] - public class DeflatePacker : AutoContext - { - - /// - /// Выполняет упаковку двоичных данных по алгоритму Deflate - /// - /// ДвоичныеДанные. Данные для упаковки. - /// Число. Уровень сжатия (0-2). - /// ДвоичныеДанные - Результат упаковки - [ContextMethod("УпаковатьДанные")] - public IValue CompressData(IValue InputData, int OutputCompressionLevel = 2) +/*---------------------------------------------------------- +Use of this source code is governed by an MIT-style +license that can be found in the LICENSE file or at +https://opensource.org/licenses/MIT. +---------------------------------------------------------- +// Codebase: https://github.com/ArKuznetsov/oscript-deflate/ +----------------------------------------------------------*/ + +using System.IO; +using System.IO.Compression; +using ScriptEngine.Machine; +using ScriptEngine.Machine.Contexts; +using ScriptEngine.HostedScript.Library.Binary; + +namespace oscriptcomponent +{ + /// + /// Предоставляет методы для упаковки / распаковки данных по алгоритму Deflate + /// + [ContextClass("УпаковщикDeflate", "DeflatePacker")] + public class DeflatePacker : AutoContext + { + + /// + /// Выполняет упаковку двоичных данных по алгоритму Deflate + /// + /// ДвоичныеДанные. Данные для упаковки. + /// Число. Уровень сжатия (0-2). + /// ДвоичныеДанные - Результат упаковки + [ContextMethod("УпаковатьДанные")] + public IValue CompressData(IValue InputData, int OutputCompressionLevel = 2) { MemoryStreamContext inputStream; @@ -38,29 +38,29 @@ public IValue CompressData(IValue InputData, int OutputCompressionLevel = 2) inputStream = MemoryStreamContext.Constructor(); data.OpenStreamForRead().CopyTo(inputStream); inputStream.Seek(0, StreamPositionEnum.Begin); - } - else - { - throw RuntimeException.InvalidArgumentType("InputData"); - } - - MemoryStreamContext outputStream = MemoryStreamContext.Constructor(); - - CompressStream(inputStream, outputStream); - - return outputStream.CloseAndGetBinaryData(); - - } - - /// - /// Выполняет распаковку данных по алгоритму Deflate - /// - /// Поток. Исходный поток для распаковки. - /// ДвоичныеДанные - Результат распаковки - [ContextMethod("РаспаковатьДанные")] - public IValue DecompressData(IValue InputData) - { - + } + else + { + throw RuntimeException.InvalidArgumentType("InputData"); + } + + MemoryStreamContext outputStream = MemoryStreamContext.Constructor(); + + CompressStream(inputStream, outputStream); + + return outputStream.CloseAndGetBinaryData(); + + } + + /// + /// Выполняет распаковку данных по алгоритму Deflate + /// + /// Поток. Исходный поток для распаковки. + /// ДвоичныеДанные - Результат распаковки + [ContextMethod("РаспаковатьДанные")] + public IValue DecompressData(IValue InputData) + { + MemoryStreamContext inputStream; if (InputData.AsObject() is BinaryDataContext data) @@ -68,167 +68,167 @@ public IValue DecompressData(IValue InputData) inputStream = MemoryStreamContext.Constructor(); data.OpenStreamForRead().CopyTo(inputStream); inputStream.Seek(0, StreamPositionEnum.Begin); - } - else - { - throw RuntimeException.InvalidArgumentType("InputData"); - } - - MemoryStreamContext outputStream = MemoryStreamContext.Constructor(); - - DecompressStream(inputStream, outputStream); - - return outputStream.CloseAndGetBinaryData(); - - } - - /// - /// Выполняет упаковку входящего потока по алгоритму Deflate - /// - /// Поток. Исходный поток для упаковки. - /// Поток. Результат упаковки. - /// Число. Уровень сжатия (0-2). - [ContextMethod("УпаковатьПоток")] - public void CompressStream(IValue InputStream, IValue OutputStream, int OutputCompressionLevel = 2) - { - - CompressionLevel streamCompressionLevel; - - switch (OutputCompressionLevel) - { - case 0: - streamCompressionLevel = CompressionLevel.NoCompression; - break; - case 1: - streamCompressionLevel = CompressionLevel.Fastest; - break; - case 2: - streamCompressionLevel = CompressionLevel.Optimal; - break; - default: - streamCompressionLevel = CompressionLevel.Optimal; - break; - } - - DeflateStream compressor; - - if (OutputStream.AsObject() is IStreamWrapper outputStreamWrapper) - { - compressor = new DeflateStream(outputStreamWrapper.GetUnderlyingStream(), streamCompressionLevel, true); - } - else - { - throw RuntimeException.InvalidArgumentType("OutputStream"); - } - - if (InputStream.AsObject() is IStreamWrapper inputStreamWrapper) - { - inputStreamWrapper.GetUnderlyingStream().CopyTo(compressor); - compressor.Close(); - } - else - { - throw RuntimeException.InvalidArgumentType("InputStream"); - } - - } - - /// - /// Выполняет распаковку входящего потока по алгоритму Deflate - /// - /// Поток. Исходный поток для распаковки. - /// Поток. Результат распаковки. - [ContextMethod("РаспаковатьПоток")] - public void DecompressStream(IValue InputStream, IValue OutputStream) - { - - DeflateStream decompressor; - - if (InputStream.AsObject() is IStreamWrapper inputStreamWrapper) - { - decompressor = new DeflateStream(inputStreamWrapper.GetUnderlyingStream(), CompressionMode.Decompress, true); - } - else - { - throw RuntimeException.InvalidArgumentType("InputStream"); - } - - if (OutputStream.AsObject() is IStreamWrapper outputStreamWrapper) - { - decompressor.CopyTo(outputStreamWrapper.GetUnderlyingStream()); - decompressor.Close(); - } - else - { - throw RuntimeException.InvalidArgumentType("OutputStream"); - } - - } - - /// - /// Выполняет упаковку указанного файла по алгоритму Deflate - /// - /// Строка. Путь к файлу для упаковки. - /// Строка. Путь к файлу - результату упаковки. - /// Число. Уровень сжатия (0-2). - [ContextMethod("УпаковатьФайл")] - public void CompressFile(IValue InputFileName, IValue OutputFileName, int OutputCompressionLevel = 2) - { - - CompressionLevel fileCompressionLevel; - - switch (OutputCompressionLevel) - { - case 0: - fileCompressionLevel = CompressionLevel.NoCompression; - break; - case 1: - fileCompressionLevel = CompressionLevel.Fastest; - break; - case 2: - fileCompressionLevel = CompressionLevel.Optimal; - break; - default: - fileCompressionLevel = CompressionLevel.Optimal; - break; - } - - FileStream inputFileStream = File.Open(InputFileName.AsString(), FileMode.Open); - FileStream outputFileStream = File.Create(OutputFileName.AsString()); - var compressor = new DeflateStream(outputFileStream, fileCompressionLevel); - inputFileStream.CopyTo(compressor); - compressor.Close(); - inputFileStream.Close(); - outputFileStream.Close(); - } - - /// - /// Выполняет распаковку указанного файла по алгоритму Deflate - /// - /// Строка. Путь к файлу для распаковки. - /// Строка. Путь к файлу - результату распаковки. - [ContextMethod("РаспаковатьФайл")] - public void DecompressFile(IValue InputFileName, IValue OutputFileName) - { - - FileStream inputFileStream = File.Open(InputFileName.AsString(), FileMode.Open); - FileStream outputFileStream = File.Create(OutputFileName.AsString()); - var decompressor = new DeflateStream(inputFileStream, CompressionMode.Decompress); - decompressor.CopyTo(outputFileStream); - decompressor.Close(); - inputFileStream.Close(); - outputFileStream.Close(); - } - - /// - /// Создает УпаковщикDeflate - /// - /// УпаковщикDeflate - [ScriptConstructor] - public static IRuntimeContextInstance Constructor() - { - return new DeflatePacker(); - } - - } + } + else + { + throw RuntimeException.InvalidArgumentType("InputData"); + } + + MemoryStreamContext outputStream = MemoryStreamContext.Constructor(); + + DecompressStream(inputStream, outputStream); + + return outputStream.CloseAndGetBinaryData(); + + } + + /// + /// Выполняет упаковку входящего потока по алгоритму Deflate + /// + /// Поток. Исходный поток для упаковки. + /// Поток. Результат упаковки. + /// Число. Уровень сжатия (0-2). + [ContextMethod("УпаковатьПоток")] + public void CompressStream(IValue InputStream, IValue OutputStream, int OutputCompressionLevel = 2) + { + + CompressionLevel streamCompressionLevel; + + switch (OutputCompressionLevel) + { + case 0: + streamCompressionLevel = CompressionLevel.NoCompression; + break; + case 1: + streamCompressionLevel = CompressionLevel.Fastest; + break; + case 2: + streamCompressionLevel = CompressionLevel.Optimal; + break; + default: + streamCompressionLevel = CompressionLevel.Optimal; + break; + } + + DeflateStream compressor; + + if (OutputStream.AsObject() is IStreamWrapper outputStreamWrapper) + { + compressor = new DeflateStream(outputStreamWrapper.GetUnderlyingStream(), streamCompressionLevel, true); + } + else + { + throw RuntimeException.InvalidArgumentType("OutputStream"); + } + + if (InputStream.AsObject() is IStreamWrapper inputStreamWrapper) + { + inputStreamWrapper.GetUnderlyingStream().CopyTo(compressor); + compressor.Close(); + } + else + { + throw RuntimeException.InvalidArgumentType("InputStream"); + } + + } + + /// + /// Выполняет распаковку входящего потока по алгоритму Deflate + /// + /// Поток. Исходный поток для распаковки. + /// Поток. Результат распаковки. + [ContextMethod("РаспаковатьПоток")] + public void DecompressStream(IValue InputStream, IValue OutputStream) + { + + DeflateStream decompressor; + + if (InputStream.AsObject() is IStreamWrapper inputStreamWrapper) + { + decompressor = new DeflateStream(inputStreamWrapper.GetUnderlyingStream(), CompressionMode.Decompress, true); + } + else + { + throw RuntimeException.InvalidArgumentType("InputStream"); + } + + if (OutputStream.AsObject() is IStreamWrapper outputStreamWrapper) + { + decompressor.CopyTo(outputStreamWrapper.GetUnderlyingStream()); + decompressor.Close(); + } + else + { + throw RuntimeException.InvalidArgumentType("OutputStream"); + } + + } + + /// + /// Выполняет упаковку указанного файла по алгоритму Deflate + /// + /// Строка. Путь к файлу для упаковки. + /// Строка. Путь к файлу - результату упаковки. + /// Число. Уровень сжатия (0-2). + [ContextMethod("УпаковатьФайл")] + public void CompressFile(IValue InputFileName, IValue OutputFileName, int OutputCompressionLevel = 2) + { + + CompressionLevel fileCompressionLevel; + + switch (OutputCompressionLevel) + { + case 0: + fileCompressionLevel = CompressionLevel.NoCompression; + break; + case 1: + fileCompressionLevel = CompressionLevel.Fastest; + break; + case 2: + fileCompressionLevel = CompressionLevel.Optimal; + break; + default: + fileCompressionLevel = CompressionLevel.Optimal; + break; + } + + FileStream inputFileStream = File.Open(InputFileName.AsString(), FileMode.Open); + FileStream outputFileStream = File.Create(OutputFileName.AsString()); + var compressor = new DeflateStream(outputFileStream, fileCompressionLevel); + inputFileStream.CopyTo(compressor); + compressor.Close(); + inputFileStream.Close(); + outputFileStream.Close(); + } + + /// + /// Выполняет распаковку указанного файла по алгоритму Deflate + /// + /// Строка. Путь к файлу для распаковки. + /// Строка. Путь к файлу - результату распаковки. + [ContextMethod("РаспаковатьФайл")] + public void DecompressFile(IValue InputFileName, IValue OutputFileName) + { + + FileStream inputFileStream = File.Open(InputFileName.AsString(), FileMode.Open); + FileStream outputFileStream = File.Create(OutputFileName.AsString()); + var decompressor = new DeflateStream(inputFileStream, CompressionMode.Decompress); + decompressor.CopyTo(outputFileStream); + decompressor.Close(); + inputFileStream.Close(); + outputFileStream.Close(); + } + + /// + /// Создает УпаковщикDeflate + /// + /// УпаковщикDeflate + [ScriptConstructor] + public static IRuntimeContextInstance Constructor() + { + return new DeflatePacker(); + } + + } } \ No newline at end of file diff --git a/src/oscript-component/oscript-component.csproj b/src/oscript-deflate/oscript-deflate.csproj similarity index 83% rename from src/oscript-component/oscript-component.csproj rename to src/oscript-deflate/oscript-deflate.csproj index 4668731..0a2b191 100644 --- a/src/oscript-component/oscript-component.csproj +++ b/src/oscript-deflate/oscript-deflate.csproj @@ -6,10 +6,10 @@ AnyCPU - $(MSBuildProjectDirectory)\bin\$(Configuration)\$(TargetFramework)\oscript-component.xml + $(MSBuildProjectDirectory)\bin\$(Configuration)\$(TargetFramework)\oscript-deflate.xml - $(MSBuildProjectDirectory)\bin\$(Configuration)\$(TargetFramework)\oscript-component.xml + $(MSBuildProjectDirectory)\bin\$(Configuration)\$(TargetFramework)\oscript-deflate.xml