Example #1
0
        public void GetArchiveItems()
        {
            var currentDirectoryInfo = new DirectoryInfo(AbsolutePath);

            foreach (var fileInfo in currentDirectoryInfo.GetFiles())
            {
                var archiveFile = new ArchiveFile()
                {
                    AbsolutePath = fileInfo.FullName,
                    RelativePath = RelativePath + @"\" + fileInfo.Name,
                    Name         = fileInfo.Name,
                    Length       = fileInfo.Length,
                };

                ArchiveItems.Add(archiveFile);
            }

            foreach (var directoryInfo in currentDirectoryInfo.GetDirectories())
            {
                var archiveFolder = new ArchiveFolder()
                {
                    AbsolutePath = directoryInfo.FullName,
                    RelativePath = RelativePath + @"\" + directoryInfo.Name,
                    Name         = directoryInfo.Name,
                };

                archiveFolder.GetArchiveItems();

                ArchiveItems.Add(archiveFolder);
            }
        }
Example #2
0
        /// <summary>
        /// 归档文件
        /// </summary>
        public Archiver(string targetFile, string[] filesOrFoldersToBeArchived)
        {
            TargetPath = targetFile;

            string directoryPath = null;

            foreach (var fileOrFolder in filesOrFoldersToBeArchived)
            {
                var info = GetFileOrFolder(fileOrFolder);
                if (info == null)
                {
                    continue;
                }

                if (info is FileInfo)
                {
                    var fileInfo = info as FileInfo;
                    if (directoryPath != null)
                    {
                        if (directoryPath != fileInfo.DirectoryName)
                        {
                            throw new NotSupportedException();
                        }
                    }
                    else
                    {
                        directoryPath = fileInfo.DirectoryName;
                    }

                    var archiveFile = new ArchiveFile()
                    {
                        AbsolutePath = fileInfo.FullName,
                        RelativePath = fileInfo.Name,
                        Name         = fileInfo.Name,
                        Length       = fileInfo.Length,
                    };

                    ArchiveItems.Add(archiveFile);
                }
                else if (info is DirectoryInfo)
                {
                    var directoryInfo = info as DirectoryInfo;
                    if (directoryPath != null)
                    {
                        if (directoryPath != directoryInfo.Parent.FullName)
                        {
                            throw new NotSupportedException();
                        }
                    }
                    else
                    {
                        directoryPath = directoryInfo.Parent.FullName;
                    }

                    var archiveFolder = new ArchiveFolder()
                    {
                        AbsolutePath = directoryInfo.FullName,
                        RelativePath = directoryInfo.Name,
                        Name         = directoryInfo.Name,
                    };

                    archiveFolder.GetArchiveItems();

                    ArchiveItems.Add(archiveFolder);
                }
            }
        }