private static void AddFile(ZipFileBuilder builder, SPFile file, string folder) { using (Stream fileStream = file.OpenBinaryStream()) { builder.Add(folder + "\\" + file.Name, fileStream); fileStream.Close(); } }
protected void Page_Load(object sender, EventArgs e) { string listId = Request.Params["listId"]; if (string.IsNullOrEmpty(listId)) { return; } SPList list = SPContext.Current.Web.Lists.GetList(new Guid(listId), false); if (!list.IsDocumentLibrary()) { return; } string pItemIds = Request.Params["itemIDs"]; if (string.IsNullOrEmpty(pItemIds)) { return; } SPDocumentLibrary library = (SPDocumentLibrary)list; string[] sItemIds = pItemIds.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); int[] itemsIDs = new int[sItemIds.Length]; for (int i = 0; i < sItemIds.Length; i++) { itemsIDs[i] = Convert.ToInt32(sItemIds[i]); } if (itemsIDs.Length > 0) { using (MemoryStream ms = new MemoryStream()) { using (ZipFileBuilder builder = new ZipFileBuilder(ms)) { foreach (int id in itemsIDs) { SPListItem item = library.GetItemById(id); if (item.IsFolder()) { AddFolder(builder, item.Folder, string.Empty); } else { AddFile(builder, item.File, string.Empty); } } builder.Finish(); WriteStreamToResponse(ms); } } } }
private string CreateZipFile(Context context, params Android.Net.Uri[] files) { var fileDestinationPath = $"/sdcard/{DateTime.Now.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture)}.zip"; var zip = new ZipFileBuilder(this, fileDestinationPath); foreach (var file in files) { using (var stream = context.ContentResolver.OpenInputStream(file)) { zip.AddFile($"{Guid.NewGuid().ToString("N")}.jpg", stream); } } zip.Build(); return(fileDestinationPath); }
private void AddFolder(ZipFileBuilder builder, SPFolder folder, string parentFolder) { string folderPath = parentFolder == string.Empty ? folder.Name : parentFolder + "\\" + folder.Name; builder.AddDirectory(folderPath); foreach (SPFile file in folder.Files) { AddFile(builder, file, folderPath); } foreach (SPFolder subFolder in folder.SubFolders) { AddFolder(builder, subFolder, folderPath); } }
private void ZipBuilderInner(bool postUpdates) { // since this is testing a real job, I need to pass real connection strings from that project var dbConnection = Config["Values:ConnectionStrings:Database"]; var repo = new JobTrackerRepository(dbConnection); var logger = LoggerFactory.Create(config => config.AddDebug()).CreateLogger("testing"); // I already have some blobs in this location in this account. // a more robust test would upload some files first rather than assume these var job = QueueClientExtensions.SaveJobAsync(repo, "adamo", new ZipRequest() { ContainerName = "sample-uploads", BlobPrefix = "anonUser" }).Result; var storageConnection = Config["Values:ConnectionStrings:Storage"]; var function = new ZipFileBuilder(storageConnection, (id) => $"https://localhost:44377/JobUpdated/{id}", repo, logger) { PostStatusUpdates = postUpdates }; var result = function.ExecuteAsync(job.Id).Result; // verify the download works var response = _client.GetAsync(result.Url).Result; response.EnsureSuccessStatusCode(); // and is a valid zip file var tempFile = Path.Combine(Path.GetTempPath(), Path.GetTempFileName() + ".zip"); using (var downloadFile = File.Create(tempFile)) { response.Content.CopyToAsync(downloadFile).Wait(); } using (var zipFile = ZipFile.OpenRead(tempFile)) { Assert.IsTrue(zipFile.Entries.Any()); } File.Delete(tempFile); }