NTFS file attributes being cleared from source files

Dec 11, 2009 at 3:08 PM

I've been using your DotNetZip library for a few months now and it's been working great. I just discovered today when saving files to an archive, that the NTFS file attributes are all cleared from the source files. Several of the files I'm archiving need the read-only attribute set to work properly. I've looked through the documentation and don't see anything about leaving the source file's attributes unaltered. What is interesting is when I extract the files, the archived files do have these attributes, so I don't understand why the original file would lose these. Any idea what causes this?


I call the AddDirectory method for adding files to an archive and use the following signature:

ZipFile.AddDirectory(string directoryName, string directoryPathInArchive)

 

I'm using version 1.8.4.24 of the full library (Ionic.Zip.dll)

 

Thanks,

Jim

Coordinator
Dec 11, 2009 at 5:48 PM
Edited Dec 11, 2009 at 7:52 PM

I don't see that behavior in any of my tests.

I also inspected the code, and there is only one place where the file attributes are written - that is in the extraction logic, where the original file attributes are restored.  You said you observed that this worked for you.

Attributes on the original source files are never set, within the DotNetZip library.

Can you reproduce this in a very simple test case?  I just tried and could not.

 

Coordinator
Dec 11, 2009 at 6:08 PM

Here's the code I tried.  It did not show any change in the file attributes for the file that was contained in the directory that I zipped up.

    public void Run()
    {
        string zipfile = "Restless.zip";
        string dir = "RestlessDir";
        if (Directory.Exists(dir))
        {
            ClearReadOnly(dir);
            Directory.Delete(dir, true);
        }

        Directory.CreateDirectory(dir);

        string filename = "ReadOnly.txt";
        string fullpath = Path.Combine(dir,filename);
        if (File.Exists(fullpath))
            File.Delete(fullpath);

        // create the file
        string createText = "Hello and Welcome" + Environment.NewLine;
        File.WriteAllText(fullpath, createText);

        // set the readonly bit 
        var a = File.GetAttributes(fullpath);
        a |= FileAttributes.ReadOnly;
        File.SetAttributes(fullpath, a);

        // get and display the attributes
        a = File.GetAttributes(fullpath);
        System.Console.WriteLine("BEFORE file attrs: {0}", a.ToString());        

        // zip the directory
        using (var zip = new ZipFile())
        {
            zip.AddDirectory(dir, "dir");
            zip.Save(zipfile);
        }

        // get and display the attributes again
        a = File.GetAttributes(fullpath);
        System.Console.WriteLine("AFTER file attrs: {0}", a.ToString());

        // remove the directory
        ClearReadOnly(dir);
        Directory.Delete(dir, true);
    }


Dec 11, 2009 at 7:20 PM

Hi Cheeso,

Thanks for the quick reply. I'm sorry I wasted your time because it turns out your library had nothing to do with my problem. I was copying the source folders to a backup directory in a separate thread which was clearing the file attributes. I thought I commented out everything associated with that folder copy operation, but turns out I did not.

 

Thanks again.

 

Coordinator
Dec 11, 2009 at 8:26 PM

No problem, glad you figured it out.