[FileTools, SimpleSort, ZipFile/Entry] TorrentZip works! Mostly!

Writing to torrentzip works but for some reason the compressed streams are not being generated at the correct size. This is odd and seems to only affect newly added files and not ones copied from other archives. Also, found some glaring flaws in the headered output that explains why I was having issues previously. Typos D=
This commit is contained in:
Matt Nadareski
2016-09-16 11:37:55 -07:00
parent 444fc1696b
commit c69dddbc26
4 changed files with 313 additions and 286 deletions

View File

@@ -337,6 +337,7 @@ namespace SabreTools.Helper
else else
{ {
FileTools.WriteToArchive(input, _outdir, found); FileTools.WriteToArchive(input, _outdir, found);
//FileTools.WriteTorrentZip(input, _outdir, found, _logger);
} }
} }
} }
@@ -404,12 +405,14 @@ namespace SabreTools.Helper
else else
{ {
FileTools.WriteToArchive(newinput, _outdir, found); FileTools.WriteToArchive(newinput, _outdir, found);
//FileTools.WriteTorrentZip(newinput, _outdir, found, _logger);
} }
} }
// Then output the headered rom (renamed) // Then output the headered rom (renamed)
Rom newfound = found; Rom newfound = found;
newfound.Name = Path.GetFileNameWithoutExtension(newfound.Name) + " (" + rom.HashData.CRC + ")" + Path.GetExtension(newfound.Name); newfound.Name = Path.GetFileNameWithoutExtension(newfound.Name) + " (" + rom.HashData.CRC + ")" + Path.GetExtension(newfound.Name);
newfound.HashData = rom.HashData;
// Add rom to the matched list // Add rom to the matched list
key = newfound.HashData.Size + "-" + newfound.HashData.CRC; key = newfound.HashData.Size + "-" + newfound.HashData.CRC;
@@ -450,6 +453,7 @@ namespace SabreTools.Helper
else else
{ {
FileTools.WriteToArchive(input, _outdir, newfound); FileTools.WriteToArchive(input, _outdir, newfound);
//FileTools.WriteTorrentZip(input, _outdir, newfound, _logger);
} }
} }
} }
@@ -535,7 +539,8 @@ namespace SabreTools.Helper
} }
else else
{ {
FileTools.WriteToArchive(outfile, _outdir, found); FileTools.WriteToArchive(input, _outdir, found);
//FileTools.WriteTorrentZip(input, _outdir, found, _logger);
} }
try try

View File

@@ -86,6 +86,10 @@ namespace SabreTools.Helper
{ {
return _entries[i].SHA1; return _entries[i].SHA1;
} }
public bool Contains(string n)
{
return _entries.Contains(new ZipFileEntry(new MemoryStream(), n, true));
}
#endregion #endregion
@@ -658,6 +662,8 @@ namespace SabreTools.Helper
torrentZip &= zfe.TorrentZip; torrentZip &= zfe.TorrentZip;
} }
_centerDirSize = (ulong)_zipstream.Position - _centerDirStart;
// Then get the central directory hash // Then get the central directory hash
OptimizedCRC ocrc = new OptimizedCRC(); OptimizedCRC ocrc = new OptimizedCRC();
byte[] buffer = new byte[_centerDirSize]; byte[] buffer = new byte[_centerDirSize];
@@ -674,7 +680,6 @@ namespace SabreTools.Helper
_zipstream.Position = currentPosition; _zipstream.Position = currentPosition;
// Now set more of the information // Now set more of the information
_centerDirSize = (ulong)_zipstream.Position - _centerDirStart;
_fileComment = (torrentZip ? Encoding.ASCII.GetBytes(("TORRENTZIPPED-" + calculatedCrc).ToCharArray()) : new byte[0]); _fileComment = (torrentZip ? Encoding.ASCII.GetBytes(("TORRENTZIPPED-" + calculatedCrc).ToCharArray()) : new byte[0]);
_zipStatus = (torrentZip ? ZipStatus.TorrentZip : ZipStatus.None); _zipStatus = (torrentZip ? ZipStatus.TorrentZip : ZipStatus.None);

View File

@@ -399,8 +399,8 @@ namespace SabreTools.Helper
_torrentZip = true; _torrentZip = true;
// Open the stream for reading // Open the stream for reading
using (BinaryReader br = new BinaryReader(_zipstream)) BinaryReader br = new BinaryReader(_zipstream);
{
// Set the position of the writer based on the entry information // Set the position of the writer based on the entry information
br.BaseStream.Seek((long)_relativeOffset, SeekOrigin.Begin); br.BaseStream.Seek((long)_relativeOffset, SeekOrigin.Begin);
@@ -547,7 +547,7 @@ namespace SabreTools.Helper
} }
// Back to code I understand // Back to code I understand
if (String.Equals(_fileName, tempFileName, StringComparison.InvariantCulture)) if (!String.Equals(_fileName, tempFileName, StringComparison.InvariantCulture))
{ {
return ZipReturn.ZipLocalFileHeaderError; return ZipReturn.ZipLocalFileHeaderError;
} }
@@ -556,13 +556,13 @@ namespace SabreTools.Helper
_dataLocation = (ulong)_zipstream.Position; _dataLocation = (ulong)_zipstream.Position;
// Now if no other data should be after the data, return // Now if no other data should be after the data, return
if((_generalPurposeBitFlag & GeneralPurposeBitFlag.ZeroedCRCAndSize) == GeneralPurposeBitFlag.ZeroedCRCAndSize) if((_generalPurposeBitFlag & GeneralPurposeBitFlag.ZeroedCRCAndSize) == 0)
{ {
return ZipReturn.ZipGood; return ZipReturn.ZipGood;
} }
// Otherwise, compare the data after the file too // Otherwise, compare the data after the file too
_zipstream.Seek((long)_compressedSize, SeekOrigin.Current); _zipstream.Position += (long)_compressedSize;
// If there's no subheader, read the next thing as crc // If there's no subheader, read the next thing as crc
uint tempCrc = br.ReadUInt32(); uint tempCrc = br.ReadUInt32();
@@ -584,7 +584,6 @@ namespace SabreTools.Helper
return ZipReturn.ZipLocalFileHeaderError; return ZipReturn.ZipLocalFileHeaderError;
} }
} }
}
catch catch
{ {
return ZipReturn.ZipLocalFileHeaderError; return ZipReturn.ZipLocalFileHeaderError;
@@ -605,8 +604,8 @@ namespace SabreTools.Helper
_torrentZip = true; _torrentZip = true;
// Open the stream for reading // Open the stream for reading
using (BinaryReader br = new BinaryReader(_zipstream)) BinaryReader br = new BinaryReader(_zipstream);
{
// Set the position of the writer based on the entry information // Set the position of the writer based on the entry information
br.BaseStream.Seek((long)_relativeOffset, SeekOrigin.Begin); br.BaseStream.Seek((long)_relativeOffset, SeekOrigin.Begin);
@@ -704,7 +703,6 @@ namespace SabreTools.Helper
// Set the position of the data // Set the position of the data
_dataLocation = (ulong)_zipstream.Position; _dataLocation = (ulong)_zipstream.Position;
} }
}
catch catch
{ {
return ZipReturn.ZipLocalFileHeaderError; return ZipReturn.ZipLocalFileHeaderError;
@@ -750,6 +748,7 @@ namespace SabreTools.Helper
bw.Write((ushort)_compressionMethod); bw.Write((ushort)_compressionMethod);
bw.Write(_lastModFileTime); bw.Write(_lastModFileTime);
bw.Write(_lastModFileDate); bw.Write(_lastModFileDate);
_crc32Location = (ulong)_zipstream.Position; _crc32Location = (ulong)_zipstream.Position;
// Now, write dummy bytes for crc, compressed size, and uncompressed size // Now, write dummy bytes for crc, compressed size, and uncompressed size
@@ -984,8 +983,9 @@ namespace SabreTools.Helper
using (OptimizedCRC crc = new OptimizedCRC()) using (OptimizedCRC crc = new OptimizedCRC())
using (MD5 md5 = System.Security.Cryptography.MD5.Create()) using (MD5 md5 = System.Security.Cryptography.MD5.Create())
using (SHA1 sha1 = System.Security.Cryptography.SHA1.Create()) using (SHA1 sha1 = System.Security.Cryptography.SHA1.Create())
using (BinaryReader fs = new BinaryReader(stream))
{ {
BinaryReader fs = new BinaryReader(stream);
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int read; int read;
while ((read = fs.Read(buffer, 0, buffer.Length)) > 0) while ((read = fs.Read(buffer, 0, buffer.Length)) > 0)
@@ -999,7 +999,7 @@ namespace SabreTools.Helper
md5.TransformFinalBlock(buffer, 0, 0); md5.TransformFinalBlock(buffer, 0, 0);
sha1.TransformFinalBlock(buffer, 0, 0); sha1.TransformFinalBlock(buffer, 0, 0);
tempCrc = (uint)crc.Value; tempCrc = crc.UnsignedValue;
_md5 = md5.Hash; _md5 = md5.Hash;
_sha1 = sha1.Hash; _sha1 = sha1.Hash;
} }

View File

@@ -129,10 +129,6 @@ namespace SabreTools.Helper
Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName)); Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName));
} }
// Open the input file for reading
readStream = File.OpenRead(inputFile);
ulong streamSize = (ulong)(new FileInfo(inputFile).Length);
// Open or create the archive // Open or create the archive
if (!File.Exists(archiveFileName)) if (!File.Exists(archiveFileName))
{ {
@@ -141,7 +137,16 @@ namespace SabreTools.Helper
else else
{ {
// Open the old archive for reading // Open the old archive for reading
oldZipFile.Open(archiveFileName, new FileInfo(archiveFileName).LastWriteTime.Ticks, false); oldZipFile.Open(archiveFileName, new FileInfo(archiveFileName).LastWriteTime.Ticks, true);
// If the old one contains the new file, then just skip out
if (oldZipFile.Contains(rom.Name))
{
success = true;
}
// Otherwise, process the old zipfile
else
{
zipFile.Create(archiveFileName + ".new"); zipFile.Create(archiveFileName + ".new");
// Copy over all files to the new archive // Copy over all files to the new archive
@@ -150,8 +155,8 @@ namespace SabreTools.Helper
// Instantiate the streams // Instantiate the streams
CompressionMethod icompressionMethod = CompressionMethod.Stored; CompressionMethod icompressionMethod = CompressionMethod.Stored;
ulong istreamSize = 0; ulong istreamSize = 0;
oldZipFile.OpenReadStream(i, true, out readStream, out istreamSize, out icompressionMethod); oldZipFile.OpenReadStream(i, false, out readStream, out istreamSize, out icompressionMethod);
zipFile.OpenWriteStream(true, true, oldZipFile.Filename(i), streamSize, CompressionMethod.Deflated, out writeStream); zipFile.OpenWriteStream(false, true, oldZipFile.Filename(i), istreamSize, CompressionMethod.Deflated, out writeStream);
// Copy the input stream to the output // Copy the input stream to the output
byte[] ibuffer = new byte[8 * 1024]; byte[] ibuffer = new byte[8 * 1024];
@@ -160,10 +165,22 @@ namespace SabreTools.Helper
{ {
writeStream.Write(ibuffer, 0, ilen); writeStream.Write(ibuffer, 0, ilen);
} }
writeStream.Flush();
zipFile.CloseWriteStream(BitConverter.ToUInt32(oldZipFile.CRC32(i), 0));
}
} }
} }
// Now open the write stream for the new rom // If the file has already been found, return it
if (success)
{
return success;
}
// Open the input file for reading
readStream = File.OpenRead(inputFile);
ulong streamSize = (ulong)(new FileInfo(inputFile).Length);
zipReturn = zipFile.OpenWriteStream(false, true, rom.Name, streamSize, CompressionMethod.Deflated, out writeStream); zipReturn = zipFile.OpenWriteStream(false, true, rom.Name, streamSize, CompressionMethod.Deflated, out writeStream);
// Copy the input stream to the output // Copy the input stream to the output