private void CollectDirectory(string root, DirectoryInfo directory, ZipFileInfoCollection zipFiles)
 {
     foreach (FileInfo file in directory.GetFiles())
     {
         CollectFile(root, file, zipFiles);
     }
     foreach (DirectoryInfo dir in directory.GetDirectories())
     {
         CollectDirectory(root, dir, zipFiles);
     }
 }
Exemple #2
0
 private void CollectDirectory(string root, DirectoryInfo directory, ZipFileInfoCollection zipFiles)
 {
     FileInfo[] files = directory.GetFiles();
     for (int i = 0; i < files.Length; i++)
     {
         FileInfo fileInfo = files[i];
         this.CollectFile(fileInfo.DirectoryName.Replace(root, "") + "\\", fileInfo, zipFiles);
     }
     DirectoryInfo[] directories = directory.GetDirectories();
     for (int j = 0; j < directories.Length; j++)
     {
         DirectoryInfo directory2 = directories[j];
         this.CollectDirectory(root, directory2, zipFiles);
     }
 }
Exemple #3
0
        public void Compress(string path)
        {
            ZipFileInfoCollection zipFileInfoCollection = new ZipFileInfoCollection();

            foreach (DirectoryInfo current in this._directories)
            {
                string root = (current.Parent == null) ? current.FullName : current.Parent.FullName;
                this.CollectDirectory(root, current, zipFileInfoCollection);
            }
            foreach (KeyValuePair <string, FileInfo> current2 in this._singleFiles)
            {
                this.CollectFile(current2.Key, current2.Value, zipFileInfoCollection);
            }
            using (GZipStream gZipStream = new GZipStream(File.Create(path), CompressionMode.Compress))
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                binaryFormatter.Serialize(gZipStream, zipFileInfoCollection);
            }
        }
        public void Compress(string path)
        {
            ZipFileInfoCollection zipFiles = new ZipFileInfoCollection(_token);

            foreach (var dir in _directories)
            {
                string root = dir.Parent == null ? dir.FullName : dir.Parent.FullName;
                CollectDirectory(root, dir, zipFiles);
            }
            foreach (FileInfo fileItem in _singleFiles)
            {
                CollectFile(null, fileItem, zipFiles);
            }
            using (GZipStream stream = new GZipStream(File.Create(path), CompressionMode.Compress))
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                binaryFormatter.Serialize(stream, zipFiles);
            }
        }
Exemple #5
0
        public MemoryStream ToMemoryStream()
        {
            ZipFileInfoCollection zipFileInfoCollection = new ZipFileInfoCollection();

            foreach (DirectoryInfo current in this._directories)
            {
                string root = (current.Parent == null) ? current.FullName : current.Parent.FullName;
                this.CollectDirectory(root, current, zipFileInfoCollection);
            }
            foreach (KeyValuePair <string, FileInfo> current2 in this._singleFiles)
            {
                this.CollectFile(current2.Key, current2.Value, zipFileInfoCollection);
            }
            MemoryStream    memoryStream    = new MemoryStream();
            BinaryFormatter binaryFormatter = new BinaryFormatter();

            binaryFormatter.Serialize(memoryStream, zipFileInfoCollection);
            memoryStream.Position = 0L;
            return(memoryStream);
        }
        public MemoryStream ToMemoryStream()
        {
            ZipFileInfoCollection zipFiles = new ZipFileInfoCollection(_token);

            foreach (var dir in _directories)
            {
                string root = dir.Parent == null ? dir.FullName : dir.Parent.FullName;
                CollectDirectory(root, dir, zipFiles);
            }
            foreach (FileInfo fileItem in _singleFiles)
            {
                CollectFile(null, fileItem, zipFiles);
            }

            MemoryStream    ms = new MemoryStream();
            BinaryFormatter binaryFormatter = new BinaryFormatter();

            binaryFormatter.Serialize(ms, zipFiles);
            ms.Position = 0;
            return(ms);
        }
Exemple #7
0
 public void Decompress(FileInfo fileToDecompress, string saveDirectory)
 {
     using (FileStream fileStream = fileToDecompress.OpenRead())
     {
         using (GZipStream gZipStream = new GZipStream(fileStream, CompressionMode.Decompress))
         {
             BinaryFormatter       binaryFormatter       = new BinaryFormatter();
             ZipFileInfoCollection zipFileInfoCollection = binaryFormatter.Deserialize(gZipStream) as ZipFileInfoCollection;
             foreach (ZipFileInfo current in zipFileInfoCollection)
             {
                 string path = saveDirectory + current.RelativePath;
                 if (!Directory.Exists(Path.GetDirectoryName(path)))
                 {
                     Directory.CreateDirectory(Path.GetDirectoryName(path));
                 }
                 using (FileStream fileStream2 = File.Create(path))
                 {
                     fileStream2.Write(current.FileBytes, 0, current.FileBytes.Length);
                 }
             }
         }
     }
 }
Exemple #8
0
 private void CollectFile(string root, FileInfo file, ZipFileInfoCollection zipFiles)
 {
     using (FileStream fileStream = file.OpenRead())
     {
         if ((File.GetAttributes(file.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & file.Extension != ".gz")
         {
             using (MemoryStream memoryStream = new MemoryStream())
             {
                 byte[] array = new byte[10240];
                 int    count;
                 while ((count = fileStream.Read(array, 0, array.Length)) > 0)
                 {
                     memoryStream.Write(array, 0, count);
                 }
                 zipFiles.Add(new ZipFileInfo
                 {
                     FileBytes    = memoryStream.ToArray(),
                     RelativePath = root + file.Name
                 });
             }
         }
     }
 }
 private void CollectFile(string root, FileInfo file, ZipFileInfoCollection zipFiles)
 {
     using (FileStream originalFileStream = file.OpenRead())
     {
         if ((File.GetAttributes(file.FullName) &
              FileAttributes.Hidden) != FileAttributes.Hidden & file.Extension != ".gz")
         {
             using (MemoryStream ms = new MemoryStream())
             {
                 byte[] buffer = new byte[10240];
                 int    count  = 0;
                 while ((count = originalFileStream.Read(buffer, 0, buffer.Length)) > 0)
                 {
                     ms.Write(buffer, 0, count);
                 }
                 zipFiles.Add(new ZipFileInfo
                 {
                     FileBytes    = ms.ToArray(),
                     RelativePath = string.IsNullOrEmpty(root) ? "\\" + file.Name : file.DirectoryName.Replace(root, "") + "\\" + file.Name
                 });
             }
         }
     }
 }