[ALL] TorrentZip stuff

Taking a cue from RomVault and GordonJ, I'm including the Zlib code as converted by DotNetZip as well as creating a couple slightly more maleable structures for writing archives.
This commit is contained in:
Matt Nadareski
2016-09-15 12:16:33 -07:00
parent 5f79f4205c
commit 29b1330d97
26 changed files with 12095 additions and 104 deletions

View File

@@ -4,6 +4,7 @@ using SharpCompress.Archive.SevenZip;
using SharpCompress.Common;
using SharpCompress.Reader;
using System;
//using Ionic.Zlib;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
@@ -87,6 +88,20 @@ namespace SabreTools.Helper
return success;
}
/// <summary>
/// Copy a file to an output archive
/// </summary>
/// <param name="inputFile">Input filename to be moved</param>
/// <param name="outputDirectory">Output directory to build to</param>
/// <param name="rom">RomData representing the new information</param>
/// <returns>True if the archive was written properly, false otherwise</returns>
public static bool WriteToArchiveIonic(string inputFile, string outputDirectory, Rom rom)
{
return true;
}
/// <summary>
/// Write an input file to a torrent GZ file
/// </summary>
@@ -841,7 +856,7 @@ namespace SabreTools.Helper
br.ReadInt32(); // size of the central directory
position = br.ReadInt32(); // offset of start of central directory with respect to the starting disk number
int commentlength = br.ReadInt16();
zas.Comment = Style.ConvertHex(BitConverter.ToString(br.ReadBytes(commentlength)));
zas.Comment = Style.ConvertHexToAscii(BitConverter.ToString(br.ReadBytes(commentlength)));
}
}
@@ -1102,7 +1117,7 @@ namespace SabreTools.Helper
foreach (ZipArchiveEntryStruct zaes in zae.Entries)
{
offsets.Add(bw.BaseStream.Position);
bw.Write(new byte[] { 0x50, 0x4b, 0x03, 0x04 }); // local file header signature
bw.Write(Constants.LocalFileHeaderSignature);
bw.Write((ushort)20);
bw.Write((ushort)GeneralPurposeBitFlag.DeflatingMaximumCompression);
bw.Write((ushort)CompressionMethod.Deflated);
@@ -1128,7 +1143,7 @@ namespace SabreTools.Helper
zae.SOCDOffset = (int)bw.BaseStream.Position;
foreach (ZipArchiveEntryStruct zaes in zae.Entries)
{
bw.Write(new byte[] { 0x50, 0x4b, 0x01, 0x02 }); // central file header signature
bw.Write(Constants.CentralDirectoryHeaderSignature);
bw.Write((ushort)ArchiveVersion.MSDOSandOS2);
bw.Write((ushort)20);
bw.Write((ushort)GeneralPurposeBitFlag.DeflatingMaximumCompression);
@@ -1161,7 +1176,7 @@ namespace SabreTools.Helper
bw.Write(zae.EOCDOffset - zae.SOCDOffset);
bw.Write(zae.SOCDOffset);
bw.Write((short)22);
bw.Write(new char[] { 'T', 'O', 'R', 'R', 'E', 'N', 'T', 'Z', 'I', 'P', 'P', 'E', 'D', '-' });
bw.Write("TORRENTZIPPED-".ToCharArray());
}
using (BinaryReader br = new BinaryReader(File.OpenRead(output)))

View File

@@ -528,7 +528,7 @@ namespace SabreTools.Helper
/// <summary>
/// http://stackoverflow.com/questions/5613279/c-sharp-hex-to-ascii
/// </summary>
public static string ConvertHex(String hexString)
public static string ConvertHexToAscii(String hexString)
{
if (hexString.Contains("-"))
{
@@ -546,6 +546,36 @@ namespace SabreTools.Helper
return sb.ToString();
}
/// <summary>
/// http://stackoverflow.com/questions/15920741/convert-from-string-ascii-to-string-hex
/// </summary>
public static string ConvertAsciiToHex(string asciiString)
{
string hexOutput = "";
foreach (char _eachChar in asciiString.ToCharArray())
{
// Get the integral value of the character.
int value = Convert.ToInt32(_eachChar);
// Convert the decimal value to a hexadecimal value in string form.
hexOutput += String.Format("{0:X2}", value).Remove(0, 2);
// to make output as your eg
// hexOutput +=" "+ String.Format("{0:X}", value);
}
return hexOutput;
}
/// <summary>
/// https://github.com/gjefferyes/RomVault/blob/master/ROMVault2/SupportedFiles/Zip/zipFile.cs
/// </summary>
public static bool IsUnicode(string s)
{
char[] c = s.ToCharArray();
for (int i = 0; i < c.Length; i++)
if (c[i] > 255) return true;
return false;
}
#endregion
}
}