[ALL] Allow for original Dates to be written again in zipfiles

This commit is contained in:
Matt Nadareski
2016-10-14 16:58:15 -07:00
parent cbbab3518c
commit 1a998d575c
12 changed files with 245 additions and 115 deletions

View File

@@ -760,11 +760,12 @@ namespace SabreTools.Helper
/// <param name="streamSize">Size of the stream regardless of compression</param>
/// <param name="compressionMethod">Compression method to compare against</param>
/// <returns>Status of the underlying stream</returns>
public ZipReturn OpenReadStream(int index, bool raw, out Stream stream, out ulong streamSize, out CompressionMethod compressionMethod)
public ZipReturn OpenReadStream(int index, bool raw, out Stream stream, out ulong streamSize, out CompressionMethod compressionMethod, out uint lastMod)
{
// Set all of the defaults
streamSize = 0;
compressionMethod = CompressionMethod.Stored;
lastMod = 0;
_readIndex = index;
stream = null;
@@ -783,7 +784,7 @@ namespace SabreTools.Helper
}
// Now return the results of opening the local file
return _entries[index].OpenReadStream(raw, out stream, out streamSize, out compressionMethod);
return _entries[index].OpenReadStream(raw, out stream, out streamSize, out compressionMethod, out lastMod);
}
/// <summary>
@@ -795,7 +796,7 @@ namespace SabreTools.Helper
/// <param name="streamSize">Size of the stream regardless of compression</param>
/// <param name="compressionMethod">Compression method to compare against</param>
/// <returns>Status of the underlying stream</returns>
public ZipReturn OpenReadStreamQuick(ulong pos, bool raw, out Stream stream, out ulong streamSize, out CompressionMethod compressionMethod)
public ZipReturn OpenReadStreamQuick(ulong pos, bool raw, out Stream stream, out ulong streamSize, out CompressionMethod compressionMethod, out uint lastMod)
{
// Get the temporary entry based on the defined position
ZipFileEntry tempEntry = new ZipFileEntry(_zipstream);
@@ -812,12 +813,13 @@ namespace SabreTools.Helper
stream = null;
streamSize = 0;
compressionMethod = CompressionMethod.Stored;
lastMod = 0;
return zr;
}
_readIndex = 0;
// Return the file stream if it worked
return tempEntry.OpenReadStream(raw, out stream, out streamSize, out compressionMethod);
return tempEntry.OpenReadStream(raw, out stream, out streamSize, out compressionMethod, out lastMod);
}
/// <summary>
@@ -838,7 +840,8 @@ namespace SabreTools.Helper
/// <param name="compressionMethod">Compression method to compare against</param>
/// <param name="stream">Output stream representing the correctly compressed stream</param>
/// <returns>Status of the underlying stream</returns>
public ZipReturn OpenWriteStream(bool raw, bool torrentZip, string filename, ulong uncompressedSize, CompressionMethod compressionMethod, out Stream stream)
public ZipReturn OpenWriteStream(bool raw, bool torrentZip, string filename, ulong uncompressedSize,
CompressionMethod compressionMethod, out Stream stream, uint lastMod = Constants.TorrentZipFileDateTime)
{
// Check to see if the stream is writable
stream = null;
@@ -848,7 +851,7 @@ namespace SabreTools.Helper
}
// Open the entry stream based on the current position
ZipFileEntry zfe = new ZipFileEntry(_zipstream, filename);
ZipFileEntry zfe = new ZipFileEntry(_zipstream, filename, lastMod: lastMod);
ZipReturn zr = zfe.OpenWriteStream(raw, torrentZip, uncompressedSize, compressionMethod, out stream);
_entries.Add(zfe);

View File

@@ -23,8 +23,7 @@ namespace SabreTools.Helper
private ArchiveVersion _versionMadeBy;
private ArchiveVersion _versionNeeded;
private GeneralPurposeBitFlag _generalPurposeBitFlag;
private ushort _lastModFileTime;
private ushort _lastModFileDate;
private uint _lastMod;
private uint _crc;
private ulong _compressedSize;
private ulong _uncompressedSize;
@@ -56,15 +55,10 @@ namespace SabreTools.Helper
get { return _generalPurposeBitFlag; }
private set { _generalPurposeBitFlag = value; }
}
public ushort LastModFileTime
public uint LastMod
{
get { return _lastModFileTime; }
set { _lastModFileTime = value; }
}
public ushort LastModFileDate
{
get { return _lastModFileDate; }
set { _lastModFileDate = value; }
get { return _lastMod; }
set { _lastMod = value; }
}
public byte[] CRC
{
@@ -135,14 +129,13 @@ namespace SabreTools.Helper
/// </summary>
/// <param name="zipstream">Stream representing the entry</param>
/// <param name="filename">Internal filename to use</param>
public ZipFileEntry(Stream zipstream, string filename)
public ZipFileEntry(Stream zipstream, string filename, uint lastMod = Constants.TorrentZipFileDateTime)
{
_zip64 = false;
_zipstream = zipstream;
_generalPurposeBitFlag = GeneralPurposeBitFlag.DeflatingMaximumCompression;
_compressionMethod = CompressionMethod.Deflated;
_lastModFileTime = 48128;
_lastModFileDate = 8600;
_lastMod = lastMod;
FileName = filename;
}
@@ -181,8 +174,7 @@ namespace SabreTools.Helper
}
// Keep reading available information, skipping the unnecessary
_lastModFileTime = br.ReadUInt16();
_lastModFileDate = br.ReadUInt16();
_lastMod = br.ReadUInt32();
_crc = br.ReadUInt32();
_compressedSize = br.ReadUInt32();
_uncompressedSize = br.ReadUInt32();
@@ -356,8 +348,7 @@ namespace SabreTools.Helper
bw.Write(versionNeededToExtract);
bw.Write((ushort)_generalPurposeBitFlag);
bw.Write((ushort)_compressionMethod);
bw.Write(_lastModFileTime);
bw.Write(_lastModFileDate);
bw.Write(_lastMod);
bw.Write(_crc);
bw.Write(compressedSize32);
bw.Write(uncompressedSize32);
@@ -413,11 +404,7 @@ namespace SabreTools.Helper
{
return ZipReturn.ZipLocalFileHeaderError;
}
if (br.ReadUInt16() != _lastModFileTime)
{
return ZipReturn.ZipLocalFileHeaderError;
}
if (br.ReadUInt16() != _lastModFileDate)
if (br.ReadUInt32() != _lastMod)
{
return ZipReturn.ZipLocalFileHeaderError;
}
@@ -616,8 +603,7 @@ namespace SabreTools.Helper
}
_compressionMethod = (CompressionMethod)br.ReadUInt16();
_lastModFileTime = br.ReadUInt16();
_lastModFileDate = br.ReadUInt16();
_lastMod = br.ReadUInt32();
_crc = br.ReadUInt32();
_compressedSize = br.ReadUInt32();
_uncompressedSize = br.ReadUInt32();
@@ -736,8 +722,7 @@ namespace SabreTools.Helper
bw.Write(versionNeededToExtract);
bw.Write((ushort)_generalPurposeBitFlag);
bw.Write((ushort)_compressionMethod);
bw.Write(_lastModFileTime);
bw.Write(_lastModFileDate);
bw.Write(_lastMod);
_crc32Location = (ulong)_zipstream.Position;
@@ -780,10 +765,11 @@ namespace SabreTools.Helper
/// <param name="streamSize">Size of the stream regardless of compression</param>
/// <param name="compressionMethod">Compression method to compare against</param>
/// <returns>Status of the underlying stream</returns>
public ZipReturn OpenReadStream(bool raw, out Stream stream, out ulong streamSize, out CompressionMethod compressionMethod)
public ZipReturn OpenReadStream(bool raw, out Stream stream, out ulong streamSize, out CompressionMethod compressionMethod, out uint lastMod)
{
streamSize = 0;
compressionMethod = _compressionMethod;
lastMod = _lastMod;
_readStream = null;
_zipstream.Seek((long)_dataLocation, SeekOrigin.Begin);
@@ -834,6 +820,7 @@ namespace SabreTools.Helper
/// <param name="uncompressedSize">Uncompressed size of the stream</param>
/// <param name="compressionMethod">Compression method to compare against</param>
/// <param name="stream">Output stream representing the correctly compressed stream</param>
/// <param name="tzip">True if the file should use the TorrentZip date (default), false otherwise</param>
/// <returns>Status of the underlying stream</returns>
public ZipReturn OpenWriteStream(bool raw, bool torrentZip, ulong uncompressedSize, CompressionMethod compressionMethod, out Stream stream)
{