MakeDirectoryEntry() public method

Make a new for a directory.
public MakeDirectoryEntry ( string directoryName ) : ZipEntry
directoryName string The raw untransformed name for the new directory
return ZipEntry
Exemplo n.º 1
0
        public void CreatedValues()
        {
            string tempDir=GetTempFilePath();
            Assert.IsNotNull(tempDir, "No permission to execute this test?");

            tempDir=Path.Combine(tempDir, "SharpZipTest");

            if( tempDir!=null ) {

                Directory.CreateDirectory(tempDir);

                try {
                    // Note the seconds returned will be even!
                    DateTime createTime=new DateTime(2100, 2, 27, 11, 07, 56);
                    DateTime lastWriteTime=new DateTime(2050, 11, 3, 7, 23, 32);
                    DateTime lastAccessTime=new DateTime(2050, 11, 3, 0, 42, 12);

                    string tempFile=Path.Combine(tempDir, "SharpZipTest.Zip");
                    using( FileStream f=File.Create(tempFile, 1024) ) {
                        f.WriteByte(0);
                    }

                    File.SetCreationTime(tempFile, createTime);
                    File.SetLastWriteTime(tempFile, lastWriteTime);
                    File.SetLastAccessTime(tempFile, lastAccessTime);

                    FileAttributes attributes=FileAttributes.Hidden;

                    File.SetAttributes(tempFile, attributes);
                    ZipEntryFactory factory=null;
                    ZipEntry entry;
                    int combinedAttributes=0;

                    try {
                        factory=new ZipEntryFactory();

                        factory.Setting=ZipEntryFactory.TimeSetting.CreateTime;
                        factory.GetAttributes=~((int)FileAttributes.ReadOnly);
                        factory.SetAttributes=(int)FileAttributes.ReadOnly;
                        combinedAttributes=(int)(FileAttributes.ReadOnly|FileAttributes.Hidden);

                        entry=factory.MakeFileEntry(tempFile);
                        Assert.AreEqual(createTime, entry.DateTime, "Create time failure");
                        Assert.AreEqual(entry.ExternalFileAttributes, combinedAttributes);
                        Assert.AreEqual(1, entry.Size);

                        factory.Setting=ZipEntryFactory.TimeSetting.LastAccessTime;
                        entry=factory.MakeFileEntry(tempFile);
                        Assert.AreEqual(lastAccessTime, entry.DateTime, "Access time failure");
                        Assert.AreEqual(1, entry.Size);

                        factory.Setting=ZipEntryFactory.TimeSetting.LastWriteTime;
                        entry=factory.MakeFileEntry(tempFile);
                        Assert.AreEqual(lastWriteTime, entry.DateTime, "Write time failure");
                        Assert.AreEqual(1, entry.Size);
                    }
                    finally {
                        File.Delete(tempFile);
                    }

                    // Do the same for directories
                    // Note the seconds returned will be even!
                    createTime=new DateTime(2090, 2, 27, 11, 7, 56);
                    lastWriteTime=new DateTime(2107, 12, 31, 23, 59, 58);
                    lastAccessTime=new DateTime(1980, 1, 1, 1, 0, 0);

                    Directory.SetCreationTime(tempDir, createTime);
                    Directory.SetLastWriteTime(tempDir, lastWriteTime);
                    Directory.SetLastAccessTime(tempDir, lastAccessTime);

                    factory.Setting=ZipEntryFactory.TimeSetting.CreateTime;
                    entry=factory.MakeDirectoryEntry(tempDir);
                    Assert.AreEqual(createTime, entry.DateTime, "Directory create time failure");
                    Assert.IsTrue((entry.ExternalFileAttributes&(int)FileAttributes.Directory)==(int)FileAttributes.Directory);

                    factory.Setting=ZipEntryFactory.TimeSetting.LastAccessTime;
                    entry=factory.MakeDirectoryEntry(tempDir);
                    Assert.AreEqual(lastAccessTime, entry.DateTime, "Directory access time failure");

                    factory.Setting=ZipEntryFactory.TimeSetting.LastWriteTime;
                    entry=factory.MakeDirectoryEntry(tempDir);
                    Assert.AreEqual(lastWriteTime, entry.DateTime, "Directory write time failure");
                }
                finally {
                    Directory.Delete(tempDir, true);
                }
            }
        }