public void ZipFiles( FileFullPath zipFileName, FullPath baseDir, IEnumerable <FileFullPath> filesToZip, bool optimizeFiles) { _taskContext.LogInfo($"Zipping {zipFileName}"); var zipFileFullPath = zipFileName.ToFullPath(); if (File.Exists(zipFileFullPath)) { File.Delete(zipFileFullPath); } List <FileFullPath> tmpList = filesToZip.ToList(); if (optimizeFiles) { tmpList = RemoveDuplicateFiles(tmpList, baseDir); } using (ZipArchive newFile = ZipFile.Open(zipFileFullPath, ZipArchiveMode.Create)) { foreach (FileFullPath fileToZip in tmpList) { LocalPath debasedFileName = fileToZip.ToFullPath().DebasePath(baseDir); newFile.CreateEntryFromFile(fileToZip.ToString(), debasedFileName, CompressionLevel.Optimal); } } }
private List <FileFullPath> RemoveDuplicateFiles(List <FileFullPath> filesToZip, FullPath baseDir) { ZipMetadata metadata = new ZipMetadata(); List <FileFullPath> list = new List <FileFullPath>(); while (filesToZip.Count > 0) { FileFullPath current = filesToZip[filesToZip.Count - 1]; var currentDebase = current.ToFullPath().DebasePath(baseDir); filesToZip.RemoveAt(filesToZip.Count - 1); ZipMetadataItem metaItem = new ZipMetadataItem { FileName = currentDebase }; metaItem.DestinationFiles.Add(currentDebase); metadata.Items.Add(metaItem); list.Add(current); byte[] firstHash = CalculateHash(current.ToString()); FileInfo currentInfo = new FileInfo(current.ToString()); for (int i = filesToZip.Count - 1; i >= 0; i--) { FileFullPath tmp = filesToZip[i]; if (tmp.FileName != current.FileName) { continue; } FileInfo tmpInfo = new FileInfo(tmp.ToString()); if (tmpInfo.Length != currentInfo.Length) { continue; } byte[] secondHash = CalculateHash(tmp.ToString()); if (!HashEqual(firstHash, secondHash)) { continue; } metaItem.DestinationFiles.Add(tmp.ToFullPath().DebasePath(baseDir)); filesToZip.RemoveAt(i); } } string metadataFile = Path.Combine(baseDir.ToString(), MetadataFileName); File.WriteAllText(metadataFile, JsonConvert.SerializeObject(metadata)); list.Add(new FileFullPath(metadataFile)); return(list); }
public ICollection <PackagedFileInfo> ListFiles() { List <PackagedFileInfo> files = new List <PackagedFileInfo>(); foreach (string fileName in _directoryFilesLister.ListFiles( _directoryPath.ToString(), _recursive, DirectoryFilter)) { FileFullPath fileNameFullPath = new FileFullPath(fileName); LocalPath debasedFileName = fileNameFullPath.ToFullPath().DebasePath(_directoryPath); if (!LoggingHelper.LogIfFilteredOut(fileName, FileFilter, _taskContext, _logFiles)) { continue; } PackagedFileInfo packagedFileInfo = new PackagedFileInfo(fileNameFullPath, debasedFileName); files.Add(packagedFileInfo); } return(files); }