另类压缩

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Diagnostics;
namespace DupFileCompression
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceFolder = args[0];
            string option = args[1].Substring(1);
            if (option == "c")
            {
                Console.WriteLine("Compress folder: {0}", args[0]);
                var dupFiles =
                    from file in Directory.GetFiles(args[0], "*", SearchOption.AllDirectories)
                    let fi = new FileInfo(file)
                    group file by String.Format("{0}{1}{2}", fi.Name, fi.Length, fi.LastWriteTime.Ticks) into g
                    select new { Id = g.Key, Files = g };
                int count = 0;
                foreach (var dupFile in dupFiles)
                {
                    int fileCount = dupFile.Files.Count();
                    if (fileCount < 2)
                    {
                        continue;
                    }
                    else
                    {
                        Console.WriteLine("Left: {0}", dupFile.Files.First());
                        foreach (var file in dupFile.Files.Skip(1))
                        {
                            Console.WriteLine("Delete: {0}", file);
                            File.Delete(file);
                            File.Create(Path.Combine(Path.GetDirectoryName(file), dupFile.Id)).Close();
                        }
                    }
                }
                Console.WriteLine(count);
            }
            else if (option == "d")
            {
                Console.WriteLine("Decompress folder: {0}", args[0]);
                var dupFiles =
                    from file in Directory.GetFiles(args[0], "*", SearchOption.AllDirectories)
                    let fi = new FileInfo(file)
                    group file by fi.Length == 0 ? fi.Name : String.Format("{0}{1}{2}", fi.Name, fi.Length, fi.LastWriteTime.Ticks) into g
                    select new { Id = g.Key, Files = g };
                int count = 0;
                foreach (var dupFile in dupFiles)
                {
                    int fileCount = dupFile.Files.Count();
                    if (fileCount < 2)
                    {
                        continue;
                    }
                    else
                    {
                        string first =
                            (from file in dupFile.Files
                             let fi = new FileInfo(file)
                             where fi.Length != 0
                             select file).First();
                        Console.WriteLine("Left: {0}", dupFile.Files.First());
                        Debug.Assert(new FileInfo(first).Length != 0);
                        var dupedFiles =
                            from file in dupFile.Files
                            let fi = new FileInfo(file)
                            where fi.Length == 0
                            select file;
                        foreach (var file in dupedFiles)
                        {
                            Console.WriteLine("Restore: {0}", file);
                            Debug.Assert(new FileInfo(file).Length == 0);
                            File.Delete(file);
                            File.Copy(first, Path.Combine(Path.GetDirectoryName(file),Path.GetFileName(first)), true);
                        }
                    }
                }
                Console.WriteLine(count);
            }
        }
    }
}
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s