string ZipDirectoryToALocalTempFile(IActivityIOOperationsEndPoint src, Dev2ZipOperationTO args) { // tmp dir for files required var tmpDir = CreateTmpDirectory(); var tempFilename = CreateTmpFile(); var tmpPath = ActivityIOFactory.CreatePathFromString(tmpDir, "", "",""); var tmpEndPoint = ActivityIOFactory.CreateOperationEndPointFromIOPath(tmpPath); // stage contents to local folder TransferDirectoryContents(src, tmpEndPoint, new Dev2CRUDOperationTO(true)); using(var zip = new ZipFile()) { zip.SaveProgress += (sender, eventArgs) => { if(eventArgs.CurrentEntry != null) { Dev2Logger.Log.Debug(string.Format("Event Type: {0} Total Entries: {1} Entries Saved: {2} Current Entry: {3}", eventArgs.EventType, eventArgs.EntriesTotal, eventArgs.EntriesSaved, eventArgs.CurrentEntry.FileName)); } }; // set password if exist if(args.ArchivePassword != string.Empty) { zip.Password = args.ArchivePassword; } // compression ratio zip.CompressionLevel = ExtractZipCompressionLevel(args.CompressionRatio); var toAdd = ListDirectory(tmpEndPoint, ReadTypes.FilesAndFolders); // add all files to archive 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); } // remove locally staged files DirectoryHelper.CleanUp(tmpDir); return tempFilename; }
string ZipFileToALocalTempFile(IActivityIOOperationsEndPoint src, Dev2ZipOperationTO args) { // normal not wild char file && not directory var packFile = src.IOPath.Path; var tempFileName = CreateTmpFile(); if(src.RequiresLocalTmpStorage()) { string tempDir = 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)) { File.WriteAllBytes(tmpFile, s.ToByteArray()); } } using(var zip = new ZipFile()) { // set password if exist if(args.ArchivePassword != string.Empty) { zip.Password = args.ArchivePassword; } // compression ratio zip.CompressionLevel = ExtractZipCompressionLevel(args.CompressionRatio); // add all files to archive zip.AddFile(packFile, "."); zip.Save(tempFileName); } return tempFileName; }
public string Zip(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, Dev2ZipOperationTO args) { string status; try { status = ValidateZipSourceDestinationFileOperation(src, dst, args, () => { string tempFileName; if(src.PathIs(src.IOPath) == enPathType.Directory || Dev2ActivityIOPathUtils.IsStarWildCard(src.IOPath.Path)) { tempFileName = ZipDirectoryToALocalTempFile(src, args); } else { tempFileName = ZipFileToALocalTempFile(src, args); } return TransferTempZipFileToDestination(src, dst, args, tempFileName); }); } finally { _filesToDelete.ForEach(RemoveTmpFile); } return status; }
string ValidateZipSourceDestinationFileOperation(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, Dev2ZipOperationTO args, Func<string> performAfterValidation) { 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("Destination file already exists and overwrite is set to false"); } //ensures destination folder structure exists var opStatus = CreateEndPoint(dst, new Dev2CRUDOperationTO(args.Overwrite), dst.PathIs(dst.IOPath) == enPathType.Directory); if(!opStatus.Equals("Success")) { throw new Exception("Recursive Directory Create Failed For [ " + dst.IOPath.Path + " ]"); } return performAfterValidation(); }
string TransferTempZipFileToDestination(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, Dev2ZipOperationTO args, string tmpZip) { // now transfer the zip file to the correct location string result; using(Stream s2 = new MemoryStream(File.ReadAllBytes(tmpZip))) { // add archive name to path 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; }
public string Zip(IActivityIOOperationsEndPoint src, IActivityIOOperationsEndPoint dst, Dev2ZipOperationTO args) { Source = src; Destination = dst; Dev2ZipOperationTO = args; return "Successful"; }