string ZipFileToALocalTempFile(IActivityIOOperationsEndPoint src, IDev2ZipOperationTO args) { var packFile = src.IOPath.Path; var tempFileName = CreateTmpFile(); if (src.RequiresLocalTmpStorage()) { var tempDir = _common.CreateTmpDirectory(); var tmpFile = Path.Combine(tempDir, src.IOPath.Path.Split(src.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries) .Last()); packFile = tmpFile; using (var s = src.Get(src.IOPath, _filesToDelete)) { _fileWrapper.WriteAllBytes(tmpFile, s.ToByteArray()); } } using (var zip = new ZipFile()) { if (args.ArchivePassword != string.Empty) { zip.Password = args.ArchivePassword; } zip.CompressionLevel = _common.ExtractZipCompressionLevel(args.CompressionRatio); zip.AddFile(packFile, "."); zip.Save(tempFileName); } return(tempFileName); }
public string Zip(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, IDev2ZipOperationTO args) { Source = src; Destination = dst; Dev2ZipOperationTO = args; return("Successful"); }
string ValidateZipSourceDestinationFileOperation(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, IDev2ZipOperationTO args, Func <string> performAfterValidation) { _common.AddMissingFileDirectoryParts(src, dst); if (dst.PathIs(dst.IOPath) == enPathType.Directory) { var sourcePart = src.IOPath.Path.Split(src.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries) .Last(); if (src.PathIs(src.IOPath) == enPathType.File) { var fileInfo = new FileInfo(sourcePart); dst.IOPath.Path = dst.Combine(sourcePart.Replace(fileInfo.Extension, ".zip")); } else { dst.IOPath.Path = dst.IOPath.Path + ".zip"; } } else { var sourcePart = dst.IOPath.Path.Split(dst.PathSeperator().ToCharArray(), StringSplitOptions.RemoveEmptyEntries) .Last(); var fileInfo = new FileInfo(sourcePart); dst.IOPath.Path = dst.IOPath.Path.Replace(fileInfo.Extension, ".zip"); } if (!args.Overwrite && dst.PathExist(dst.IOPath)) { throw new Exception(ErrorResource.DestinationFileAlreadyExists); } var opStatus = CreateEndPoint(dst, new Dev2CRUDOperationTO(args.Overwrite), dst.PathIs(dst.IOPath) == enPathType.Directory); if (!opStatus.Equals(ResultOk)) { throw new Exception(string.Format(ErrorResource.RecursiveDirectoryCreateFailed, dst.IOPath.Path)); } return(performAfterValidation()); }
string TransferTempZipFileToDestination(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, IDev2ZipOperationTO args, string tmpZip) { string result; using (Stream s2 = new MemoryStream(_fileWrapper.ReadAllBytes(tmpZip))) { dst = ActivityIOFactory.CreateOperationEndPointFromIOPath( ActivityIOFactory.CreatePathFromString(dst.IOPath.Path, dst.IOPath.Username, dst.IOPath.Password, true, dst.IOPath.PrivateKeyFile)); var zipTransferArgs = new Dev2CRUDOperationTO(args.Overwrite); result = ResultOk; if (src.RequiresLocalTmpStorage()) { if (dst.Put(s2, dst.IOPath, zipTransferArgs, null, _filesToDelete) < 0) { result = ResultBad; } } else { var fileInfo = new FileInfo(src.IOPath.Path); if (fileInfo.Directory != null && Path.IsPathRooted(fileInfo.Directory.ToString())) { if (dst.Put(s2, dst.IOPath, zipTransferArgs, fileInfo.Directory.ToString(), _filesToDelete) < 0) { result = ResultBad; } } else { if (dst.Put(s2, dst.IOPath, zipTransferArgs, null, _filesToDelete) < 0) { result = ResultBad; } } } } return(result); }
string ZipDirectoryToALocalTempFile(IActivityIOOperationsEndPoint src, IDev2ZipOperationTO args) { var tmpDir = _common.CreateTmpDirectory(); var tempFilename = CreateTmpFile(); var tmpPath = ActivityIOFactory.CreatePathFromString(tmpDir, "", ""); var tmpEndPoint = ActivityIOFactory.CreateOperationEndPointFromIOPath(tmpPath); TransferDirectoryContents(src, tmpEndPoint, new Dev2CRUDOperationTO(true)); using (var zip = new ZipFile()) { zip.SaveProgress += (sender, eventArgs) => { if (eventArgs.CurrentEntry != null) { Dev2Logger.Debug($"Event Type: {eventArgs.EventType} Total Entries: {eventArgs.EntriesTotal} Entries Saved: {eventArgs.EntriesSaved} Current Entry: {eventArgs.CurrentEntry.FileName}", GlobalConstants.WarewolfDebug); } }; if (args.ArchivePassword != string.Empty) { zip.Password = args.ArchivePassword; } zip.CompressionLevel = _common.ExtractZipCompressionLevel(args.CompressionRatio); var toAdd = ListDirectory(tmpEndPoint, ReadTypes.FilesAndFolders); foreach (var p in toAdd) { if (tmpEndPoint.PathIs(p) == enPathType.Directory) { var directoryPathInArchive = p.Path.Replace(tmpPath.Path, ""); zip.AddDirectory(p.Path, directoryPathInArchive); } else { zip.AddFile(p.Path, "."); } } zip.Save(tempFilename); } var dir = new DirectoryHelper(); dir.CleanUp(tmpDir); return(tempFilename); }
public string Zip(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, IDev2ZipOperationTO args) { string status; try { status = _validator.ValidateZipSourceDestinationFileOperation(src, dst, args, () => { string tempFileName; tempFileName = src.PathIs(src.IOPath) == enPathType.Directory || Dev2ActivityIOPathUtils.IsStarWildCard(src.IOPath.Path) ? _implementation.ZipDirectoryToALocalTempFile(src, args) : _implementation.ZipFileToALocalTempFile(src, args); return(_implementation.TransferTempZipFileToDestination(src, dst, args, tempFileName)); }); } finally { RemoveAllTmpFiles(); } return(status); }
public string Zip(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, IDev2ZipOperationTO args) { string status; try { status = ValidateZipSourceDestinationFileOperation(src, dst, args, () => { string tempFileName; tempFileName = src.PathIs(src.IOPath) == enPathType.Directory || Dev2ActivityIOPathUtils.IsStarWildCard(src.IOPath.Path) ? ZipDirectoryToALocalTempFile(src, args) : ZipFileToALocalTempFile(src, args); return(TransferTempZipFileToDestination(src, dst, args, tempFileName)); }); } finally { _filesToDelete.ForEach(RemoveTmpFile); } return(status); }