[英]file's date changes after zip in and out again, according to XCOPY
所以,这就是问题所在:我有一些普通文件,它们被放入一个ZIP文件中(有关ZIP的详细信息,请参见下文)。 然后,我将它们解压缩(有关所用工具的详细信息,请参见下文),然后还原文件。 按照使用的ZIP / UNZIP工具中的标准,恢复文件的日期。 使用DIR进行查询时,或在Windows资源管理器中,所涉及的文件具有与ZIP / UNZIP进程处理之前相同的日期。
所以,一切正常。
但是,然后,我使用XCOPY /D
命令来进一步操作磁盘上这些文件的不同副本……XCOPY说:一个文件比另一个文件新。 给定事实,日期,小时,直到分钟都是相同的..区别在于较小的实体,例如秒?
所有涉及的磁盘都有NTFS文件系统。
例:
C:\my>dir C:\windows\Background_mycomputer.cmd C:\my\directory\Background_mycomputer.cmd
Volume in drive C is mycomputerC
Volume Serial Number is 1234-5678
Directory of C:\windows
31/12/2014 19:50 51 Background_mycomputer.cmd
1 File(s) 51 bytes
Directory of C:\my\directory
31/12/2014 19:50 51 Background_mycomputer.cmd
1 File(s) 51 bytes
0 Dir(s) 33.655.316.480 bytes free
C:\my>xcopy C:\windows\Background_mycomputer.cmd C:\my\directory\Background_mycomputer.cmd /D
Overwrite C:\my\directory\Background_mycomputer.cmd (Yes/No/All)? y
C:\windows\Background_mycomputer.cmd
1 File(s) copied
C:\my>xcopy C:\my\directory\Background_mycomputer.cmd C:\windows\Background_mycomputer.cmd /D
0 File(s) copied
C:\my>xcopy C:\windows\Background_mycomputer.cmd C:\my\directory\Background_mycomputer.cmd /D
0 File(s) copied
C:\my>unzip -v
UnZip 6.00 of 20 April 2009, by Info-ZIP. Maintained by C. Spieler. Send
bug reports using http://www.info-zip.org/zip-bug.html; see README for details.
Latest sources and executables are at ftp://ftp.info-zip.org/pub/infozip/ ;
see ftp://ftp.info-zip.org/pub/infozip/UnZip.html for other sites.
Compiled with Microsoft C 13.10 (Visual C++ 7.1) for
Windows 9x / Windows NT/2K/XP/2K3 (32-bit) on Apr 20 2009.
UnZip special compilation options:
ASM_CRC
COPYRIGHT_CLEAN (PKZIP 0.9x unreducing method not supported)
NTSD_EAS
SET_DIR_ATTRIB
TIMESTAMP
UNIXBACKUP
USE_EF_UT_TIME
USE_UNSHRINK (PKZIP/Zip 1.x unshrinking method supported)
USE_DEFLATE64 (PKZIP 4.x Deflate64(tm) supported)
UNICODE_SUPPORT [wide-chars] (handle UTF-8 paths)
MBCS-support (multibyte character support, MB_CUR_MAX = 1)
LARGE_FILE_SUPPORT (large files over 2 GiB supported)
ZIP64_SUPPORT (archives using Zip64 for large files supported)
USE_BZIP2 (PKZIP 4.6+, using bzip2 lib version 1.0.5, 10-Dec-2007)
VMS_TEXT_CONV
[decryption, version 2.11 of 05 Jan 2007]
UnZip and ZipInfo environment options:
UNZIP: [none]
UNZIPOPT: [none]
ZIPINFO: [none]
ZIPINFOOPT: [none]
C:\my>ver
Microsoft Windows [Version 6.1.7601]
C:\my>zip -?
Copyright (c) 1990-2006 Info-ZIP - Type 'zip "-L"' for software license.
Zip 2.32 (June 19th 2006). Usage:
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
The default action is to add or replace zipfile entries from list, which
can include the special name - to compress standard input.
If zipfile and list are omitted, zip compresses stdin to stdout.
-f freshen: only changed files -u update: only changed or new files
-d delete entries in zipfile -m move into zipfile (delete files)
-r recurse into directories -j junk (don't record) directory names
-0 store only -l convert LF to CR LF (-ll CR LF to LF)
-1 compress faster -9 compress better
-q quiet operation -v verbose operation/print version info
-c add one-line comments -z add zipfile comment
-@ read names from stdin -o make zipfile as old as latest entry
-x exclude the following names -i include only the following names
-F fix zipfile (-FF try harder) -D do not add directory entries
-A adjust self-extracting exe -J junk zipfile prefix (unzipsfx)
-T test zipfile integrity -X eXclude eXtra file attributes
-! use privileges (if granted) to obtain all aspects of WinNT security
-R PKZIP recursion (see manual)
-$ include volume label -S include system and hidden files
-e encrypt -n don't compress these suffixes
C:\my>
问题:我不希望XCOPY在我知道它们无效的地方进行更新,因为时间格式做错了。 我该如何预防呢?
从我的角度来看,涉及到的是XCOPY,非常具体的ZIP和UNZIP以及NTFS文件系统。 哪一个做错了什么?
我必须强调,除了ZIP和UNZIP,对文件没有其他更改,例如更改1个文件,然后在不到60秒的时间内更改为另一个文件。
在测试时,显示的时间不是当前时间,也不是当前时间。 没有文件正在适应当前时间,该时间指的是该文件的上次更改,可能是过去的任何时间。 在这种情况下,是一天之后,但是可以是任何东西。
我注意到Raymond Chen在编写Powershell脚本( GitHub链接 )以使用System.IO.Compression
和System.IO.Compression.FileSystem
库刷新zip存档时描述的特殊行为。
有趣的是,Zip存档可以存储具有相同元数据(名称,相对路径,修改日期)的同一文件的多个副本。 在Windows资源管理器中,提取文件的第二个副本将失败,因为该文件已存在。
当试图防止重新压缩已存档的文件时,我检查了相对路径和日期,并注意到LastWriteTime中最多有两秒钟的差异。 此解决方法弥补了精度损失:
$AlreadyArchivedFile = ($WriteArchive.Entries | Where-Object {#zip will store multiple copies of the exact same file - prevent this by checking if already archived.
(($_.FullName -eq $RelativePath) -and ($_.Length -eq $File.Length) ) -and
([math]::Abs(($_.LastWriteTime.UtcDateTime - $File.LastWriteTimeUtc).Seconds) -le 2) #ZipFileExtensions timestamps are only precise within 2 seconds.
})
另外,IsDaylightSavingTime标志也不存储在Zip存档中。 结果,当提取的文件比原始存档的文件新一个小时时,我感到惊讶。 我尝试了几次,发现每次压缩和提取文件时,提取文件的时间戳都会增加一个小时。
这是一个非常丑陋的解决方法,它将存档文件的时间减少了一个小时,以使原始源文件和提取的文件时间戳保持一致:
If($File.LastWriteTime.IsDaylightSavingTime() -and $ArchivedFile){#HACK: fix for buggy date - adds an hour inside archive when the zipped file was created during PDT (files created during PST are not affected).
$entry = $WriteArchive.GetEntry($RelativePath)
$entry.LastWriteTime = ($File.LastWriteTime.ToLocalTime() - (New-TimeSpan -Hours 1))
}
可能有更好的方法来处理此问题。 不幸的是,我不知道以任何方式将文件的“夏令时”指示器存储在.Zip存档中,并且该信息丢失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.