Search code examples
c#windowssharpziplibfile-attributes

How to modify file attribute in zip file?


Using SharpZipLib, I'm adding files to a existing zip file:

        using (ZipFile zf = new ZipFile(zipFile)) {
            zf.BeginUpdate();
            foreach (FileInfo fileInfo in fileInfos) {
                string name = fileInfo.FullName.Substring(rootDirectory.Length);
                FileAttributes attributes = fileInfo.Attributes;
                if (clearArchiveAttribute) attributes &= ~FileAttributes.Archive;

                zf.Add(fileInfo.FullName, name);
//TODO: Modify file attribute?
            }
            zf.CommitUpdate();
            zf.Close();
        }

Now the task is to clear the Archive file attribute.
But unfortunately, I figured out that this is only possible when creating a new zip file using ZipOutputStream and set ExternalFileAttributes:

            // ...
            ZipEntry entry = new ZipEntry(name);
            entry.ExternalFileAttributes = (int)attributes;
            // ...

Is there a way to add files and modify file attributes?

Is this possible with DotNetZip?


Solution

  • Because the source of SharpZipLib is available, I added another overload of ZipFile.Add by myself:

        public void Add(string fileName, string entryName, int attributes) {
            if (fileName == null) {
                throw new ArgumentNullException("fileName");
            }
    
            if (entryName == null) {
                throw new ArgumentNullException("entryName");
            }
    
            CheckUpdating();
            ZipEntry entry = EntryFactory.MakeFileEntry(entryName);
            entry.ExternalFileAttributes = attributes;
            AddUpdate(new ZipUpdate(fileName, entry));
        }
    

    Works great...