diff --git a/SabreTools.Helper/Tools/ArchiveTools.cs b/SabreTools.Helper/Tools/ArchiveTools.cs index 9df4699d..e0279880 100644 --- a/SabreTools.Helper/Tools/ArchiveTools.cs +++ b/SabreTools.Helper/Tools/ArchiveTools.cs @@ -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; diff --git a/SabreTools.sln b/SabreTools.sln index fa7bfab3..fcbab6b9 100644 --- a/SabreTools.sln +++ b/SabreTools.sln @@ -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 diff --git a/TGZTest/App.config b/TGZTest/App.config new file mode 100644 index 00000000..88fa4027 --- /dev/null +++ b/TGZTest/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/TGZTest/Properties/AssemblyInfo.cs b/TGZTest/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..9eac89ab --- /dev/null +++ b/TGZTest/Properties/AssemblyInfo.cs @@ -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")] diff --git a/TGZTest/TGZTest.cs b/TGZTest/TGZTest.cs new file mode 100644 index 00000000..ede8f0f8 --- /dev/null +++ b/TGZTest/TGZTest.cs @@ -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(); + } + } +} diff --git a/TGZTest/TGZTest.csproj b/TGZTest/TGZTest.csproj new file mode 100644 index 00000000..9d0567e9 --- /dev/null +++ b/TGZTest/TGZTest.csproj @@ -0,0 +1,86 @@ + + + + + Debug + AnyCPU + {73FBE2C1-39FB-4FFC-93F4-FB4998492429} + Exe + Properties + TGZTest + TGZTest + v4.5.2 + 512 + true + + + AnyCPU + true + full + false + ..\..\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + ..\..\Release\ + TRACE + prompt + 4 + + + true + ..\..\Debug-x64\ + DEBUG;TRACE + full + x64 + prompt + MinimumRecommendedRules.ruleset + true + + + ..\..\Release-x64\ + TRACE + true + pdbonly + x64 + prompt + MinimumRecommendedRules.ruleset + true + + + + + + + + + + + + + + + + + + + + + {225a1afd-0890-44e8-b779-7502665c23a5} + SabreTools.Helper + + + + + \ No newline at end of file