internal static ZipArchiveEntry DoCreateEntryFromFile(ZipArchive destination,
string sourceFileName, string entryName, CompressionLevel? compressionLevel)
{
if (destination == null)
throw new ArgumentNullException(nameof(destination));
if (sourceFileName == null)
throw new ArgumentNullException(nameof(sourceFileName));
if (entryName == null)
throw new ArgumentNullException(nameof(entryName));
// Checking of compressionLevel is passed down to DeflateStream and the IDeflater implementation
// as it is a pluggable component that completely encapsulates the meaning of compressionLevel.
// Argument checking gets passed down to FileStream's ctor and CreateEntry
Contract.Ensures(Contract.Result<ZipArchiveEntry>() != null);
Contract.EndContractBlock();
using (Stream fs = new FileStream(sourceFileName, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 0x1000, useAsync: false))
{
ZipArchiveEntry entry = compressionLevel.HasValue
? destination.CreateEntry(entryName, compressionLevel.Value)
: destination.CreateEntry(entryName);
DateTime lastWrite = File.GetLastWriteTime(sourceFileName);
// If file to be archived has an invalid last modified time, use the first datetime representable in the Zip timestamp format
// (midnight on January 1, 1980):
if (lastWrite.Year < 1980 || lastWrite.Year > 2107)
lastWrite = new DateTime(1980, 1, 1, 0, 0, 0);
entry.LastWriteTime = lastWrite;
using (Stream es = entry.Open())
fs.CopyTo(es);
return entry;
}
}