preserve original datetime for creation date, modified date, accessed date

Jan 5, 2011 at 2:41 PM

 I have the following when creating and merging zip files

What the problem is : Don't know how to preserve the orginal created, modified and accessed time.

Please advise.

Thanks

jhw 

 ZipFile zipnew = ZipFile.Read(strPath);

ZipFile zipold = new ZipFile(path);

{using (System.IO.MemoryStream mem = new System.IO.MemoryStream(Convert.ToInt32(zenew.UncompressedSize)))

zenew.Extract(mem);

stringfn = zenew.FileName.ToString();

zipold.UpdateEntry(fn, mem.ToArray());

 } 

zipold.Save();

Coordinator
Jan 6, 2011 at 7:03 PM

Hi jfw,

First thing:  employ using clauses around your use of ZipFile.  It will avoid problems down the road.  For examples, check ANY of the documentation or code examples published on codeplex.com.

Now, on to your question:

The modified, created, and accessed times are available as properties on the ZipEntry object. I think that is zenew in your code, but your code isn't complete so I'm not sure.  In that case, you can reference zenew.AccessedTime, zenew.Modifiedtime, and zenew.CreatedTime.  You can also set these properties on the entry in the "old" (being updated) zipfile. 

Something like this:

...
zenew.Extract(mem);
var e = zipold.UpdateEntry(zenew.FileName, mem.ToArray());
e.AccessedTime = zenew.AccessedTime;
e.ModifiedTime = zenew.ModifiedTime;
e.CreatedTime = zenew.CreatedTime;
...

There are probably ways to do what you are doing that are more memory efficient, or faster, using streams. But what you are doing should work.

Jan 6, 2011 at 10:37 PM

Thanks for your reply.

How would I properly code the using in this scenario?

ZipFile zipnew = ZipFile.Read(strPath);

ZipFile zipold = new ZipFile(path);

foreach (ZipEntry zenew in zipnew)

{ using (System.IO.MemoryStream mem = new System.IO.MemoryStream(Convert.ToInt32(zenew.UncompressedSize)))

{

string fn = zenew.FileName.ToString();

// at this point fn is the full path and filename

DateTime mt = zenew.ModifiedTime;

DateTime ct = zenew.CreationTime;

DateTime at = zenew.AccessedTime;

DateTime lm = zenew.LastModified;

zenew.Extract(mem);

var zn = zipold.UpdateEntry(fn, mem.ToArray());

zn.AccessedTime = at;

zn.ModifiedTime = mt;

zn.CreationTime = ct;

}

}

zipold.Save();

zipnew.Dispose();

The zipold filename is truncated to just the filename and not the full path.

Thanks for your help.

Joe

From: Cheeso [email removed]
Sent: Thursday, January 06, 2011 1:04 PM
To: joe@eis-network.com
Subject: Re: preserve original datetime for creation date, modified date, accessed date [DotNetZip:240569]

From: Cheeso

Hi jfw,

First thing: employ using clauses around your use of ZipFile. It will avoid problems down the road. For examples, check ANY of the documentation or code examples published on codeplex.com.

Now, on to your question:

The modified, created, and accessed times are available as properties on the ZipEntry object. I think that is zenew in your code, but your code isn't complete so I'm not sure. In that case, you can reference zenew.AccessedTime, zenew.Modifiedtime, and zenew.CreatedTime. You can also set these properties on the entry in the "old" (being updated) zipfile.

Something like this:

...
zenew.Extract(mem);
var e = zipold.UpdateEntry(zenew.FileName, mem.ToArray());
e.AccessedTime = zenew.AccessedTime;
e.ModifiedTime = zenew.ModifiedTime;
e.CreatedTime = zenew.CreatedTime;
...

There are probably ways to do what you are doing that are more memory efficient, or faster, using streams. But what you are doing should work.

Coordinator
Jan 7, 2011 at 1:40 PM

e.Filename = fn