[ArchiveTools, TGZTest] Correct TGZ write, add test project (temp)

This commit is contained in:
Matt Nadareski
2016-08-24 20:33:35 -07:00
parent 8339dc1264
commit 1339e6e121
6 changed files with 201 additions and 37 deletions

View File

@@ -596,20 +596,14 @@ namespace SabreTools.Helper
// Now get the Rom info for the file so we have hashes and size
Rom rom = RomTools.GetSingleFileInfo(input);
// Now, check to make sure the output file doesn't already exist
string outfile = Path.Combine(outdir, rom.SHA1 + ".gz");
if (File.Exists(outfile))
{
return true;
}
// Rename the input file based on the SHA-1
string tempname = Path.Combine(Path.GetDirectoryName(input), rom.SHA1);
File.Move(input, tempname);
// If it doesn't exist, create the output file and then write
string outfile = Path.Combine(outdir, rom.SHA1 + ".gz");
using (FileStream inputstream = new FileStream(tempname, FileMode.Open))
using (GZipStream output = new GZipStream(File.OpenWrite(outfile), CompressionMode.Compress))
using (GZipStream output = new GZipStream(File.Open(outfile, FileMode.Create, FileAccess.Write), CompressionMode.Compress))
{
inputstream.CopyTo(output);
}
@@ -618,31 +612,38 @@ namespace SabreTools.Helper
File.Move(tempname, input);
// Now that it's renamed, inject the header info
using (BinaryReader br = new BinaryReader(File.Open(outfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
using (BinaryWriter bw = new BinaryWriter(File.Open(outfile, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)))
using (StreamWriter sw = new StreamWriter(new MemoryStream()))
using (BinaryWriter sw = new BinaryWriter(new MemoryStream()))
{
// Copy the first part of the file to the memory stream
sw.Write(br.ReadBytes(3)); //0x1F (ID1), 0x8B (ID2), 0x08 (Compression method - Deflate)
// Now write TGZ info
byte[] data = new byte[] { 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1c, 0x0 } // Flags with FEXTRA set (1), MTIME (4), XFLAG (1), OS (1), FEXTRA-LEN (2, Mirrored)
.Concat(StringToByteArray(rom.MD5)) // MD5
.Concat(StringToByteArray(rom.CRC)) // CRC
.Concat(BitConverter.GetBytes(rom.Size).Reverse().ToArray()) // Long size (Mirrored)
.ToArray();
sw.Write(data);
// Finally, copy the rest of the data from the original file
br.BaseStream.Seek(10, SeekOrigin.Begin);
while (br.BaseStream.Position < br.BaseStream.Length)
using (BinaryReader br = new BinaryReader(File.OpenRead(outfile)))
{
sw.Write(br.ReadByte());
// Copy the first part of the file to the memory stream
sw.Write(br.ReadBytes(3)); //0x1F (ID1), 0x8B (ID2), 0x08 (Compression method - Deflate)
// Now write TGZ info
byte[] data = new byte[] { 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1c, 0x0 } // Flags with FEXTRA set (1), MTIME (4), XFLAG (1), OS (1), FEXTRA-LEN (2, Mirrored)
.Concat(StringToByteArray(rom.MD5)) // MD5
.Concat(StringToByteArray(rom.CRC)) // CRC
.Concat(BitConverter.GetBytes(rom.Size).Reverse().ToArray()) // Long size (Mirrored)
.ToArray();
sw.Write(data);
// Finally, copy the rest of the data from the original file
br.BaseStream.Seek(10, SeekOrigin.Begin);
int i = 10;
while (br.BaseStream.Position < br.BaseStream.Length)
{
sw.Write(br.ReadByte());
i++;
}
}
// Now write the new file over the original
sw.BaseStream.Seek(0, SeekOrigin.Begin);
sw.BaseStream.CopyTo(bw.BaseStream);
using (BinaryWriter bw = new BinaryWriter(File.Open(outfile, FileMode.Create)))
{
// Now write the new file over the original
sw.BaseStream.Seek(0, SeekOrigin.Begin);
bw.BaseStream.Seek(0, SeekOrigin.Begin);
sw.BaseStream.CopyTo(bw.BaseStream);
}
}
return true;

View File

@@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SabreTools.Helper", "SabreT
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleSort", "SimpleSort\SimpleSort.csproj", "{7668FFA4-19AF-4F5D-8463-C7EF5B080FA4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TGZTest", "TGZTest\TGZTest.csproj", "{73FBE2C1-39FB-4FFC-93F4-FB4998492429}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -58,14 +60,14 @@ Global
{7668FFA4-19AF-4F5D-8463-C7EF5B080FA4}.Release|Any CPU.Build.0 = Release|Any CPU
{7668FFA4-19AF-4F5D-8463-C7EF5B080FA4}.Release|x64.ActiveCfg = Release|x64
{7668FFA4-19AF-4F5D-8463-C7EF5B080FA4}.Release|x64.Build.0 = Release|x64
{9B0BF92E-C347-4DB5-BF60-C34AEACD3FB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9B0BF92E-C347-4DB5-BF60-C34AEACD3FB5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9B0BF92E-C347-4DB5-BF60-C34AEACD3FB5}.Debug|x64.ActiveCfg = Debug|Any CPU
{9B0BF92E-C347-4DB5-BF60-C34AEACD3FB5}.Debug|x64.Build.0 = Debug|Any CPU
{9B0BF92E-C347-4DB5-BF60-C34AEACD3FB5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9B0BF92E-C347-4DB5-BF60-C34AEACD3FB5}.Release|Any CPU.Build.0 = Release|Any CPU
{9B0BF92E-C347-4DB5-BF60-C34AEACD3FB5}.Release|x64.ActiveCfg = Release|Any CPU
{9B0BF92E-C347-4DB5-BF60-C34AEACD3FB5}.Release|x64.Build.0 = Release|Any CPU
{73FBE2C1-39FB-4FFC-93F4-FB4998492429}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{73FBE2C1-39FB-4FFC-93F4-FB4998492429}.Debug|Any CPU.Build.0 = Debug|Any CPU
{73FBE2C1-39FB-4FFC-93F4-FB4998492429}.Debug|x64.ActiveCfg = Debug|x64
{73FBE2C1-39FB-4FFC-93F4-FB4998492429}.Debug|x64.Build.0 = Debug|x64
{73FBE2C1-39FB-4FFC-93F4-FB4998492429}.Release|Any CPU.ActiveCfg = Release|Any CPU
{73FBE2C1-39FB-4FFC-93F4-FB4998492429}.Release|Any CPU.Build.0 = Release|Any CPU
{73FBE2C1-39FB-4FFC-93F4-FB4998492429}.Release|x64.ActiveCfg = Release|Any CPU
{73FBE2C1-39FB-4FFC-93F4-FB4998492429}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

6
TGZTest/App.config Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>

View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("TGZTest")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TGZTest")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("73fbe2c1-39fb-4ffc-93f4-fb4998492429")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

33
TGZTest/TGZTest.cs Normal file
View File

@@ -0,0 +1,33 @@
using System.IO;
using SabreTools.Helper;
namespace SabreTools
{
public class TGZTest
{
public static void Main(string[] args)
{
Logger logger = new Logger(true, "tgztest.log");
logger.Start();
foreach (string arg in args)
{
string temparg = arg.Replace("\"", "").Replace("file://", "");
if (File.Exists(temparg))
{
ArchiveTools.WriteTorrentGZ(temparg, "tgz", logger);
}
else if (Directory.Exists(temparg))
{
foreach (string file in Directory.EnumerateFiles(temparg, "*", SearchOption.AllDirectories))
{
ArchiveTools.WriteTorrentGZ(file, "tgz", logger);
}
}
}
logger.Close();
}
}
}

86
TGZTest/TGZTest.csproj Normal file
View File

@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{73FBE2C1-39FB-4FFC-93F4-FB4998492429}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TGZTest</RootNamespace>
<AssemblyName>TGZTest</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\..\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>..\..\Debug-x64\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>..\..\Release-x64\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="TGZTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SabreTools.Helper\SabreTools.Helper.csproj">
<Project>{225a1afd-0890-44e8-b779-7502665c23a5}</Project>
<Name>SabreTools.Helper</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>