Exemplo n.º 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);
            }
        }
Exemplo n.º 2
0
        public void Unarchive()
        {
            using (var inputStream = new FileStream(SourcePath, FileMode.Open, FileAccess.Read))
            {
                using (var tempStream = new FileStream(SourcePath + ".tmp", FileMode.Create, FileAccess.ReadWrite))
                {
                    Compressor.GzipDecompress(inputStream, tempStream);

                    tempStream.Seek(0, SeekOrigin.Begin);

                    var header = new ArchiveEntityHeader();
                    header.ReadStream(tempStream);

                    for (int i = 0; i < header.Length; i++)
                    {
                        var archiveFile = new ArchiveFile();
                        archiveFile.ReadHeader(tempStream);
                        archiveFile.AbsolutePath = Path.Combine(TargetPath, archiveFile.RelativePath);

                        ArchiveItems.Add(archiveFile);
                    }

                    ArchiveItems.ForEach(x => x.ReadContent(tempStream));
                }

                File.Delete(SourcePath + ".tmp");
            }
        }
Exemplo n.º 3
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);
                }
            }
        }