Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
Prepared projects to allow building without self extracting (SFX) fun…
Browse files Browse the repository at this point in the history
…ctionality, to reduce build size.
  • Loading branch information
squid-box committed May 20, 2021
1 parent a2e13e1 commit 977c2ba
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 52 deletions.
12 changes: 7 additions & 5 deletions SevenZip.Tests/MiscellaneousTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@ public class MiscellaneousTests : TestBase
[Test]
public void SerializationTest()
{
var ex = new ArgumentException("blahblah");
var bf = new BinaryFormatter();
var argumentException = new ArgumentException("blahblah");
var binaryFormatter = new BinaryFormatter();

using (var ms = new MemoryStream())
{
using (var fileStream = File.Create(TemporaryFile))
{
bf.Serialize(ms, ex);
SevenZipCompressor cmpr = new SevenZipCompressor();
cmpr.CompressStream(ms, fileStream);
binaryFormatter.Serialize(ms, argumentException);
var compressor = new SevenZipCompressor();
compressor.CompressStream(ms, fileStream);
}
}
}

#if SFX
[Test]
public void CreateSfxArchiveTest([Values]SfxModule sfxModule)
{
Expand Down Expand Up @@ -59,6 +60,7 @@ public void CreateSfxArchiveTest([Values]SfxModule sfxModule)
process?.Kill();
});
}
#endif

[Test]
public void LzmaEncodeDecodeTest()
Expand Down
1 change: 1 addition & 0 deletions SevenZip.Tests/SevenZip.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<OutputPath>..\Stage\$(Configuration)\</OutputPath>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>SevenZipTests.snk</AssemblyOriginatorKeyFile>
<DefineConstants>SFX</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NUnit.ConsoleRunner" Version="3.10.0" />
Expand Down
43 changes: 20 additions & 23 deletions SevenZip/SevenZip.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>SevenZipSharp</AssemblyName>
<TargetFrameworks>netstandard2.0;net45;net5.0-windows</TargetFrameworks>
Expand All @@ -13,37 +13,42 @@
<OutputPath>..\Stage\$(Configuration)\</OutputPath>
<DocumentationFile>..\Stage\$(Configuration)\SevenZipSharp.xml</DocumentationFile>
<RegisterForComInterop>false</RegisterForComInterop>
<DefaultItemExcludes>$(DefaultItemExcludes);sfx\*</DefaultItemExcludes>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DefineConstants>TRACE;DEBUG;UNMANAGED</DefineConstants>
<DefineConstants>TRACE;DEBUG;UNMANAGED;SFX</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DefineConstants>TRACE;UNMANAGED</DefineConstants>
<DefineConstants>TRACE;UNMANAGED;SFX</DefineConstants>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="sfx\7zS.sfx" />
<EmbeddedResource Include="sfx\7zxSD_All.sfx" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="sfx\Configs.xml" />
</ItemGroup>
<ItemGroup>
<Content Include="7z.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="7z64.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="arch\Test.txt" />
<Content Include="sfx\Configs.xslt" />
<None Include="SevenZip.snk" />
</ItemGroup>
<ItemGroup>
<ItemGroup Condition="$(DefineConstants.Contains('SFX'))">
<EmbeddedResource Include="sfx\7z.sfx" />
<EmbeddedResource Include="sfx\7zCon.sfx" />
<EmbeddedResource Include="sfx\7zS.sfx" />
<EmbeddedResource Include="sfx\7zSD.sfx" />
<EmbeddedResource Include="sfx\7zxSD_All.sfx" />
<EmbeddedResource Include="sfx\7zxSD_All_x64.sfx" />
<EmbeddedResource Include="sfx\7zxSD_Deflate.sfx" />
<EmbeddedResource Include="sfx\7zxSD_Deflate_x64.sfx" />
<EmbeddedResource Include="sfx\7zxSD_LZMA.sfx" />
<EmbeddedResource Include="sfx\7zxSD_LZMA_x64.sfx" />
<EmbeddedResource Include="sfx\7zxSD_LZMA2.sfx" />
<EmbeddedResource Include="sfx\7zxSD_LZMA2_x64.sfx" />
<EmbeddedResource Include="sfx\7zxSD_PPMd.sfx" />
<EmbeddedResource Include="sfx\7zxSD_PPMd_x64.sfx" />
<EmbeddedResource Include="sfx\Configs.xml" />
<EmbeddedResource Include="sfx\Configs.xsd" />
<EmbeddedResource Include="sfx\Configs.xslt" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="arch\Test.bzip2.7z" />
Expand All @@ -52,19 +57,11 @@
<EmbeddedResource Include="arch\Test.ppmd.7z" />
<EmbeddedResource Include="arch\Test.rar" />
<EmbeddedResource Include="arch\Test.tar" />
<EmbeddedResource Include="arch\Test.txt" />
<EmbeddedResource Include="arch\Test.txt.bz2" />
<EmbeddedResource Include="arch\Test.txt.gz" />
<EmbeddedResource Include="arch\Test.txt.xz" />
<EmbeddedResource Include="arch\Test.zip" />
<None Include="SevenZip.snk" />
<EmbeddedResource Include="sfx\7z.sfx" />
<EmbeddedResource Include="sfx\7zCon.sfx" />
<EmbeddedResource Include="sfx\7zxSD_All_x64.sfx" />
<EmbeddedResource Include="sfx\7zxSD_Deflate_x64.sfx" />
<EmbeddedResource Include="sfx\7zxSD_LZMA2.sfx" />
<EmbeddedResource Include="sfx\7zxSD_LZMA2_x64.sfx" />
<EmbeddedResource Include="sfx\7zxSD_LZMA_x64.sfx" />
<EmbeddedResource Include="sfx\7zxSD_PPMd_x64.sfx" />
<EmbeddedResource Include="arch\Test.zip" />
</ItemGroup>
<ItemGroup>
<PackageReference Condition="'$(TargetFramework)' != 'net45'" Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
Expand Down
60 changes: 36 additions & 24 deletions SevenZip/SevenZipSfx.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace SevenZip
{
#if SFX
using System;
using System.Collections.Generic;
using System.Globalization;
Expand Down Expand Up @@ -68,7 +69,6 @@ public class SevenZipSfx
}
}

private SfxModule _module = SfxModule.Default;
private string _moduleFileName;
private Dictionary<SfxModule, List<string>> _sfxCommands;

Expand All @@ -77,7 +77,7 @@ public class SevenZipSfx
/// </summary>
public SevenZipSfx()
{
_module = SfxModule.Default;
SfxModule = SfxModule.Default;
CommonInit();
}

Expand All @@ -92,7 +92,7 @@ public SevenZipSfx(SfxModule module)
throw new ArgumentException("You must specify the custom module executable.", nameof(module));
}

_module = module;
SfxModule = module;
CommonInit();
}

Expand All @@ -102,15 +102,15 @@ public SevenZipSfx(SfxModule module)
/// <param name="moduleFileName"></param>
public SevenZipSfx(string moduleFileName)
{
_module = SfxModule.Custom;
SfxModule = SfxModule.Custom;
ModuleFileName = moduleFileName;
CommonInit();
}

/// <summary>
/// Gets the sfx module type.
/// </summary>
public SfxModule SfxModule => _module;
public SfxModule SfxModule { get; private set; }

/// <summary>
/// Gets or sets the custom sfx module file name
Expand All @@ -127,14 +127,14 @@ public string ModuleFileName
}

_moduleFileName = value;
_module = SfxModule.Custom;
SfxModule = SfxModule.Custom;
var sfxName = Path.GetFileName(value);

foreach (var mod in SfxSupportedModuleNames.Keys)
{
if (SfxSupportedModuleNames[mod].Contains(sfxName))
{
_module = mod;
SfxModule = mod;
}
}
}
Expand Down Expand Up @@ -178,15 +178,15 @@ private static SfxModule GetModuleByName(string name)
/// <param name="xmlDefinitions">The resource name for xml definitions</param>
private void LoadCommandsFromResource(string xmlDefinitions)
{
using (Stream cfg = Assembly.GetExecutingAssembly().GetManifestResourceStream(
using (var cfg = Assembly.GetExecutingAssembly().GetManifestResourceStream(
GetResourceString(xmlDefinitions + ".xml")))
{
if (cfg == null)
{
throw new SevenZipSfxValidationException("The configuration \"" + xmlDefinitions +
"\" does not exist.");
}
using (Stream schm = Assembly.GetExecutingAssembly().GetManifestResourceStream(
using (var schm = Assembly.GetExecutingAssembly().GetManifestResourceStream(
GetResourceString(xmlDefinitions + ".xsd")))
{
if (schm == null)
Expand All @@ -195,18 +195,18 @@ private void LoadCommandsFromResource(string xmlDefinitions)
"\" does not exist.");
}
var sc = new XmlSchemaSet();
using (XmlReader scr = XmlReader.Create(schm))
using (var scr = XmlReader.Create(schm))
{
sc.Add(null, scr);
var settings = new XmlReaderSettings {ValidationType = ValidationType.Schema, Schemas = sc};
string validationErrors = "";
var validationErrors = "";
settings.ValidationEventHandler +=
((s, t) =>
{
validationErrors += String.Format(CultureInfo.InvariantCulture, "[{0}]: {1}\n",
t.Severity.ToString(), t.Message);
});
using (XmlReader rdr = XmlReader.Create(cfg, settings))
using (var rdr = XmlReader.Create(cfg, settings))
{
_sfxCommands = new Dictionary<SfxModule, List<string>>();
rdr.Read();
Expand All @@ -218,7 +218,7 @@ private void LoadCommandsFromResource(string xmlDefinitions)
rdr.Read();
do
{
SfxModule mod = GetModuleByName(rdr["modules"]);
var mod = GetModuleByName(rdr["modules"]);
rdr.ReadStartElement("config");
rdr.Read();
if (rdr.Name == "id")
Expand All @@ -240,7 +240,7 @@ private void LoadCommandsFromResource(string xmlDefinitions)
}
} while (rdr.Name == "config");
}
if (!String.IsNullOrEmpty(validationErrors))
if (!string.IsNullOrEmpty(validationErrors))
{
throw new SevenZipSfxValidationException(
"\n" + validationErrors.Substring(0, validationErrors.Length - 1));
Expand All @@ -257,30 +257,37 @@ private void LoadCommandsFromResource(string xmlDefinitions)
/// <param name="settings">The sfx settings dictionary to validate.</param>
private void ValidateSettings(SfxSettings settings)
{
if (_module == SfxModule.Custom)
if (SfxModule == SfxModule.Custom)
{
return;
}
List<string> commands = _sfxCommands[_module];

var commands = _sfxCommands[SfxModule];

if (commands == null)
{
return;
}

var invalidCommands = new List<string>();
foreach (string command in settings.Keys)

foreach (var command in settings.Keys)
{
if (!commands.Contains(command))
{
invalidCommands.Add(command);
}
}

if (invalidCommands.Count > 0)
{
var invalidText = new StringBuilder("\nInvalid commands:\n");
foreach (string str in invalidCommands)

foreach (var str in invalidCommands)
{
invalidText.Append(str);
}

throw new SevenZipSfxValidationException(invalidText.ToString());
}
}
Expand All @@ -293,23 +300,26 @@ private void ValidateSettings(SfxSettings settings)
private static Stream GetSettingsStream(SfxSettings settings)
{
var ms = new MemoryStream();
byte[] buf = Encoding.UTF8.GetBytes(@";!@Install@!UTF-8!" + '\n');
var buf = Encoding.UTF8.GetBytes(@";!@Install@!UTF-8!" + '\n');
ms.Write(buf, 0, buf.Length);
foreach (string command in settings.Keys)

foreach (var command in settings.Keys)
{
buf =
Encoding.UTF8.GetBytes(String.Format(CultureInfo.InvariantCulture, "{0}=\"{1}\"\n", command,
settings[command]));
ms.Write(buf, 0, buf.Length);
}

buf = Encoding.UTF8.GetBytes(@";!@InstallEnd@!");
ms.Write(buf, 0, buf.Length);

return ms;
}

private SfxSettings GetDefaultSettings()
{
switch (_module)
switch (SfxModule)
{
default:
return null;
Expand All @@ -334,7 +344,7 @@ private SfxSettings GetDefaultSettings()
/// Writes the whole to the other one.
/// </summary>
/// <param name="src">The source stream to read from.</param>
/// <param name="dest">The destination stream to wrie to.</param>
/// <param name="dest">The destination stream to write to.</param>
private static void WriteStream(Stream src, Stream dest)
{
if (src == null)
Expand All @@ -350,6 +360,7 @@ private static void WriteStream(Stream src, Stream dest)
src.Seek(0, SeekOrigin.Begin);
var buf = new byte[32768];
int bytesRead;

while ((bytesRead = src.Read(buf, 0, buf.Length)) > 0)
{
dest.Write(buf, 0, bytesRead);
Expand Down Expand Up @@ -408,12 +419,12 @@ public void MakeSfx(Stream archive, SfxSettings settings, Stream sfxStream)

ValidateSettings(settings);

using (var sfx = Assembly.GetExecutingAssembly().GetManifestResourceStream(GetResourceString(SfxSupportedModuleNames[_module][0])))
using (var sfx = Assembly.GetExecutingAssembly().GetManifestResourceStream(GetResourceString(SfxSupportedModuleNames[SfxModule][0])))
{
WriteStream(sfx, sfxStream);
}

if (_module == SfxModule.Custom || _sfxCommands[_module] != null)
if (SfxModule == SfxModule.Custom || _sfxCommands[SfxModule] != null)
{
using (var set = GetSettingsStream(settings))
{
Expand Down Expand Up @@ -490,4 +501,5 @@ public void MakeSfx(string archiveFileName, SfxSettings settings, Stream sfxStre
}
}
}
#endif
}

0 comments on commit 977c2ba

Please sign in to comment.