TimeStamps are off when saving to MemoryStream

Aug 18, 2009 at 8:52 PM
Edited Aug 18, 2009 at 8:53 PM

I am using the latest DotNetZipLib and am wondering when I zip to a disk file and use:   zip.AddFile(FileName, PathName);

I get the correct modified time stamps of the files in the zip file.

However when I zip to a memory stream and use:  zip.AddEntry(FileName, PathName, File.ReadAllBytes(FileName));

I do not get correct modified time stamps in the zip file but the timestamps are all the same.  (I get the time of the zip operation).

thanks

Coordinator
Aug 18, 2009 at 9:24 PM
Edited Aug 18, 2009 at 9:27 PM

The behavior you are observing is expected.

If you call ZipFile.AddFile(), you pass in the name of a file on the disk.  The DotNetZip library can then open the file and retrieve its timestamps, and embed those timestamps into the zip archive, once it is saved.

If you call ZipFile.AddEntry(), there is no way for DotNetZip to know where you got the content for the entry (in this case a byte array).  You called File.ReadAllBytes(), but DotNetZip doesn't know that.  To DotNetZip, it is just a byte array, and there is no way for DotNetZip to relate that byte array back to the source filesystem file.  The bytes could just as easily have been randomly generated - there is no way for DotNetZip to know where you got them.  Therefore there is no way for DotNetZip to intelligently set the timestamps on an entry added this way.

You seem to be mixing up 2 things:  you said "when I zip to a disk file" and "when I zip to a memory stream".    I think when you say "zip to a disk file" you mean, savve the zip archive to a file in the filesystem.  And when you say "zip to a memory stream" you mean "save the zip archive to a MemoryStream."   But the timestamps for the entries in a zipfile will not vary depending on how you save the ZipFile.  The timestamps vary (as I described above) depending on how you add the entries.

code example #1:

using (var zip = new ZipFile())
{
    zip.AddFile("MyFile.txt");
    zip.Save("MyArchive.zip");
}

The above code will save the zip archive into a filesystem file, and timestamps for the single entry will be intact.

example 2:

using (var ms = new MemoryStream())
{
  using (var zip = new ZipFile())
  {
      zip.AddFile("MyFile.txt");
      zip.Save(ms);
  }
}

The above code will save the zip archive into a memory stream, and timestamps for the single entry will be intact.

On the other hand, example 3:

using (var zip = new ZipFile())
{
    zip.AddEntry("MyFile.txt","",File.ReadAllBytes("MyFile.txt"));
    zip.Save("MyArchive.zip");
}

The above code will save the zip archive into a filesystem file, but timestamps for the single entry will all show "the current time".

Likewise, example 4:

using (var ms = new MemoryStream())
{
  using (var zip = new ZipFile())
  {
      zip.AddEntry("MyFile.txt","",File.ReadAllBytes("MyFile.txt"));
      zip.Save(ms);
  }
}

The above code will save the zip archive into a MemoryStream, but timestamps for the single entry will all show "the current time".

Aug 20, 2009 at 4:15 PM

Thanks for the clear explanation.  Now I can zip in memory successfully with correct file timestamps and copy the files using a different set of credentials and then write to the zip file using default credentials!