Extracted file changes Date modified

When I tried to extract the file using winrar, it will save the date changed for the file inside .gz. But when I extracted it using code that works (I got from other blogs):

 public static void Decompress(FileInfo fileToDecompress)
    {
        using (FileStream originalFileStream = fileToDecompress.OpenRead())
        {
            string currentFileName = fileToDecompress.FullName;
            string date = fileToDecompress.LastWriteTimeUtc.ToString();
            MessageBox.Show(date);
            string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);

            using (FileStream decompressedFileStream = File.Create(newFileName))
            {
                using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
                {
                    decompressionStream.CopyTo(decompressedFileStream);
                    Console.WriteLine("Decompressed: {0}", fileToDecompress.Name);
                }
            }
        }
    }

It changes the changed date of the extracted file to the current date, which is the date and time that I extracted.

How to save file modification date?

For example, the file in .gz is dated 8/13/2014, using winrar, which it did not change, but when I use my code, it changes to the current date that I extracted.

+4
source share
3 answers

, , , . , , , .

, , , File.SetLastWriteTime (http://msdn.microsoft.com/en-US/library/system.io.file.setlastwritetime(v=vs.110).aspx):

File.SetLastWriteTimeUtc(newFileName, fileToDecompress.LastWriteTimeUtc);
+1

.gz , , , http .

:

    using (Stream s = File.OpenRead("file.gz"))
    {
        uint timestamp = 0;

        for (int x = 0; x < 4; x++)
            s.ReadByte();

        for (int x = 0; x < 4; x++)
        {
            timestamp += (uint)s.ReadByte() << (8*x);
        }

        DateTime innerFileTime = new DateTime(1970, 1, 1).AddSeconds(timestamp)
            // Gzip times are stored in universal time, I actually needed two calls to get it almost right, and the time was still out by an hour
            .ToLocalTime().ToLocalTime();


        // Reset stream position before decompressing file
        s.Seek(0, SeekOrigin.Begin);
    }
+2

- gzip , :

MTIME ( )                           . Unix, ,              00:00:00 , 1 1970 . ( ,              MS-DOS ,             , .)              , MTIME ,              . MTIME = 0 ,             .

GZIP.

, , .

+1

All Articles