Exemple #1
0
        public int CompressArchive(Dictionary<string, Stream> files, string outFileName)
        {
            // create output archive
            SevenZipCompressor compressor = new SevenZipCompressor();
            try
            {
                var dict = new Dictionary<string, Stream>();

                foreach (var fileName in files.Keys)
                    dict.Add(fileName, files[fileName]);
                compressor.CompressStreamDictionary(dict, outFileName);
                return 0;

            }
            catch (Exception ex)
            {
                Logger.Logwrite(ex.Message);
                Console.Error.WriteLine("Error: Could not create output archive: " + outFileName);

                Console.Error.WriteLine("\t" + ex.Message);
                return 1;
            }
        }
Exemple #2
0
        private void BackupFiles(IList<IVssWMComponent> components, IDictionary<string, string> volumeMap,
                                   IDictionary<string, string> snapshotVolumeMap, IDictionary<string, string> vmNamesMap,
                                   Options options)
        {
            IList<System.IO.Stream> streams = new List<System.IO.Stream>();
            try
            {
                foreach (var component in components)
                {
                    string vmBackupPath = Path.Combine(options.Output, string.Format(options.OutputFormat, vmNamesMap[component.ComponentName], component.ComponentName, DateTime.Now, "7z"));
                    File.Delete(vmBackupPath);

                    var files = new Dictionary<string, System.IO.Stream>();

                    foreach (var file in component.Files)
                    {
                        string path;
                        if (file.IsRecursive)
                            path = file.Path;
                        else
                            path = Path.Combine(file.Path, file.FileSpecification);

                        // Get the longest matching path
                        var volumePath = volumeMap.Keys.OrderBy((o) => o.Length).Reverse().Where((o) => path.StartsWith(o, StringComparison.OrdinalIgnoreCase)).First();
                        var volumeName = volumeMap[volumePath];

                        var fileName = Path.GetFileName(path.Substring(volumePath.Length)).ToUpperInvariant();
                        var include = Path.GetExtension(fileName).ToLowerInvariant() != ".avhdx" && Path.GetExtension(fileName).ToLowerInvariant() != ".vmrs";

                        if (include && options.VhdInclude != null)
                        {
                            if (options.VhdInclude.Count(x => string.CompareOrdinal(x.ToUpperInvariant(), fileName) == 0) == 0)
                                include = false;
                        }

                        if (include && options.VhdIgnore != null)
                        {
                            if (options.VhdIgnore.Count(x => string.CompareOrdinal(x.ToUpperInvariant(), fileName) == 0) != 0)
                                include = false;
                        }

                        if (include)
                            AddPathToSevenZip(files, streams, snapshotVolumeMap[volumeName], volumePath.Length, path);
                    }

                    SevenZipExtractor.SetLibraryPath(Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "7z.dll"));

                    var sevenZip = new SevenZipCompressor();
                    sevenZip.ArchiveFormat = OutArchiveFormat.SevenZip;
                    sevenZip.CompressionMode = CompressionMode.Create;
                    sevenZip.DirectoryStructure = true;
                    sevenZip.PreserveDirectoryRoot = false;
                    sevenZip.CustomParameters.Add("mt", "on");

                    switch (options.CompressionLevel)
                    {
                        case 0:
                            sevenZip.CompressionLevel = CompressionLevel.None;
                            break;
                        case 1:
                            sevenZip.CompressionLevel = CompressionLevel.Fast;
                            break;
                        case 2:
                            sevenZip.CompressionLevel = CompressionLevel.Fast;
                            break;
                        case 3:
                            sevenZip.CompressionLevel = CompressionLevel.Low;
                            break;
                        case 4:
                            sevenZip.CompressionLevel = CompressionLevel.Low;
                            break;
                        case 5:
                            sevenZip.CompressionLevel = CompressionLevel.Low;
                            break;
                        case 6:
                            sevenZip.CompressionLevel = CompressionLevel.Normal;
                            break;
                        case 7:
                            sevenZip.CompressionLevel = CompressionLevel.High;
                            break;
                        case 8:
                            sevenZip.CompressionLevel = CompressionLevel.High;
                            break;
                        case 9:
                            sevenZip.CompressionLevel = CompressionLevel.Ultra;
                            break;
                    }

                    if (BackupProgress != null)
                    {
                        sevenZip.FileCompressionStarted += (sender, e) =>
                        {
                            var ebp = new BackupProgressEventArgs()
                            {
                                AcrhiveFileName = e.FileName,
                                Action = EventAction.StartingArchive
                            };

                            sevenZipCurrentFile = e.FileName;

                            Report7ZipProgress(component, volumeMap, ebp);

                            if (cancel)
                            {
                                e.Cancel = true;
                            }
                        };

                        sevenZip.FileCompressionFinished += (sender, e) =>
                        {
                            var ebp = new BackupProgressEventArgs()
                            {
                                AcrhiveFileName = sevenZipCurrentFile,
                                Action = EventAction.ArchiveDone
                            };

                            sevenZipCurrentFile = String.Empty;

                            Report7ZipProgress(component, volumeMap, ebp);
                        };

                        sevenZip.Compressing += (sender, e) =>
                        {
                            var ebp = new BackupProgressEventArgs()
                            {
                                AcrhiveFileName = sevenZipCurrentFile,
                                Action = EventAction.PercentProgress,
                                CurrentEntry = sevenZipCurrentFile,
                                PercentDone = e.PercentDone
                            };

                            Report7ZipProgress(component, volumeMap, ebp);

                            if (cancel)
                            {
                                e.Cancel = true;
                            }
                        };
                    }

                    if (string.IsNullOrEmpty(options.Password))
                        sevenZip.CompressStreamDictionary(files, vmBackupPath);
                    else
                        sevenZip.CompressStreamDictionary(files, vmBackupPath, options.Password);

                    if (cancel)
                    {
                        if (File.Exists(vmBackupPath))
                        {
                            File.Delete(vmBackupPath);
                        }
                        throw new BackupCancelledException();
                    }
                }
            }
            finally
            {
                // Make sure that all streams are closed
                foreach (var s in streams)
                    s.Close();
            }
        }
Exemple #3
0
        public void Write(CloudFile cloudFile, Stream stream, OnProgress onProgress)
        {
            logger.DebugFormat("7zip, start writing [{0}]", cloudFile.FullName());

            cloudFile.Format = CloudFileFormat.SevenZip;
            //cloudFile.Fingerprint = HashHelper.MD5(cloudFile.Fingerprint + password);
            stream.Position = 0;

            // set the name into the package
            Dictionary<String, Stream> dir = new Dictionary<string, Stream>();
            dir[cloudFile.Name] = stream;

            // drive low level provider to write data
            using (MemoryStream compressedStream = new MemoryStream())
            {
                SevenZipCompressor compressor = new SevenZipCompressor();

                compressor.Compressing += new EventHandler<ProgressEventArgs>(
                    (o, p) => {
                        onProgress(cloudFile, ProcessTypes.Compress, (p.PercentDone * stream.Length / 100), stream.Length);
                        logger.InfoFormat("Compressing [{0}], [{1}% of {2}MB]", cloudFile.Name, p.PercentDone, (int)(stream.Length / 1024 / 1024));
                    });

                compressor.CompressStreamDictionary(dir, compressedStream, password);

                compressedStream.Position = 0;

                cloudFile.MD5 = cloudFile.Fingerprint;
                cloudFile.Fingerprint = HashHelper.MD5(cloudFile.Fingerprint+password);
                provider.Write(cloudFile, compressedStream, onProgress);
            }

            logger.DebugFormat("7zip, finish writing [{0}]", cloudFile.FullName());
        }